55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Pick;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Validation\Rule;
|
||
|
|
|
||
|
|
class PicksController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$picks = Pick::query()
|
||
|
|
->where('user_id', $request->user()->id)
|
||
|
|
->get([
|
||
|
|
'id',
|
||
|
|
'match_id',
|
||
|
|
'selection',
|
||
|
|
'points_awarded',
|
||
|
|
'submitted_at',
|
||
|
|
'graded_at',
|
||
|
|
]);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'picks' => $picks,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$validated = $request->validate([
|
||
|
|
'match_id' => ['required', 'integer', 'exists:matches,id'],
|
||
|
|
'selection' => ['required', Rule::in(['home', 'draw', 'away'])],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$pick = Pick::updateOrCreate(
|
||
|
|
[
|
||
|
|
'user_id' => $request->user()->id,
|
||
|
|
'match_id' => $validated['match_id'],
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'selection' => $validated['selection'],
|
||
|
|
'submitted_at' => now(),
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return response()->json(
|
||
|
|
['pick' => $pick],
|
||
|
|
$pick->wasRecentlyCreated ? 201 : 200
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|