diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/WetterstationServer.iml b/.idea/WetterstationServer.iml new file mode 100644 index 0000000..90eab65 --- /dev/null +++ b/.idea/WetterstationServer.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0465759 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..219eca6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/db.py b/db.py new file mode 100644 index 0000000..1f51d17 --- /dev/null +++ b/db.py @@ -0,0 +1,9 @@ +from sqlalchemy import create_engine +from sqlmodel import SQLModel, Session + +engine = create_engine("sqlite:///database.db") +SQLModel.metadata.create_all(engine) + +def get_session(): + with Session(engine) as session: + yield session \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0a620bd --- /dev/null +++ b/main.py @@ -0,0 +1,35 @@ +import functools + +from fastapi import FastAPI, Depends +from sqlmodel import Session + +from db import get_session +from models.station import StationCreateRequest, StationCreateResponse, Station, StationListResponse, \ + StationUpdateResponse, StationUpdateRequest +from services import stationService + +app = FastAPI() + + +@app.get("/stations/list") +async def list_stations(session: Session = Depends(get_session)): + result = stationService.list_stations(session) + return [StationListResponse.model_validate(station) for station in result] + +@app.post("/stations/create", response_model=StationCreateResponse, status_code=200) +async def create_station(station_data: StationCreateRequest, session: Session = Depends(get_session)) -> StationCreateResponse: + station = stationService.create_station(session, station_data) + return StationCreateResponse.model_validate(station) + +@app.patch("/stations/update", response_model=StationUpdateResponse, status_code=200) +async def update_station(station_data: StationUpdateRequest, session: Session = Depends(get_session)): + station = stationService.update_station(session, station_data) + return StationUpdateResponse.model_validate(station) + +@app.delete("/stations/{station_id}", status_code=204) +async def delete_station(station_id: int, session: Session = Depends(get_session)): + stationService.delete_station(session, station_id) + +@app.get("/hello/{name}") +async def say_hello(name: str): + return {"message": f"Hello {name}"} diff --git a/models/station.py b/models/station.py new file mode 100644 index 0000000..0a0e9b4 --- /dev/null +++ b/models/station.py @@ -0,0 +1,32 @@ +from datetime import datetime +from typing import Optional + +from sqlmodel import SQLModel, Field + + +class Station(SQLModel, table=True): + id: int = Field(default=None, primary_key=True) + mac: str = Field(unique=True) + name: Optional[str] = Field(default=None) + created_at: datetime = Field(default_factory=datetime.now) + +class StationCreateRequest(SQLModel): + name: str + mac: str + +class StationCreateResponse(SQLModel): + id: int + name: str + +class StationListResponse(SQLModel): + id: int + name: str + created_at: datetime + +class StationUpdateRequest(SQLModel): + id: int + name: str + +class StationUpdateResponse(SQLModel): + id: int + name: str \ No newline at end of file diff --git a/services/stationService.py b/services/stationService.py new file mode 100644 index 0000000..f02bdcf --- /dev/null +++ b/services/stationService.py @@ -0,0 +1,39 @@ +from typing import Sequence + +from fastapi import HTTPException +from sqlmodel import Session, select + +from models.station import StationCreateRequest, Station, StationUpdateRequest + + +def list_stations(session: Session) -> Sequence[Station]: + statement = select(Station) + result = session.exec(statement).all() + return result + +def create_station(session: Session, station_data: StationCreateRequest) -> Station: + station = Station.model_validate(station_data) + session.add(station) + session.commit() + session.refresh(station) + return station + + +def update_station(session: Session, station_data: StationUpdateRequest) -> Station: + station = session.get(Station, station_data.id) + if not station: + raise HTTPException(status_code=404, detail="Station not found") + + station.name = station_data.name + session.commit() + session.refresh(station) + return Station.model_validate(station) + + +def delete_station(session: Session, station_id: int): + station = session.get(Station, station_id) + if not station: + raise HTTPException(status_code=404, detail="Station not found") + + session.delete(station) + session.commit() \ No newline at end of file diff --git a/test_main.http b/test_main.http new file mode 100644 index 0000000..a2d81a9 --- /dev/null +++ b/test_main.http @@ -0,0 +1,11 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/ +Accept: application/json + +### + +GET http://127.0.0.1:8000/hello/User +Accept: application/json + +###