from fastapi import APIRouter, Depends, HTTPException, status from app.api.deps import get_server_service, get_server_repo from app.repositories.server import ServerRepository from app.schemas.server import ServerRead, ServerCreate, ServerUpdate from app.services.server import ServerService router = APIRouter(prefix="/servers", tags=["Servers"]) @router.get("", response_model=list[ServerRead]) async def list_servers( server_svc: ServerService = Depends(get_server_service), ): return await server_svc.get_active() @router.get("/all", response_model=list[ServerRead]) async def list_all_servers( server_repo: ServerRepository = Depends(get_server_repo), ): return await server_repo.get_all() @router.post("", response_model=ServerRead, status_code=status.HTTP_201_CREATED) async def create_server( body: ServerCreate, server_svc: ServerService = Depends(get_server_service), ): try: return await server_svc.create( name=body.name, host=body.host, port=body.port, protocol=body.protocol, location=body.location, country_code=body.country_code, max_users=body.max_users, ) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) @router.get("/{server_id}", response_model=ServerRead) async def get_server( server_id: int, server_svc: ServerService = Depends(get_server_service), ): server = await server_svc.get_by_id(server_id) if server is None: raise HTTPException(status_code=404, detail="Server not found") return server @router.patch("/{server_id}", response_model=ServerRead) async def update_server( server_id: int, body: ServerUpdate, server_svc: ServerService = Depends(get_server_service), ): server = await server_svc.update( server_id, **body.model_dump(exclude_unset=True) ) if server is None: raise HTTPException(status_code=404, detail="Server not found") return server