diff --git a/app/Http/Controllers/RoomControlController.php b/app/Http/Controllers/RoomControlController.php index b9b932e..a24f1a8 100644 --- a/app/Http/Controllers/RoomControlController.php +++ b/app/Http/Controllers/RoomControlController.php @@ -5,9 +5,11 @@ namespace App\Http\Controllers; use App\Http\Requests\ReceiveRoomRegisterRequest; use App\Http\Requests\ReceiveRoomStatusDataRequest; use App\Http\Requests\SendRoomSwitchCommandRequest; +use App\Http\Requests\ReceiveSongIncrementRequest; use App\Services\TcpSocketClient; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; +use App\Models\Song; use App\Models\Branch; use App\Models\Room; use App\Models\MachineStatus; @@ -303,4 +305,55 @@ class RoomControlController extends Controller $room->save(); return $validated['command']==='error' ? ApiResponse::error('機房控制失敗') : ApiResponse::success($room); } + /** + * @OA\Post( + * path="/api/room/incrementSongCount", + * summary="歌曲點歌", + * description="歌曲點歌 依據 song_id 對歌曲加1", + * operationId="incrementSong", + * tags={"Room Control"}, + * security={{"Authorization":{}}}, + * @OA\RequestBody( + * required=true, + * @OA\JsonContent(ref="#/components/schemas/ReceiveSongIncrementRequest") + * ), + * @OA\Response( + * response=200, + * description="成功遞增 song_counts 並回傳歌曲資訊", + * @OA\JsonContent( + * allOf={ + * @OA\Schema(ref="#/components/schemas/ApiResponse"), + * @OA\Schema( + * @OA\Property(property="data", ref="#/components/schemas/SongCounts") + * ) + * } + * ) + * ), + * @OA\Response( + * response=404, + * description="找不到歌曲", + * @OA\JsonContent( + * @OA\Property(property="code", type="string", example="NOT_FOUND"), + * @OA\Property(property="message", type="string", example="歌曲不存在"), + * @OA\Property(property="data", type="null") + * ) + * ) + * ) + */ + public function incrementSongCount(ReceiveSongIncrementRequest $request): JsonResponse + { + $request->validate([ + 'song_id' => 'required|integer|exists:songs,id', + ]); + + $song = Song::find($request->input('song_id')); + + if (!$song) { + return ApiResponse::error('歌曲不存在', 404); + } + + $song->increment('song_counts'); + + return ApiResponse::success($song->only(['id', 'name', 'song_counts'])); + } } diff --git a/app/Http/Requests/ReceiveSongIncrementRequest.php b/app/Http/Requests/ReceiveSongIncrementRequest.php new file mode 100644 index 0000000..2f552c8 --- /dev/null +++ b/app/Http/Requests/ReceiveSongIncrementRequest.php @@ -0,0 +1,27 @@ +|string> + */ + public function rules(): array + { + return [ + 'song_id' => ['required','integer','exists:songs,id'], + ]; + } +} diff --git a/app/Models/Song.php b/app/Models/Song.php index 69f7d63..eccbcf6 100644 --- a/app/Models/Song.php +++ b/app/Models/Song.php @@ -9,6 +9,15 @@ use App\Helpers\ChineseNameConverter; use App\Helpers\ChineseStrokesConverter; use App\Traits\LogsModelActivity; +/** + * @OA\Schema( + * schema="SongCounts", + * type="object", + * @OA\Property(property="id", type="integer", example=16), + * @OA\Property(property="name", type="string", example="愛"), + * @OA\Property(property="song_counts", type="integer", example="1"), + * ) + */ class Song extends Model { /** @use HasFactory<\Database\Factories\SongFactory> */ diff --git a/routes/api.php b/routes/api.php index 2201fba..92690fd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -17,4 +17,5 @@ Route::middleware('auth:sanctum')->group(function () { Route::post('/room/sendSwitch', [RoomControlController::class, 'sendSwitch']); Route::post('/room/receiveSwitch', [RoomControlController::class, 'receiveSwitch']); Route::post('/room/heartbeat', [RoomControlController::class, 'StatusReport']); + Route::post('/room/incrementSongCount', [RoomControlController::class, 'incrementSongCount']); }); \ No newline at end of file