53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.models.material import Material
|
|
from app.schemas.material import MaterialResponse, MaterialProperties
|
|
|
|
logger = logging.getLogger("app.routers.materials")
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/materials", response_model=list[MaterialResponse])
|
|
async def get_materials(db: AsyncSession = Depends(get_db)):
|
|
logger.info("GET /api/materials")
|
|
|
|
result = await db.execute(select(Material).where(Material.is_active == True).order_by(Material.id))
|
|
materials = result.scalars().all()
|
|
logger.info("Found %d active materials", len(materials))
|
|
|
|
response = [
|
|
MaterialResponse(
|
|
id=m.id,
|
|
name=m.name,
|
|
category=m.category,
|
|
price_per_gram=m.price_per_gram,
|
|
density_g_cm3=m.density_g_cm3,
|
|
flow_rate_mm3_s=m.flow_rate_mm3_s,
|
|
properties=MaterialProperties(
|
|
max_temp_c=m.max_temp_c,
|
|
min_temp_c=m.min_temp_c,
|
|
strength=m.strength,
|
|
flexibility=m.flexibility,
|
|
chemical_resistance=m.chemical_resistance,
|
|
uv_resistance=m.uv_resistance,
|
|
food_safe=m.food_safe,
|
|
),
|
|
description=m.description,
|
|
color_options=m.color_options or [],
|
|
)
|
|
for m in materials
|
|
]
|
|
|
|
for m in materials:
|
|
logger.debug(" Material: id=%d, name=%s, category=%s, price=%.1f RUB/g",
|
|
m.id, m.name, m.category, m.price_per_gram)
|
|
|
|
logger.info("Returning %d materials", len(response))
|
|
return response
|