generated from liutk/owl-admin-base
70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use App\Models\Activity;
|
|
use App\Models\ActivityGame;
|
|
use App\Http\Resources\Api\ActivityGameResource;
|
|
use App\Http\Requests\JoinGameRequest;
|
|
use App\Services\Api\ActivityGameService;
|
|
|
|
class ActivityGameController extends ApiController
|
|
{
|
|
|
|
protected string $serviceName = ActivityGameService::class;
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$activityId = $request->input('activity_id', 0);
|
|
|
|
$games = ActivityGame::with(['logs'=> function($q){
|
|
$q->where('user_id', auth('api')->user()?->id ?? 0);
|
|
}])
|
|
->where('activity_id', $activityId)->show()->sort()
|
|
->simplePaginate($request->query('per_page', 20));
|
|
|
|
return $this->success(['games'=>ActivityGameResource::collection($games)->resolve()]);
|
|
}
|
|
|
|
public function latestGame(Request $request)
|
|
{
|
|
//获取最新的活动;
|
|
$activity = Activity::show()->sort()->first();
|
|
$game = $activity?->games()->show()
|
|
->whereDate('game_at', now())
|
|
->where('game_at', '>', now())
|
|
->where('state', 1)
|
|
->orderBy('game_at', 'asc')->first();
|
|
|
|
//若已经没有最新的活动了, 则拿取当天最后一个
|
|
if(!$game){
|
|
$game = $activity?->games()->show()
|
|
->whereDate('game_at', now())
|
|
->orderBy('game_at', 'desc')->first();
|
|
}
|
|
|
|
if($game){
|
|
$game->load(['logs'=> function($q){
|
|
$q->where('user_id', auth('api')->user()?->id ?? 0);
|
|
}]);
|
|
|
|
return ActivityGameResource::make($game);
|
|
}
|
|
|
|
return $this->success();
|
|
}
|
|
|
|
public function joinGame(ActivityGame $game, JoinGameRequest $request)
|
|
{
|
|
$res = $this->service->join($game, $request->user(), $request->input('score'));
|
|
|
|
if($res['status']){
|
|
return $this->success(null, '竞猜成功');
|
|
}else{
|
|
return $this->error($res['message']);
|
|
}
|
|
}
|
|
} |