diff --git a/backend/src/routes/tags.py b/backend/src/routes/tags.py index e7a945d..cf1b118 100644 --- a/backend/src/routes/tags.py +++ b/backend/src/routes/tags.py @@ -22,7 +22,7 @@ async def all_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( *, tag: TagCreate, @@ -37,49 +37,49 @@ async def add_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( *, - name: str = Path(..., title="Name of the tag"), + id: str = Path(..., title="ID of the tag"), session: Session = Depends(database.get_session) ) -> Tag: """Fetch a tag by name.""" - try: - tag = session.exec(select(Tag).where(Tag.name == name)).one() - return tag - except sqlalchemy.exc.NoResultFound: + tag = session.get(Tag, id) + if not tag: 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( *, - name: str = Path(..., title="Name of the tag"), + id: str = Path(..., title="ID of the tag"), session: Session = Depends(database.get_session) ): """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.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 tag by name") +@router.patch("/{id}") async def patch_tag( *, tag: TagCreate, - name: str = Path(..., title="Name of the tag"), + id: str = Path(..., title="ID of the tag"), session: Session = Depends(database.get_session) ): """Apply partial updates to a tag.""" - # Search for tag with name in database - try: - db_tag = session.exec(select(Tag).where(Tag.name == name)).one() - except sqlalchemy.exc.NoResultFound: + # Fetch tag with id from database + db_tag = session.get(Tag, id) + if not db_tag: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) # Set all provided arguments in the database project