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 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( 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) session: Session = Depends(database.get_session)
) -> Project: ) -> Project:
"""Fetch a project by name.""" """Fetch a project by name."""
try: project = session.get(Project, id)
project = session.exec(select(Project).where(Project.name == name)).one() if not project:
return project
except sqlalchemy.exc.NoResultFound:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) 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( 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) session: Session = Depends(database.get_session)
): ):
"""Delete a module specified by name.""" """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.delete(project)
session.commit() session.commit()
# TODO: This method implicitly returns None, which will be converted to null by FastAPI, which triggers a # 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 # warning, because the method returns with header HTTP_204_NO_CONTENT, but has content
@router.patch( @router.patch("/{id}", status_code=status.HTTP_200_OK,response_model=ProjectRead)
"/{name}",
status_code=status.HTTP_200_OK,
response_model=ProjectRead,
summary="Apply partial updates to a project by name"
)
async def patch_project( async def patch_project(
*, *,
project: ProjectCreate, project: ProjectCreate,
name: str = Path(..., title="Name of the module"), id: int = Path(..., title="ID of the project"),
session: Session = Depends(database.get_session) session: Session = Depends(database.get_session)
) -> Project: ) -> Project:
"""Apply partial updates to a project.""" """Apply partial updates to a project."""
# Search for project with name in database # Search for project with name in database
try: db_project = session.get(Project, id)
db_project = session.exec(select(Project).where(Project.name == name)).one() if not db_project:
except sqlalchemy.exc.NoResultFound:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# Set all provided arguments in the database project # Set all provided arguments in the database project