You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\Concerns;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
|
|
use App\Models\Competition;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
|
|
trait ResolvesParticipantApplication
|
|
|
|
|
|
{
|
|
|
|
|
|
protected function resolvePublishedCompetitionFromRequest(Request $request): Competition
|
|
|
|
|
|
{
|
|
|
|
|
|
$slug = $request->query('competition_slug');
|
|
|
|
|
|
if (! is_string($slug) || trim($slug) === '') {
|
|
|
|
|
|
$slug = $request->header('X-Competition-Slug');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (! is_string($slug) || trim($slug) === '') {
|
|
|
|
|
|
abort(response()->json(['message' => '请附带 competition_slug(查询参数或 X-Competition-Slug 头)'], 422));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$slug = trim($slug);
|
|
|
|
|
|
|
|
|
|
|
|
$c = Competition::query()
|
|
|
|
|
|
->where('slug', $slug)
|
|
|
|
|
|
->where('published', true)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
if ($c === null) {
|
|
|
|
|
|
abort(response()->json(['message' => '赛事不存在或未发布'], 404));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function participantApplication(Request $request): Application
|
|
|
|
|
|
{
|
|
|
|
|
|
$competition = $this->resolvePublishedCompetitionFromRequest($request);
|
|
|
|
|
|
|
|
|
|
|
|
return Application::query()->firstOrCreate(
|
|
|
|
|
|
[
|
|
|
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
|
|
'competition_id' => $competition->id,
|
|
|
|
|
|
],
|
|
|
|
|
|
['status' => 'draft']
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|