Skip to content

Commit f6f106a

Browse files
committed
refactpr: use laravel-data for Ban form requests
1 parent 4d4dbc0 commit f6f106a

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

app/Data/Staff/BanData.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* NOTICE OF LICENSE.
7+
*
8+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
9+
* The details is bundled with this project in the file LICENSE.txt.
10+
*
11+
* @project UNIT3D Community Edition
12+
*
13+
* @author Roardom <roardom@protonmail.com>
14+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
15+
*/
16+
17+
namespace App\Data\Staff;
18+
19+
use App\Attributes\FromCurrentUserId;
20+
use App\Models\User;
21+
use Spatie\LaravelData\Attributes\Validation\Exists;
22+
use Spatie\LaravelData\Attributes\Validation\Max;
23+
use Spatie\LaravelData\Data;
24+
25+
class BanData extends Data
26+
{
27+
public function __construct(
28+
#[Max(65535)]
29+
public string $banReason,
30+
#[Exists(User::class, 'id')]
31+
public int $ownedBy,
32+
#[FromCurrentUserId]
33+
public int $createdBy,
34+
) {
35+
}
36+
}

app/Http/Controllers/Staff/BanController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace App\Http\Controllers\Staff;
1818

19+
use App\Data\Staff\BanData;
1920
use App\Http\Controllers\Controller;
2021
use App\Http\Requests\Staff\StoreBanRequest;
2122
use App\Models\Ban;
@@ -24,6 +25,7 @@
2425
use App\Notifications\UserBan;
2526
use App\Services\Unit3dAnnounce;
2627
use Exception;
28+
use Illuminate\Container\Attributes\CurrentUser;
2729

2830
/**
2931
* @see \Tests\Todo\Feature\Http\Controllers\Staff\BanControllerTest
@@ -45,10 +47,9 @@ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vie
4547
*
4648
* @throws Exception
4749
*/
48-
public function store(StoreBanRequest $request): \Illuminate\Http\RedirectResponse
50+
public function store(BanData $data, #[CurrentUser] User $staff): \Illuminate\Http\RedirectResponse
4951
{
50-
$user = User::findOrFail($request->string('owned_by'));
51-
$staff = $request->user();
52+
$user = User::findOrFail($data->ownedBy);
5253

5354
abort_if($user->group->is_modo || $staff->is($user), 403);
5455

@@ -57,7 +58,7 @@ public function store(StoreBanRequest $request): \Illuminate\Http\RedirectRespon
5758
'can_download' => 0,
5859
]);
5960

60-
$ban = Ban::create(['created_by' => $staff->id] + $request->validated());
61+
$ban = Ban::create($data->toArray());
6162

6263
cache()->forget('user:'.$user->passkey);
6364

app/Http/Requests/Staff/StoreBanRequest.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)