2021-12-25 18:59:36 +01:00
|
|
|
from fastapi import APIRouter, status, Path, Depends, HTTPException
|
|
|
|
from sqlmodel import select, Session
|
|
|
|
|
|
|
|
from src import database
|
2021-12-25 19:59:51 +01:00
|
|
|
from src.models.models import Record, RecordRead, RecordCreate
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/records", tags=["Records"])
|
|
|
|
|
|
|
|
|
2021-12-22 09:06:08 +01:00
|
|
|
@router.get("/", response_model=list[RecordRead], summary="Get a list of all records")
|
2021-12-25 18:59:36 +01:00
|
|
|
async def all_records(
|
|
|
|
*,
|
|
|
|
session: Session = Depends(database.get_session)
|
2021-12-25 19:24:43 +01:00
|
|
|
) -> list[Record]:
|
2021-12-21 21:00:58 +01:00
|
|
|
"""Returns a list of all records."""
|
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
records = session.exec(select(Record)).all()
|
|
|
|
return records
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
2021-12-25 19:24:43 +01:00
|
|
|
@router.post("/", status_code=status.HTTP_201_CREATED)
|
2021-12-25 18:59:36 +01:00
|
|
|
async def add_record(
|
|
|
|
*,
|
|
|
|
record: RecordCreate,
|
|
|
|
session: Session = Depends(database.get_session)
|
|
|
|
):
|
|
|
|
"""Start a record."""
|
2021-12-21 21:00:58 +01:00
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
db_record = Record.from_orm(record)
|
|
|
|
session.add(db_record)
|
|
|
|
session.commit()
|
|
|
|
session.refresh(db_record)
|
|
|
|
return db_record
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
@router.get("/{id}", response_model=RecordRead)
|
|
|
|
async def get_record(
|
|
|
|
*,
|
|
|
|
id: str = Path(..., title="ID of the record"),
|
|
|
|
session: Session = Depends(database.get_session)
|
|
|
|
) -> Record:
|
2021-12-21 21:00:58 +01:00
|
|
|
"""Fetch a record by id."""
|
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
record = session.get(Record, id)
|
|
|
|
if not record:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
return record
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
|
|
async def delete_record(
|
|
|
|
*,
|
|
|
|
id: str = Path(..., title="ID of the record"),
|
|
|
|
session: Session = Depends(database.get_session)
|
|
|
|
):
|
|
|
|
"""Delete a record specified by id."""
|
2021-12-21 21:00:58 +01:00
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
record = session.get(Record, id)
|
|
|
|
if not record:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
session.delete(record)
|
|
|
|
session.commit()
|
|
|
|
# TODO: This method implicitly returns None, which will be converted to null by FastAPI, which triggers a
|
|
|
|
# warning, because the method returns with header HTTP_204_NO_CONTENT, but has content
|
2021-12-21 21:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{id}", summary="Apply partial updates to a record by id")
|
2021-12-25 18:59:36 +01:00
|
|
|
async def patch_record(
|
|
|
|
*,
|
|
|
|
record: RecordCreate,
|
|
|
|
id: str = Path(..., title="ID of the record"),
|
|
|
|
session: Session = Depends(database.get_session)
|
|
|
|
):
|
2021-12-21 21:00:58 +01:00
|
|
|
"""Apply partial updates to a record."""
|
|
|
|
|
2021-12-25 18:59:36 +01:00
|
|
|
# Fetch record by id from database
|
|
|
|
db_record = session.get(Record, id)
|
|
|
|
if not db_record:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
# Set all provided arguments in the database record
|
|
|
|
for key, value in record.dict(exclude_unset=True).items():
|
|
|
|
setattr(db_record, key, value)
|
|
|
|
|
|
|
|
# Write modified record to database
|
|
|
|
session.add(db_record)
|
|
|
|
session.commit()
|
|
|
|
session.refresh(db_record)
|
|
|
|
return db_record
|