-
-
Notifications
You must be signed in to change notification settings - Fork 379
/
SeedboxController.php
84 lines (68 loc) · 2.5 KB
/
SeedboxController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers;
use App\Models\Seedbox;
use App\Models\User;
use Illuminate\Http\Request;
/**
* @see \Tests\Todo\Feature\Http\Controllers\SeedboxControllerTest
*/
class SeedboxController extends Controller
{
/**
* Get A Users Registered Seedboxes.
*/
public function index(Request $request, string $username): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$user = User::where('username', '=', $username)->firstOrFail();
\abort_unless(($request->user()->group->is_modo || $request->user()->id == $user->id), 403);
$seedboxes = Seedbox::where('user_id', '=', $user->id)->paginate(25);
return \view('seedbox.index', ['user' => $user, 'seedboxes' => $seedboxes]);
}
/**
* Store A Seedbox.
*/
protected function store(Request $request): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$seedbox = new Seedbox();
$seedbox->user_id = $user->id;
$seedbox->name = $request->input('name');
$seedbox->ip = $request->input('ip');
$v = \validator($seedbox->toArray(), [
'name' => 'required|alpha_num',
'ip' => 'required|unique:clients,ip',
]);
if ($v->fails()) {
return \redirect()->route('seedboxes.index', ['username' => $user->username])
->withErrors($v->errors());
}
$seedbox->save();
return \redirect()->route('seedboxes.index', ['username' => $user->username])
->withSuccess(\trans('user.seedbox-added-success'));
}
/**
* Delete A Seedbox.
*
* @throws \Exception
*/
protected function destroy(Request $request, Seedbox $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$seedbox = Seedbox::findOrFail($id);
\abort_unless(($user->group->is_modo || $user->id == $seedbox->user_id), 403);
$seedbox->delete();
return \redirect()->route('seedboxes.index', ['username' => $user->username])
->withSuccess(\trans('user.seedbox-deleted-success'));
}
}