Projects get searched by id instead of by name

This commit is contained in:
linuskmr 2021-12-25 18:32:16 +01:00
parent e70436104b
commit 8b79f7fca4

View file

@ -37,54 +37,49 @@ async def add_project(
return db_project
@router.get("/{name}", response_model=ProjectRead, summary="Get a project by name")
@router.get("/{id}", response_model=ProjectRead)
async def get_project(
*,
name: str = Path(..., title="Name of the module"),
id: int = Path(..., title="ID of the project"),
session: Session = Depends(database.get_session)
) -> Project:
"""Fetch a project by name."""
try:
project = session.exec(select(Project).where(Project.name == name)).one()
return project
except sqlalchemy.exc.NoResultFound:
project = session.get(Project, id)
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return project
@router.delete("/{name}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete a project by name")
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project(
*,
name: str = Path(..., title="Name of the module"),
id: int = Path(..., title="ID of the project"),
session: Session = Depends(database.get_session)
):
"""Delete a module specified by name."""
project = session.exec(select(Project).where(Project.name == name)).one()
project = session.get(Project, id)
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
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}",
status_code=status.HTTP_200_OK,
response_model=ProjectRead,
summary="Apply partial updates to a project by name"
)
@router.patch("/{id}", status_code=status.HTTP_200_OK,response_model=ProjectRead)
async def patch_project(
*,
project: ProjectCreate,
name: str = Path(..., title="Name of the module"),
id: int = Path(..., title="ID of the project"),
session: Session = Depends(database.get_session)
) -> Project:
"""Apply partial updates to a project."""
# Search for project with name in database
try:
db_project = session.exec(select(Project).where(Project.name == name)).one()
except sqlalchemy.exc.NoResultFound:
db_project = session.get(Project, id)
if not db_project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# Set all provided arguments in the database project