]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TournamentController.php
9d62c23bac3475f2becac58caca85296756c68bc
[alttp.git] / app / Http / Controllers / TournamentController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\ApplicationAdded;
6 use App\Models\Application;
7 use App\Models\Tournament;
8 use Illuminate\Auth\Access\AuthorizationException;
9 use Illuminate\Http\Request;
10
11 class TournamentController extends Controller
12 {
13
14         public function apply(Request $request, Tournament $tournament) {
15                 $this->authorize('apply', $tournament);
16                 $application = new Application();
17                 $application->tournament_id = $tournament->id;
18                 $application->user_id = $request->user()->id;
19                 $application->save();
20                 ApplicationAdded::dispatch($application);
21                 return $tournament->toJson();
22         }
23
24         public function single(Request $request, $id) {
25                 $tournament = Tournament::with(
26                         'applications',
27                         'applications.user',
28                         'rounds',
29                         'rounds.results',
30                         'participants',
31                         'participants.user',
32                 )->findOrFail($id);
33                 $this->authorize('view', $tournament);
34                 foreach ($tournament->rounds as $round) {
35                         try {
36                                 $this->authorize('seeResults', $round);
37                         } catch (AuthorizationException) {
38                                 $round->hideResults();
39                         }
40                 }
41                 return $tournament->toJson();
42         }
43
44 }