feat: Updated endpoint endpoints/cars.post.py via AI with auto lint fixes
This commit is contained in:
parent
27c1b13c58
commit
852fc15dc6
21
alembic/versions/20250414_212405_0ac9306b_update_car.py
Normal file
21
alembic/versions/20250414_212405_0ac9306b_update_car.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
"""add year column to cars table
|
||||||
|
Revision ID: 2f8c9a7d5b42
|
||||||
|
Revises: 2f6a3c5d1e88
|
||||||
|
Create Date: 2023-05-30 15:21:37.844438
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2f8c9a7d5b42'
|
||||||
|
down_revision = '2f6a3c5d1e88'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_name = "cars"
|
||||||
|
op.add_column(table_name, sa.Column('year', sa.Integer(), nullable=False))
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
table_name = "cars"
|
||||||
|
op.drop_column(table_name, 'year')
|
@ -1,16 +1,15 @@
|
|||||||
from fastapi import APIRouter, status
|
from fastapi import APIRouter, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
from schemas.car import CarCreate, CarSchema
|
from schemas.car import CarCreate, CarSchema
|
||||||
from helpers.car_helpers import create_car
|
from helpers.car_helpers import create_car
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from fastapi import Depends
|
|
||||||
from core.database import get_db
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/cars", status_code=status.HTTP_201_CREATED, response_model=CarSchema)
|
@router.post("/cars", status_code=201, response_model=CarSchema)
|
||||||
async def create_new_car(
|
async def create_new_car(
|
||||||
car_data: CarCreate,
|
car_data: CarCreate,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
new_car = create_car(db=db, car_data=car_data)
|
new_car = create_car(db, car_data)
|
||||||
return new_car
|
return new_car
|
@ -16,7 +16,7 @@ def create_car(db: Session, car_data: CarCreate) -> Car:
|
|||||||
Car: The newly created car object.
|
Car: The newly created car object.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
db_car = Car(**car_data.dict())
|
db_car = Car(name=car_data.name, year=car_data.year) # Added 'year' field
|
||||||
db.add(db_car)
|
db.add(db_car)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_car)
|
db.refresh(db_car)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from sqlalchemy import Column, String
|
from sqlalchemy import Column, String, Integer
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from core.database import Base
|
from core.database import Base
|
||||||
@ -9,5 +9,6 @@ class Car(Base):
|
|||||||
|
|
||||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
name = Column(String, nullable=False, unique=True, index=True)
|
name = Column(String, nullable=False, unique=True, index=True)
|
||||||
|
year = Column(Integer, nullable=False) # Added 'year' field
|
||||||
created_at = Column(func.now())
|
created_at = Column(func.now())
|
||||||
updated_at = Column(func.now(), onupdate=func.now())
|
updated_at = Column(func.now(), onupdate=func.now())
|
@ -5,12 +5,14 @@ import uuid
|
|||||||
|
|
||||||
class CarBase(BaseModel):
|
class CarBase(BaseModel):
|
||||||
name: str = Field(..., description="Name of the car")
|
name: str = Field(..., description="Name of the car")
|
||||||
|
year: int = Field(..., description="Year of the car")
|
||||||
|
|
||||||
class CarCreate(CarBase):
|
class CarCreate(CarBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class CarUpdate(CarBase):
|
class CarUpdate(CarBase):
|
||||||
name: Optional[str] = Field(None, description="Name of the car")
|
name: Optional[str] = Field(None, description="Name of the car")
|
||||||
|
year: Optional[int] = Field(None, description="Year of the car")
|
||||||
|
|
||||||
class CarSchema(CarBase):
|
class CarSchema(CarBase):
|
||||||
id: uuid.UUID
|
id: uuid.UUID
|
||||||
|
Loading…
x
Reference in New Issue
Block a user