feat: Updated endpoint endpoints/cars.post.py via AI with auto lint fixes

This commit is contained in:
Backend IM Bot 2025-04-14 23:09:08 +00:00
parent 852fc15dc6
commit 03186c1021
5 changed files with 45 additions and 7 deletions

View File

@ -0,0 +1,28 @@
"""make year field optional for cars table
Revision ID: f4c2e3a1c7b4
Revises: 2f8c9a7d5b42
Create Date: 2023-05-19 16:35:55.849331
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'f4c2e3a1c7b4'
down_revision = '2f8c9a7d5b42'
branch_labels = None
depends_on = None
def upgrade():
op.alter_column('cars', 'year',
existing_type=sa.Integer(),
nullable=True)
def downgrade():
op.alter_column('cars', 'year',
existing_type=sa.Integer(),
nullable=False)

View File

@ -1,8 +1,8 @@
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from core.database import get_db 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, get_car_by_name
router = APIRouter() router = APIRouter()
@ -11,5 +11,15 @@ 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, car_data) new_car = create_car(db=db, car_data=car_data)
return new_car return new_car
@router.get("/cars/{name}", status_code=200, response_model=CarSchema)
async def get_car(
name: str,
db: Session = Depends(get_db)
):
car = get_car_by_name(db=db, name=name)
if not car:
raise HTTPException(status_code=404, detail="Car not found")
return car

View File

@ -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(name=car_data.name, year=car_data.year) # Added 'year' field db_car = Car(name=car_data.name, year=car_data.year)
db.add(db_car) db.add(db_car)
db.commit() db.commit()
db.refresh(db_car) db.refresh(db_car)

View File

@ -9,6 +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 year = Column(Integer, nullable=True) # Made 'year' field optional
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())

View File

@ -5,7 +5,7 @@ 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") year: Optional[int] = Field(None, description="Year of the car")
class CarCreate(CarBase): class CarCreate(CarBase):
pass pass