Fetch real data from database
This commit is contained in:
parent
5c717d4ee9
commit
23aa9a905b
1 changed files with 68 additions and 38 deletions
|
@ -1,66 +1,96 @@
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from fastapi import APIRouter, status, Path
|
import sqlalchemy.exc
|
||||||
from src.models.project import ProjectCreate, ProjectRead
|
from fastapi import APIRouter, status, Path, Depends, HTTPException
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
|
||||||
|
from src import database
|
||||||
|
from src.models.project import ProjectCreate, ProjectRead, Project
|
||||||
|
|
||||||
router = APIRouter(prefix='/projects', tags=["Projects"])
|
router = APIRouter(prefix='/projects', tags=["Projects"])
|
||||||
|
|
||||||
|
|
||||||
@router.get("/", response_model=list[ProjectRead], summary="Get a list of all projects")
|
@router.get("/", response_model=list[ProjectRead], summary="Get a list of all projects")
|
||||||
async def all_projects() -> list[ProjectRead]:
|
async def all_projects(
|
||||||
|
*,
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
) -> list[Project]:
|
||||||
"""Returns a list of all projects"""
|
"""Returns a list of all projects"""
|
||||||
|
|
||||||
return [
|
projects = session.exec(select(Project)).all()
|
||||||
ProjectRead(
|
return projects
|
||||||
name="Learning",
|
|
||||||
start_date=datetime.now(),
|
|
||||||
duration=timedelta(days=3, hours=5, minutes=47),
|
|
||||||
records=42
|
|
||||||
),
|
|
||||||
ProjectRead(
|
|
||||||
name="Sports",
|
|
||||||
start_date=datetime.now(),
|
|
||||||
duration=timedelta(hours=14, minutes=10),
|
|
||||||
records=10
|
|
||||||
),
|
|
||||||
ProjectRead(
|
|
||||||
name="Gaming",
|
|
||||||
start_date=datetime.now(),
|
|
||||||
duration=timedelta(hours=3, minutes=2),
|
|
||||||
records=5
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", status_code=status.HTTP_201_CREATED, summary="Add a project")
|
@router.post("/", status_code=status.HTTP_201_CREATED, summary="Add a project", response_model=ProjectRead)
|
||||||
async def add_project(project: ProjectCreate):
|
async def add_project(
|
||||||
|
*,
|
||||||
|
project: ProjectCreate,
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
):
|
||||||
"""Add a project."""
|
"""Add a project."""
|
||||||
|
|
||||||
print('adding project', project)
|
db_project = Project.from_orm(project)
|
||||||
|
session.add(db_project)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(db_project)
|
||||||
|
return db_project
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{name}", response_model=ProjectRead, summary="Get a project by name")
|
@router.get("/{name}", response_model=ProjectRead, summary="Get a project by name")
|
||||||
async def get_project(name: str = Path(..., title="Name of the module")) -> ProjectRead:
|
async def get_project(
|
||||||
|
*,
|
||||||
|
name: str = Path(..., title="Name of the module"),
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
) -> Project:
|
||||||
"""Fetch a project by name."""
|
"""Fetch a project by name."""
|
||||||
|
|
||||||
return ProjectRead(
|
try:
|
||||||
name=name,
|
project = session.exec(select(Project).where(Project.name == name)).one()
|
||||||
start_date=datetime.now(),
|
return project
|
||||||
duration=timedelta(hours=3, minutes=2),
|
except sqlalchemy.exc.NoResultFound:
|
||||||
records=5
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{name}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete a project by name")
|
@router.delete("/{name}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete a project by name")
|
||||||
async def delete_project(name: str = Path(..., title="Name of the module")):
|
async def delete_project(
|
||||||
|
*,
|
||||||
|
name: str = Path(..., title="Name of the module"),
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
):
|
||||||
"""Delete a module specified by name."""
|
"""Delete a module specified by name."""
|
||||||
|
|
||||||
logging.debug(f"Deleting module {name}")
|
project = session.exec(select(Project).where(Project.name == name)).one()
|
||||||
|
session.delete(project)
|
||||||
|
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("/{name}", summary="Apply partial updates to a project by name")
|
@router.patch(
|
||||||
async def patch_project(project: ProjectCreate, name: str = Path(..., title="Name of the module")):
|
"/{name}",
|
||||||
|
status_code=status.HTTP_200_OK,
|
||||||
|
response_model=ProjectRead,
|
||||||
|
summary="Apply partial updates to a project by name"
|
||||||
|
)
|
||||||
|
async def patch_project(
|
||||||
|
*,
|
||||||
|
project: ProjectCreate,
|
||||||
|
name: str = Path(..., title="Name of the module"),
|
||||||
|
session: Session = Depends(database.get_session)
|
||||||
|
) -> Project:
|
||||||
"""Apply partial updates to a project."""
|
"""Apply partial updates to a project."""
|
||||||
|
|
||||||
logging.debug(f"Patching project {name} with {project}")
|
try:
|
||||||
|
db_project = session.exec(select(Project).where(Project.name == name)).one()
|
||||||
|
except sqlalchemy.exc.NoResultFound:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
# Set all provided arguments in the database project
|
||||||
|
for key, value in project.dict(exclude_unset=True).items():
|
||||||
|
setattr(db_project, key, value)
|
||||||
|
|
||||||
|
session.add(db_project)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(db_project)
|
||||||
|
return db_project
|
||||||
|
|
Loading…
Reference in a new issue