39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
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() |