3 namespace App\Http\Controllers;
5 use App\Events\UserChanged;
7 use Illuminate\Http\Request;
9 class UserController extends Controller
12 public function search(Request $request) {
13 $validatedData = $request->validate([
14 'phrase' => 'string|nullable',
17 $users = User::query();
18 if (!empty($validatedData['phrase'])) {
19 $users = $users->where('username', 'LIKE', '%'.$validatedData['phrase'].'%')
20 ->orWhere('nickname', 'LIKE', '%'.$validatedData['phrase'].'%');
22 $users = $users->limit(5);
23 return $users->get()->toJson();
26 public function setLanguage(Request $request) {
27 $user = $request->user();
30 $validatedData = $request->validate([
31 'language' => 'required|in:de,en',
34 $user->locale = $validatedData['language'];
37 return $user->toJson();
40 public function setNickname(Request $request, User $user) {
41 $this->authorize('setNickname', $user);
43 $validatedData = $request->validate([
44 'nickname' => 'string',
47 $user->nickname = $validatedData['nickname'];
50 UserChanged::dispatch($user);
52 return $user->toJson();
55 public function setStreamLink(Request $request, User $user) {
56 $this->authorize('setStreamLink', $user);
58 $validatedData = $request->validate([
59 'stream_link' => 'required|url',
62 $user->stream_link = $validatedData['stream_link'];
65 UserChanged::dispatch($user);
67 return $user->toJson();
70 public function single(Request $request, $id) {
71 $user = User::findOrFail($id);
72 $this->authorize('view', $user);
73 $user->append('random_quote');
74 $user->load('participation');
75 $user->load('participation.tournament');
76 $user->loadCount('round_first');
77 $user->loadCount('round_second');
78 $user->loadCount('round_third');
79 $user->loadCount('tournament_first');
80 $user->loadCount('tournament_second');
81 $user->loadCount('tournament_third');
82 return $user->toJson();