]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/UserController.php
temporary user select
[alttp.git] / app / Http / Controllers / UserController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\UserChanged;
6 use App\Models\User;
7 use Illuminate\Http\Request;
8
9 class UserController extends Controller
10 {
11
12         public function search(Request $request) {
13                 $validatedData = $request->validate([
14                         'phrase' => 'string|nullable',
15                 ]);
16
17                 $users = User::query();
18                 if (!empty($validatedData['phrase'])) {
19                         $users = $users->where('username', 'LIKE', '%'.$validatedData['phrase'].'%')
20                                 ->orWhere('nickname', 'LIKE', '%'.$validatedData['phrase'].'%');
21                 }
22                 $users = $users->limit(5);
23                 return $users->get()->toJson();
24         }
25
26         public function setLanguage(Request $request) {
27                 $user = $request->user();
28                 if (!$user) return;
29
30                 $validatedData = $request->validate([
31                         'language' => 'required|in:de,en',
32                 ]);
33
34                 $user->locale = $validatedData['language'];
35                 $user->update();
36
37                 return $user->toJson();
38         }
39
40         public function setNickname(Request $request, User $user) {
41                 $this->authorize('setNickname', $user);
42
43                 $validatedData = $request->validate([
44                         'nickname' => 'string',
45                 ]);
46
47                 $user->nickname = $validatedData['nickname'];
48                 $user->update();
49
50                 UserChanged::dispatch($user);
51
52                 return $user->toJson();
53         }
54
55         public function setStreamLink(Request $request, User $user) {
56                 $this->authorize('setStreamLink', $user);
57
58                 $validatedData = $request->validate([
59                         'stream_link' => 'required|url',
60                 ]);
61
62                 $user->stream_link = $validatedData['stream_link'];
63                 $user->update();
64
65                 UserChanged::dispatch($user);
66
67                 return $user->toJson();
68         }
69
70         public function single(Request $request, $id) {
71                 $user = User::findOrFail($id);
72                 $this->authorize('view', $user);
73                 $user->load('participation');
74                 $user->load('participation.tournament');
75                 $user->loadCount('round_first');
76                 $user->loadCount('round_second');
77                 $user->loadCount('round_third');
78                 $user->loadCount('tournament_first');
79                 $user->loadCount('tournament_second');
80                 $user->loadCount('tournament_third');
81                 return $user->toJson();
82         }
83
84 }