3 namespace App\Http\Controllers;
5 use App\Events\ApplicationAdded;
6 use App\Events\TournamentChanged;
7 use App\Models\Application;
8 use App\Models\Protocol;
9 use App\Models\Tournament;
10 use Illuminate\Auth\Access\AuthorizationException;
11 use Illuminate\Http\Request;
13 class TournamentController extends Controller
16 public function apply(Request $request, Tournament $tournament) {
17 $this->authorize('apply', $tournament);
18 $application = new Application();
19 $application->tournament_id = $tournament->id;
20 $application->user_id = $request->user()->id;
22 ApplicationAdded::dispatch($application);
23 Protocol::applicationReceived($tournament, $application, $request->user());
24 return $tournament->toJson();
27 public function single(Request $request, $id) {
28 $tournament = Tournament::with(
36 $this->authorize('view', $tournament);
37 foreach ($tournament->rounds as $round) {
39 $this->authorize('seeResults', $round);
40 } catch (AuthorizationException) {
41 $round->hideResults();
44 return $tournament->toJson();
47 public function discord(Request $request, Tournament $tournament) {
48 $this->authorize('update', $tournament);
49 $validatedData = $request->validate([
50 'guild_id' => 'string|nullable',
52 if (array_key_exists('guild_id', $validatedData)) {
53 $tournament->discord = $validatedData['guild_id'];
56 if ($tournament->wasChanged()) {
57 TournamentChanged::dispatch($tournament);
58 Protocol::tournamentDiscord($tournament, $request->user());
60 return $tournament->toJson();
63 public function discordSettings(Request $request, Tournament $tournament) {
64 $this->authorize('update', $tournament);
65 $validatedData = $request->validate([
66 'round_category' => 'string|nullable',
67 'round_template' => 'string|nullable',
69 if (array_key_exists('round_category', $validatedData)) {
70 $tournament->discord_round_category = $validatedData['round_category'];
72 if (array_key_exists('round_template', $validatedData)) {
73 $tournament->discord_round_template = $validatedData['round_template'];
76 if ($tournament->wasChanged()) {
77 TournamentChanged::dispatch($tournament);
78 Protocol::tournamentDiscordSettings($tournament, $request->user());
80 return $tournament->toJson();
83 public function open(Request $request, Tournament $tournament) {
84 $this->authorize('update', $tournament);
85 $tournament->accept_applications = true;
87 TournamentChanged::dispatch($tournament);
88 Protocol::tournamentOpenen($tournament, $request->user());
89 return $tournament->toJson();
92 public function close(Request $request, Tournament $tournament) {
93 $this->authorize('update', $tournament);
94 $tournament->accept_applications = false;
96 TournamentChanged::dispatch($tournament);
97 Protocol::tournamentClosed($tournament, $request->user());
98 return $tournament->toJson();
101 public function lock(Request $request, Tournament $tournament) {
102 $this->authorize('update', $tournament);
103 $tournament->locked = true;
105 TournamentChanged::dispatch($tournament);
106 Protocol::tournamentLocked($tournament, $request->user());
107 return $tournament->toJson();
110 public function unlock(Request $request, Tournament $tournament) {
111 $this->authorize('update', $tournament);
112 $tournament->locked = false;
114 TournamentChanged::dispatch($tournament);
115 Protocol::tournamentUnlocked($tournament, $request->user());
116 return $tournament->toJson();