]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ResultController.php
37ebf3b4acc51b995bf47b1f0f4d3686814f29da
[alttp.git] / app / Http / Controllers / ResultController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\ResultReported;
6 use App\Models\Participant;
7 use App\Models\Protocol;
8 use App\Models\Result;
9 use App\Models\Round;
10 use Illuminate\Http\Request;
11
12 class ResultController extends Controller
13 {
14
15         public function create(Request $request) {
16                 $validatedData = $request->validate([
17                         'participant_id' => 'required|exists:App\\Models\\Participant,id',
18                         'round_id' => 'required|exists:App\\Models\\Round,id',
19                         'time' => 'required|numeric',
20                 ]);
21
22                 $participant = Participant::findOrFail($validatedData['participant_id']);
23                 $round = Round::findOrFail($validatedData['round_id']);
24
25                 $user = $request->user();
26                 if ($user->id != $participant->user->id) {
27                         $this->authorize('create', Result::class);
28                 }
29
30                 $result = Result::updateOrCreate([
31                         'round_id' => $validatedData['round_id'],
32                         'user_id' => $participant->user_id,
33                 ], [
34                         'time' => $validatedData['time'],
35                 ]);
36
37                 Protocol::resultReported(
38                         $round->tournament,
39                         $result,
40                         $request->user(),
41                 );
42
43                 ResultReported::dispatch($result);
44
45                 return $result->toJson();
46         }
47
48 }