Implement record fetching from database
This commit is contained in:
parent
113d35201b
commit
96573e7d48
1 changed files with 66 additions and 41 deletions
|
@ -1,67 +1,92 @@
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from fastapi import APIRouter, status, Path
|
from fastapi import APIRouter, status, Path, Depends, HTTPException
|
||||||
from src.models.record import RecordStartCreate, RecordRead, RecordCreate
|
from sqlmodel import select, Session
|
||||||
|
|
||||||
|
from src import database
|
||||||
|
from src.models.record import Record, RecordRead, RecordCreate
|
||||||
|
|
||||||
router = APIRouter(prefix="/records", tags=["Records"])
|
router = APIRouter(prefix="/records", tags=["Records"])
|
||||||
|
|
||||||
|
|
||||||
@router.get("/", response_model=list[RecordRead], summary="Get a list of all records")
|
@router.get("/", response_model=list[RecordRead], summary="Get a list of all records")
|
||||||
async def all_tags() -> list[RecordRead]:
|
async def all_records(
|
||||||
|
*,
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
) -> list[RecordRead]:
|
||||||
"""Returns a list of all records."""
|
"""Returns a list of all records."""
|
||||||
|
|
||||||
return [
|
records = session.exec(select(Record)).all()
|
||||||
RecordRead(
|
return records
|
||||||
start=datetime.now() - timedelta(hours=3),
|
|
||||||
end=datetime.now(),
|
|
||||||
project="Uni",
|
|
||||||
tags=["Listening Lectures", "Not be concentrated"]
|
|
||||||
),
|
|
||||||
RecordRead(
|
|
||||||
start=datetime.now() - timedelta(hours=7),
|
|
||||||
end=datetime.now() - timedelta(hours=4),
|
|
||||||
project="Uni",
|
|
||||||
tags=["Listening Lectures", "Not be concentrated"]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/start", status_code=status.HTTP_201_CREATED, summary="Start a record")
|
@router.post("/start", status_code=status.HTTP_201_CREATED)
|
||||||
async def start_record(record: RecordStartCreate):
|
async def add_record(
|
||||||
|
*,
|
||||||
|
record: RecordCreate,
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
):
|
||||||
"""Start a record."""
|
"""Start a record."""
|
||||||
|
|
||||||
print('starting record', record)
|
db_record = Record.from_orm(record)
|
||||||
|
session.add(db_record)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(db_record)
|
||||||
|
return db_record
|
||||||
|
|
||||||
|
|
||||||
@router.post("/end", status_code=status.HTTP_204_NO_CONTENT, summary="End a record")
|
@router.get("/{id}", response_model=RecordRead)
|
||||||
async def end_record(record: RecordStartCreate):
|
async def get_record(
|
||||||
"""End a record."""
|
*,
|
||||||
|
id: str = Path(..., title="ID of the record"),
|
||||||
print('end record', record)
|
session: Session = Depends(database.get_session)
|
||||||
|
) -> Record:
|
||||||
|
|
||||||
@router.get("/{id}", response_model=RecordRead, summary="Get a record by id")
|
|
||||||
async def get_record(id: str = Path(..., title="ID of the record")) -> RecordRead:
|
|
||||||
"""Fetch a record by id."""
|
"""Fetch a record by id."""
|
||||||
|
|
||||||
return RecordRead(
|
record = session.get(Record, id)
|
||||||
start=datetime.now() - timedelta(hours=3),
|
if not record:
|
||||||
end=datetime.now(),
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
project="Uni",
|
return record
|
||||||
tags=["Listening Lectures", "Not be concentrated"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete a record by id")
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
async def delete_record(id: str = Path(..., title="ID of the record")):
|
async def delete_record(
|
||||||
"""Delete a tag specified by name."""
|
*,
|
||||||
|
id: str = Path(..., title="ID of the record"),
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
):
|
||||||
|
"""Delete a record specified by id."""
|
||||||
|
|
||||||
logging.debug(f"Deleting record {id}")
|
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
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{id}", summary="Apply partial updates to a record by id")
|
@router.patch("/{id}", summary="Apply partial updates to a record by id")
|
||||||
async def patch_record(record: RecordCreate, id: str = Path(..., title="ID of the record")):
|
async def patch_record(
|
||||||
|
*,
|
||||||
|
record: RecordCreate,
|
||||||
|
id: str = Path(..., title="ID of the record"),
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
):
|
||||||
"""Apply partial updates to a record."""
|
"""Apply partial updates to a record."""
|
||||||
|
|
||||||
logging.debug(f"Patching record {id} with {record}")
|
# 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
|
||||||
|
|
Loading…
Reference in a new issue