Tags get searched by id instead of by name

This commit is contained in:
linuskmr 2021-12-25 18:35:15 +01:00
parent 15ef4c03d1
commit 6bcdda506a

View file

@ -22,7 +22,7 @@ async def all_tags(
return tags return tags
@router.post("/", status_code=status.HTTP_201_CREATED, summary="Add a tag") @router.post("/", status_code=status.HTTP_201_CREATED)
async def add_tag( async def add_tag(
*, *,
tag: TagCreate, tag: TagCreate,
@ -37,49 +37,49 @@ async def add_tag(
return db_tag return db_tag
@router.get("/{name}", response_model=TagRead, summary="Get a tag by name") @router.get("/{name}", response_model=TagRead)
async def get_tag( async def get_tag(
*, *,
name: str = Path(..., title="Name of the tag"), id: str = Path(..., title="ID of the tag"),
session: Session = Depends(database.get_session) session: Session = Depends(database.get_session)
) -> Tag: ) -> Tag:
"""Fetch a tag by name.""" """Fetch a tag by name."""
try: tag = session.get(Tag, id)
tag = session.exec(select(Tag).where(Tag.name == name)).one() if not tag:
return tag
except sqlalchemy.exc.NoResultFound:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return tag
@router.delete("/{name}", status_code=status.HTTP_204_NO_CONTENT, summary="Delete a tag by name") @router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_tag( async def delete_tag(
*, *,
name: str = Path(..., title="Name of the tag"), id: str = Path(..., title="ID of the tag"),
session: Session = Depends(database.get_session) session: Session = Depends(database.get_session)
): ):
"""Delete a tag specified by name.""" """Delete a tag specified by name."""
tag = session.exec(select(Tag).where(Tag.name == name)).one() tag = session.get(Tag, id)
if not tag:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
session.delete(tag) session.delete(tag)
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("/{name}", summary="Apply partial updates to a tag by name") @router.patch("/{id}")
async def patch_tag( async def patch_tag(
*, *,
tag: TagCreate, tag: TagCreate,
name: str = Path(..., title="Name of the tag"), id: str = Path(..., title="ID of the tag"),
session: Session = Depends(database.get_session) session: Session = Depends(database.get_session)
): ):
"""Apply partial updates to a tag.""" """Apply partial updates to a tag."""
# Search for tag with name in database # Fetch tag with id from database
try: db_tag = session.get(Tag, id)
db_tag = session.exec(select(Tag).where(Tag.name == name)).one() if not db_tag:
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