feat: add POST /fruits endpoint to create new fruits
This commit is contained in:
parent
5f4f8d44be
commit
7421c50569
34
alembic/versions/20250430_094514_6958f2e3_update_fruit.py
Normal file
34
alembic/versions/20250430_094514_6958f2e3_update_fruit.py
Normal file
@ -0,0 +1,34 @@
|
||||
"""create table for fruits
|
||||
|
||||
Revision ID: 9f2b6a4e5c28
|
||||
Revises: 2d7c4f9e8a1c
|
||||
Create Date: 2023-05-24 12:00:00
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
import uuid
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '9f2b6a4e5c28'
|
||||
down_revision = '2d7c4f9e8a1c'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
table_name = "fruits"
|
||||
|
||||
op.create_table(
|
||||
table_name,
|
||||
sa.Column('id', sa.String(36), primary_key=True, default=lambda: str(uuid.uuid4())),
|
||||
sa.Column('name', sa.String(), nullable=False, unique=True),
|
||||
sa.Column('color', sa.String(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), server_default=func.now()),
|
||||
sa.Column('updated_at', sa.DateTime(), server_default=func.now(), onupdate=func.now()),
|
||||
sa.Index('ix_fruits_name', 'name')
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
table_name = "fruits"
|
||||
op.drop_table(table_name)
|
@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
from schemas.fruit import FruitSchema, FruitCreate
|
||||
from helpers.fruit_helpers import create_fruit
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/fruits", status_code=status.HTTP_201_CREATED, response_model=FruitSchema)
|
||||
async def create_new_fruit(
|
||||
fruit_data: FruitCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
new_fruit = create_fruit(db=db, fruit_data=fruit_data)
|
||||
return new_fruit
|
55
helpers/fruit_helpers.py
Normal file
55
helpers/fruit_helpers.py
Normal file
@ -0,0 +1,55 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from models.fruit import Fruit
|
||||
from schemas.fruit import FruitCreate
|
||||
from uuid import UUID
|
||||
|
||||
def create_fruit(db: Session, fruit_data: FruitCreate) -> Fruit:
|
||||
"""
|
||||
Creates a new fruit in the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
fruit_data (FruitCreate): The data for the fruit to create.
|
||||
|
||||
Returns:
|
||||
Fruit: The newly created fruit object.
|
||||
|
||||
Raises:
|
||||
IntegrityError: If a fruit with the same name already exists.
|
||||
"""
|
||||
try:
|
||||
db_fruit = Fruit(**fruit_data.dict())
|
||||
db.add(db_fruit)
|
||||
db.commit()
|
||||
db.refresh(db_fruit)
|
||||
return db_fruit
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
raise ValueError(f"A fruit with the name '{fruit_data.name}' already exists.")
|
||||
|
||||
def get_fruit_by_id(db: Session, fruit_id: UUID) -> Optional[Fruit]:
|
||||
"""
|
||||
Retrieves a single fruit by its ID.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
fruit_id (UUID): The ID of the fruit to retrieve.
|
||||
|
||||
Returns:
|
||||
Optional[Fruit]: The fruit object if found, otherwise None.
|
||||
"""
|
||||
return db.query(Fruit).filter(Fruit.id == fruit_id).first()
|
||||
|
||||
def get_all_fruits(db: Session) -> list[Fruit]:
|
||||
"""
|
||||
Retrieves all fruits from the database.
|
||||
|
||||
Args:
|
||||
db (Session): The database session.
|
||||
|
||||
Returns:
|
||||
list[Fruit]: A list of all fruit objects.
|
||||
"""
|
||||
return db.query(Fruit).all()
|
14
models/fruit.py
Normal file
14
models/fruit.py
Normal file
@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, String, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.sql import func
|
||||
from core.database import Base
|
||||
import uuid
|
||||
|
||||
class Fruit(Base):
|
||||
__tablename__ = "fruits"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name = Column(String, nullable=False, unique=True, index=True)
|
||||
color = Column(String, nullable=False)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
23
schemas/fruit.py
Normal file
23
schemas/fruit.py
Normal file
@ -0,0 +1,23 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
class FruitBase(BaseModel):
|
||||
name: str = Field(..., description="The name of the fruit")
|
||||
color: str = Field(..., description="The color of the fruit")
|
||||
|
||||
class FruitCreate(FruitBase):
|
||||
pass
|
||||
|
||||
class FruitUpdate(FruitBase):
|
||||
name: Optional[str] = Field(None, description="The updated name of the fruit")
|
||||
color: Optional[str] = Field(None, description="The updated color of the fruit")
|
||||
|
||||
class FruitSchema(FruitBase):
|
||||
id: UUID
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
Loading…
x
Reference in New Issue
Block a user