feat: add endpoint to delete books from the database
This commit is contained in:
parent
456f3bb8a7
commit
7c9d752ac2
23
alembic/versions/20250429_184448_d4adbaca_update_book.py
Normal file
23
alembic/versions/20250429_184448_d4adbaca_update_book.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
"""Add a delete_book helper function to handle book deletion logic.
|
||||||
|
Revision ID: 7d1f3e5a6c2b
|
||||||
|
Revises: 4a8c3b7d9e2f
|
||||||
|
Create Date: 2023-10-12 14:30:00.000000
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '7d1f3e5a6c2b'
|
||||||
|
down_revision = '4a8c3b7d9e2f'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# This migration only adds a helper method to the model class
|
||||||
|
# No database schema changes are needed
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# No database schema changes to revert
|
||||||
|
pass
|
@ -0,0 +1,26 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from core.database import get_db
|
||||||
|
from helpers.book_helpers import delete_book
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.delete("/books", status_code=status.HTTP_200_OK)
|
||||||
|
async def delete_book_endpoint(
|
||||||
|
book_id: UUID,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Delete a book from the database by its ID
|
||||||
|
"""
|
||||||
|
success = delete_book(db=db, book_id=book_id)
|
||||||
|
|
||||||
|
if not success:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail="Book not found"
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"message": "Book successfully deleted"}
|
@ -46,6 +46,25 @@ def update_book(db: Session, book_id: UUID, book_data: BookUpdate) -> Optional[B
|
|||||||
db.refresh(book)
|
db.refresh(book)
|
||||||
return book
|
return book
|
||||||
|
|
||||||
|
def delete_book(db: Session, book_id: UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Deletes a book from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
book_id (UUID): The ID of the book to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the book was successfully deleted, False if the book was not found.
|
||||||
|
"""
|
||||||
|
book = db.query(Book).filter(Book.id == book_id).first()
|
||||||
|
if not book:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db.delete(book)
|
||||||
|
db.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
def get_book_by_id(db: Session, book_id: UUID) -> Optional[Book]:
|
def get_book_by_id(db: Session, book_id: UUID) -> Optional[Book]:
|
||||||
"""
|
"""
|
||||||
Retrieves a book by its ID.
|
Retrieves a book by its ID.
|
||||||
|
@ -44,3 +44,23 @@ class Book(Base):
|
|||||||
db_session.commit()
|
db_session.commit()
|
||||||
db_session.refresh(book)
|
db_session.refresh(book)
|
||||||
return book
|
return book
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def delete_book(cls, db_session, book_id: uuid.UUID) -> bool:
|
||||||
|
"""
|
||||||
|
Delete a book record from the database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db_session: The database session
|
||||||
|
book_id: UUID of the book to delete
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if book was successfully deleted, False if book not found
|
||||||
|
"""
|
||||||
|
book = db_session.query(cls).filter(cls.id == book_id).first()
|
||||||
|
if not book:
|
||||||
|
return False
|
||||||
|
|
||||||
|
db_session.delete(book)
|
||||||
|
db_session.commit()
|
||||||
|
return True
|
Loading…
x
Reference in New Issue
Block a user