summaryrefslogtreecommitdiff
path: root/apirouters/tasks.py
blob: 02e091b5fc990b1de795c086c66d71534590b76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from typing import Annotated
from fastapi import APIRouter, Depends
from pydantic import BaseModel

from ..modules import task_types
from .agents import auth_agent, Agent

router = APIRouter()


@router.get("/tasks")
async def get_tasks(agent: Annotated[Agent, Depends(auth_agent)]):
    ships = []
    for current_ship in agent.ships.values():
        ships.append(current_ship.get_data())

    return {"ships": ships}


@router.get("/task/{ship_symbol}")
async def get_tasks(ship_symbol: str, agent: Annotated[Agent, Depends(auth_agent)]):
    for current_ship in agent.ships.values():
        if current_ship.symbol == ship_symbol:
            return current_ship.get_data()

    return {"error": "Unknown ship symbol."}


class SetTaskBody(BaseModel):
    task: task_types.task_type


@router.post("/task/{ship_symbol}/set")
async def get_tasks(ship_symbol: str, set_task: SetTaskBody, agent: Annotated[Agent, Depends(auth_agent)]):
    for current_ship in agent.ships.values():
        if current_ship.symbol == ship_symbol:
            current_ship.set_task(set_task.task)
            return current_ship.get_data()

    return {"error": "Unknown ship symbol."}