feat: add delete fruit endpoint
This commit is contained in:
parent
72a6253d9a
commit
3deec14a3d
19
alembic/versions/20250430_194451_e491efa2_update_fruit.py
Normal file
19
alembic/versions/20250430_194451_e491efa2_update_fruit.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
"""add delete_by_id helper method for Fruit
|
||||||
|
Revision ID: 3c5a6f92d7d3
|
||||||
|
Revises: 9f2b6a4e5c28
|
||||||
|
Create Date: 2023-05-30 12:42:26.524518
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '3c5a6f92d7d3'
|
||||||
|
down_revision = '9f2b6a4e5c28'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
pass
|
@ -0,0 +1,17 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException, status
|
||||||
|
from schemas.fruit import FruitSchema
|
||||||
|
from helpers.fruit_helpers import delete_fruit, get_fruit_by_id
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from fastapi import Depends
|
||||||
|
from core.database import get_db
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.delete("/fruits/{fruit_id}", status_code=status.HTTP_200_OK, response_model=FruitSchema)
|
||||||
|
async def delete_fruit_endpoint(fruit_id: uuid.UUID, db: Session = Depends(get_db)):
|
||||||
|
fruit = get_fruit_by_id(db, fruit_id)
|
||||||
|
if not fruit:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Fruit not found")
|
||||||
|
delete_fruit(db, fruit_id)
|
||||||
|
return fruit
|
@ -52,4 +52,20 @@ def get_all_fruits(db: Session) -> list[Fruit]:
|
|||||||
Returns:
|
Returns:
|
||||||
list[Fruit]: A list of all fruit objects.
|
list[Fruit]: A list of all fruit objects.
|
||||||
"""
|
"""
|
||||||
return db.query(Fruit).all()
|
return db.query(Fruit).all()
|
||||||
|
|
||||||
|
def delete_fruit(db: Session, fruit_id: UUID) -> None:
|
||||||
|
"""
|
||||||
|
Deletes a fruit from the database by its ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db (Session): The database session.
|
||||||
|
fruit_id (UUID): The ID of the fruit to delete.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
"""
|
||||||
|
fruit = db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
||||||
|
if fruit:
|
||||||
|
db.delete(fruit)
|
||||||
|
db.commit()
|
@ -11,4 +11,11 @@ class Fruit(Base):
|
|||||||
name = Column(String, nullable=False, unique=True, index=True)
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
color = Column(String, nullable=False)
|
color = Column(String, nullable=False)
|
||||||
created_at = Column(DateTime, default=func.now())
|
created_at = Column(DateTime, default=func.now())
|
||||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def delete_by_id(cls, db, fruit_id):
|
||||||
|
fruit = db.query(cls).filter(cls.id == fruit_id).first()
|
||||||
|
if fruit:
|
||||||
|
db.delete(fruit)
|
||||||
|
db.commit()
|
Loading…
x
Reference in New Issue
Block a user