3 namespace App\Http\Controllers;
5 use App\Events\ResultReported;
6 use App\Models\Participant;
7 use App\Models\Protocol;
10 use Illuminate\Http\Request;
12 class ResultController extends Controller
15 public function create(Request $request) {
16 $validatedData = $request->validate([
17 'forfeit' => 'boolean',
18 'participant_id' => 'required|exists:App\\Models\\Participant,id',
19 'round_id' => 'required|exists:App\\Models\\Round,id',
20 'time' => 'required_if:forfeit,false|numeric',
22 error_log(var_export($validatedData, true));
24 $participant = Participant::findOrFail($validatedData['participant_id']);
25 $round = Round::findOrFail($validatedData['round_id']);
27 $user = $request->user();
28 if ($user->id != $participant->user->id) {
29 $this->authorize('create', Result::class);
32 $result = Result::updateOrCreate([
33 'round_id' => $validatedData['round_id'],
34 'user_id' => $participant->user_id,
36 'forfeit' => $validatedData['forfeit'],
37 'time' => isset($validatedData['time']) ? $validatedData['time'] : 0,
40 Protocol::resultReported(
46 ResultReported::dispatch($result);
48 return $result->toJson();