/
killogram
/
E-Analyzer
Обзор
Документация
Войти
/
killogram
/
E-Analyzer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
api/customers.py
37 строк
2 KB
Korabliov Kirill
Refactor add purchase feachure
01 июн 2025, 23:54
01 июн 2025, 23:54
9d93c35
Код
Авторство
О чём код?
from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List from api.crud import get_all_customers, create_customer, link_purchase_customer, get_customers, get_customer_by_id from api.schemas import Customer, CustomerCreate, PurchaseCustomerCreate, PurchaseCustomer from api.database import get_db router = APIRouter() @router.get("/{customer_id}", response_model=Customer) def get_customer_endpoint(customer_id: int, db: Session = Depends(get_db)): customer = get_customer_by_id(db, customer_id) if not customer: raise HTTPException(status_code=404, detail="Заказчик не найден") return customer @router.get("/{purchase_id}/customers", response_model=List[Customer]) # Изменён путь для уникальности def get_customers_endpoint(purchase_id: int, db: Session = Depends(get_db)): customers = get_customers(db, purchase_id) if not customers: raise HTTPException(status_code=404, detail="Заказчики не найдены") # Убедимся, что возвращаемые данные соответствуют схеме Customer return [Customer(id=customer.id, name=customer.name, extra_data=customer.extra_data) for customer in customers] @router.post("/create/", response_model=Customer) def create_customer_endpoint(customer: CustomerCreate, db: Session = Depends(get_db)): db_customer = create_customer(db, customer) if not db_customer: raise HTTPException(status_code=400, detail="Не удалось создать заказчика") return db_customer @router.post("/link/", response_model=PurchaseCustomer) def link_purchase_customer_endpoint(purchase_customer: PurchaseCustomerCreate, db: Session = Depends(get_db)): db_link = link_purchase_customer(db, purchase_customer.purchase_id, purchase_customer.customer_name) if not db_link: raise HTTPException(status_code=400, detail="Не удалось связать заказчика с закупкой") return db_link