Stations
This commit is contained in:
parent
10dd1f76cb
commit
a38d30071e
10
.idea/.gitignore
vendored
Normal file
10
.idea/.gitignore
vendored
Normal file
@ -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
|
||||
10
.idea/WetterstationServer.iml
Normal file
10
.idea/WetterstationServer.iml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.14 (WetterstationServer)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
7
.idea/misc.xml
Normal file
7
.idea/misc.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.14 (WetterstationServer)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.14 (WetterstationServer)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/WetterstationServer.iml" filepath="$PROJECT_DIR$/.idea/WetterstationServer.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
9
db.py
Normal file
9
db.py
Normal file
@ -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
|
||||
35
main.py
Normal file
35
main.py
Normal file
@ -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}"}
|
||||
32
models/station.py
Normal file
32
models/station.py
Normal file
@ -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
|
||||
39
services/stationService.py
Normal file
39
services/stationService.py
Normal file
@ -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()
|
||||
11
test_main.http
Normal file
11
test_main.http
Normal file
@ -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
|
||||
|
||||
###
|
||||
Loading…
Reference in New Issue
Block a user