File size: 1,271 Bytes
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""E1, E2: drill set logs."""

from datetime import date
from typing import Annotated

from fastapi import APIRouter, Header, Query

from app.common.schemas import Items
from app.core.dependencies import CurrentAccount, Db
from app.modules.drills.schemas import DrillLogEnvelope, DrillLogIn, DrillLogOut
from app.modules.drills.service import DrillLogService
from app.modules.idempotency.service import run_idempotent

router = APIRouter(prefix="/v1", tags=["drills"])


@router.post("/drill-logs", status_code=201, response_model=DrillLogEnvelope)
async def create_drill_log(
    body: DrillLogIn, account: CurrentAccount, db: Db,
    idempotency_key: Annotated[str | None, Header(max_length=128)] = None,
):
    service = DrillLogService(db)

    async def handler() -> DrillLogEnvelope:
        return DrillLogEnvelope(log=await service.create(account, body))

    return await run_idempotent(db, account.id, idempotency_key, body, handler, status_code=201)


@router.get("/drill-logs", response_model=Items[DrillLogOut])
async def list_drill_logs(
    account: CurrentAccount, db: Db,
    start: Annotated[date, Query(alias="from")],
    end: Annotated[date, Query(alias="to")],
) -> Items[DrillLogOut]:
    return await DrillLogService(db).list(account, start, end)