feat: Updated endpoint endpoints/cars.post.py via AI with auto lint fixes
This commit is contained in:
parent
852fc15dc6
commit
03186c1021
28
alembic/versions/20250414_230855_25677015_update_car.py
Normal file
28
alembic/versions/20250414_230855_25677015_update_car.py
Normal 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)
|
@ -1,8 +1,8 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from core.database import get_db
|
||||
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()
|
||||
|
||||
@ -11,5 +11,15 @@ async def create_new_car(
|
||||
car_data: CarCreate,
|
||||
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
|
||||
|
||||
@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
|
@ -16,7 +16,7 @@ def create_car(db: Session, car_data: CarCreate) -> Car:
|
||||
Car: The newly created car object.
|
||||
"""
|
||||
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.commit()
|
||||
db.refresh(db_car)
|
||||
|
@ -9,6 +9,6 @@ class Car(Base):
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
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())
|
||||
updated_at = Column(func.now(), onupdate=func.now())
|
@ -5,7 +5,7 @@ import uuid
|
||||
|
||||
class CarBase(BaseModel):
|
||||
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):
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user