148 lines
4.2 KiB
Python
148 lines
4.2 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import and_
|
|
from uuid import UUID
|
|
import random
|
|
import string
|
|
|
|
from models.car import Car
|
|
from schemas.car import CarCreate
|
|
|
|
def generate_german_cars(db: Session, count: int = 10) -> List[Car]:
|
|
"""
|
|
Generates and stores a list of German cars in the database.
|
|
|
|
Args:
|
|
db (Session): The database session
|
|
count (int): Number of cars to generate (default: 10)
|
|
|
|
Returns:
|
|
List[Car]: List of generated car objects
|
|
"""
|
|
german_brands = ['BMW', 'Mercedes-Benz', 'Audi', 'Porsche', 'Volkswagen']
|
|
models = {
|
|
'BMW': ['M3', 'M5', 'X5', '7 Series', 'i8'],
|
|
'Mercedes-Benz': ['C-Class', 'E-Class', 'S-Class', 'GLE', 'AMG GT'],
|
|
'Audi': ['A4', 'A6', 'Q7', 'RS6', 'e-tron'],
|
|
'Porsche': ['911', 'Cayenne', 'Panamera', 'Taycan', 'Macan'],
|
|
'Volkswagen': ['Golf', 'Passat', 'Tiguan', 'Touareg', 'ID.4']
|
|
}
|
|
colors = ['Black', 'White', 'Silver', 'Blue', 'Red', 'Grey']
|
|
|
|
generated_cars = []
|
|
for _ in range(count):
|
|
brand = random.choice(german_brands)
|
|
model = random.choice(models[brand])
|
|
vin = ''.join(random.choices(string.ascii_uppercase + string.digits, k=17))
|
|
|
|
car_data = CarCreate(
|
|
brand=brand,
|
|
model=model,
|
|
year=random.randint(2020, 2024),
|
|
color=random.choice(colors),
|
|
vin=vin,
|
|
price=random.randint(35000, 150000),
|
|
is_available=True,
|
|
mileage=random.randint(0, 5000)
|
|
)
|
|
|
|
db_car = Car(**car_data.dict())
|
|
db.add(db_car)
|
|
generated_cars.append(db_car)
|
|
|
|
db.commit()
|
|
return generated_cars
|
|
|
|
def get_available_german_cars(db: Session, brand: Optional[str] = None) -> List[Car]:
|
|
"""
|
|
Retrieves available German cars, optionally filtered by brand.
|
|
|
|
Args:
|
|
db (Session): The database session
|
|
brand (Optional[str]): Specific German brand to filter by
|
|
|
|
Returns:
|
|
List[Car]: List of available German cars
|
|
"""
|
|
german_brands = ['BMW', 'Mercedes-Benz', 'Audi', 'Porsche', 'Volkswagen']
|
|
query = db.query(Car).filter(Car.is_available)
|
|
|
|
if brand:
|
|
if brand not in german_brands:
|
|
raise ValueError("Invalid German car brand")
|
|
query = query.filter(Car.brand == brand)
|
|
else:
|
|
query = query.filter(Car.brand.in_(german_brands))
|
|
|
|
return query.all()
|
|
|
|
def update_car_price_and_availability(
|
|
db: Session,
|
|
car_id: UUID,
|
|
new_price: Optional[int] = None,
|
|
is_available: Optional[bool] = None
|
|
) -> Optional[Car]:
|
|
"""
|
|
Updates a car's price and availability status.
|
|
|
|
Args:
|
|
db (Session): The database session
|
|
car_id (UUID): The ID of the car to update
|
|
new_price (Optional[int]): New price for the car
|
|
is_available (Optional[bool]): New availability status
|
|
|
|
Returns:
|
|
Optional[Car]: Updated car object if found, None otherwise
|
|
"""
|
|
car = db.query(Car).filter(Car.id == car_id).first()
|
|
if not car:
|
|
return None
|
|
|
|
if new_price is not None:
|
|
car.price = new_price
|
|
if is_available is not None:
|
|
car.is_available = is_available
|
|
|
|
db.commit()
|
|
db.refresh(car)
|
|
return car
|
|
|
|
def get_cars_by_price_range(
|
|
db: Session,
|
|
min_price: int,
|
|
max_price: int,
|
|
brand: Optional[str] = None
|
|
) -> List[Car]:
|
|
"""
|
|
Retrieves cars within a specified price range, optionally filtered by brand.
|
|
|
|
Args:
|
|
db (Session): The database session
|
|
min_price (int): Minimum price
|
|
max_price (int): Maximum price
|
|
brand (Optional[str]): Specific brand to filter by
|
|
|
|
Returns:
|
|
List[Car]: List of cars matching the criteria
|
|
"""
|
|
query = db.query(Car).filter(
|
|
and_(
|
|
Car.price >= min_price,
|
|
Car.price <= max_price
|
|
)
|
|
)
|
|
|
|
if brand:
|
|
query = query.filter(Car.brand == brand)
|
|
|
|
return query.all()
|
|
|
|
def generate_vin() -> str:
|
|
"""
|
|
Generates a random VIN number.
|
|
|
|
Returns:
|
|
str: Generated VIN number
|
|
"""
|
|
# VIN format: 17 characters (letters and numbers)
|
|
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=17)) |