KTV/app/Http/Controllers/BranchControlController.php
2025-06-04 15:30:51 +08:00

89 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use App\Models\Branch;
use App\Enums\RoomType;
use App\Enums\RoomStatus;
use App\Http\Responses\ApiResponse;
/**
* @OA\Tag(
* name="Branch",
* description="分店包廂"
* )
*/
class BranchControlController extends Controller
{
/**
* @OA\Get(
* path="/api/branches",
* tags={"Branch"},
* summary="取得分店資訊",
* description="需帶入 Bearer Token 取得當前用戶可存取的分店資訊",
* operationId="Branches",
* security={{"Authorization":{}}},
* @OA\Response(
* response=200,
* description="成功回傳分店列表",
* @OA\JsonContent(
* @OA\Property(property="branches", type="array", @OA\Items(
* @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="branch_name", type="string", example="測試"),
* @OA\Property(property="enable", type="boolean", example=true),
* @OA\Property(
* property="rooms",
* type="object",
* additionalProperties=@OA\Schema(
* @OA\Property(property="is_online", type="boolean", example=true),
* @OA\Property(property="status", ref="#/components/schemas/RoomStatus"),
* @OA\Property(property="started_at", type="string", format="date-time", example="2025-06-04 10:00:00Z"),
* @OA\Property(property="ended_at", type="string", format="date-time", example="2025-06-04 12:00:00Z")
* )
* )
* ))
* )
* ),
* @OA\Response(
* response=401,
* description="Unauthorized",
* @OA\JsonContent(
* allOf={
* @OA\Schema(ref="#/components/schemas/ApiResponse"),
* @OA\Schema(
* @OA\Property(property="code", type="string", example="UNAUTHORIZED"),
* @OA\Property(property="message", type="string", example="Unauthorized"),
* @OA\Property(property="data", type="null")
* )
* }
* )
* ),
* )
*/
public function Branches(Request $request): JsonResponse
{
$branches = Branch::with('rooms')->get()->map(function ($branch) {
return [
'id' => $branch->id,
'branch_name' => $branch->name,
'enable' => $branch->enable,
'rooms' => $branch->rooms
->filter(fn($room) => $room->type === RoomType::PC)
->mapWithKeys(function ($room) {
$roomName = ($room->type?->value ?? '') . $room->name;
return [
$roomName => [
'is_online' => $room->is_online,
'status' => $room->status?->value ?? null,
'started_at' => $room->started_at,
'ended_at' => $room->ended_at,
]
];
}),
];
});
return ApiResponse::success(['branches' => $branches]);
}
}