refacto: rename app into backend, to prepare for frontend dev
This commit is contained in:
parent
017092040d
commit
e605bf8603
32 changed files with 2 additions and 2 deletions
3
server/backend/adapters/http/__init__.py
Normal file
3
server/backend/adapters/http/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from .readings import readings_router
|
||||
|
||||
__all__ = ["readings_router"]
|
||||
28
server/backend/adapters/http/_readings_schemas.py
Normal file
28
server/backend/adapters/http/_readings_schemas.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from datetime import datetime
|
||||
from pydantic import BaseModel
|
||||
|
||||
from domain.entities import ConsumptionResponse
|
||||
|
||||
class ConsumptionPointSchema(BaseModel):
|
||||
period: datetime
|
||||
pulse_count_start: int
|
||||
pulse_count_end: int
|
||||
delta_pulses: int
|
||||
delta_m3: float
|
||||
|
||||
class ConsumptionResponseSchema(BaseModel):
|
||||
dev_eui: str
|
||||
start: datetime
|
||||
end: datetime
|
||||
granularity: str
|
||||
points: list[ConsumptionPointSchema]
|
||||
|
||||
@classmethod
|
||||
def from_domain(cls, r: ConsumptionResponse) -> "ConsumptionResponseSchema":
|
||||
return cls(
|
||||
dev_eui=r.dev_eui,
|
||||
start=r.start,
|
||||
end=r.end,
|
||||
granularity=r.granularity,
|
||||
points=[ConsumptionPointSchema(**p.__dict__) for p in r.points],
|
||||
)
|
||||
18
server/backend/adapters/http/main.py
Normal file
18
server/backend/adapters/http/main.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from .readings import readings_router
|
||||
|
||||
app = FastAPI(title="SimuGazAPI", version="1.0.0")
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["GET"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(readings_router)
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
30
server/backend/adapters/http/readings.py
Normal file
30
server/backend/adapters/http/readings.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, HTTPException
|
||||
|
||||
from domain.value_objects import Granularity
|
||||
from domain.exceptions import ValidationError, DatabaseError
|
||||
from services.consumption_service import ConsumptionService
|
||||
from dependencies import get_consumption_service
|
||||
|
||||
from ._readings_schemas import ConsumptionResponseSchema
|
||||
|
||||
readings_router = APIRouter(prefix="/readings", tags=["readings"])
|
||||
|
||||
|
||||
@readings_router.get("/{dev_eui}", response_model=ConsumptionResponseSchema)
|
||||
def get_consumption(
|
||||
dev_eui: str,
|
||||
start: Annotated[datetime, Query()],
|
||||
end: Annotated[datetime, Query()],
|
||||
granularity: Annotated[Granularity, Query()] = "day",
|
||||
service: ConsumptionService = Depends(get_consumption_service),
|
||||
):
|
||||
try:
|
||||
result = service.get_consumption(dev_eui, start, end, granularity)
|
||||
return ConsumptionResponseSchema.from_domain(result)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
except DatabaseError as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
Loading…
Add table
Add a link
Reference in a new issue