117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
import uuid
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..schemas.auth import UserToken
|
|
import barker.schemas.master as schemas
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionLocal
|
|
from ..models.master import Customer
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("", response_model=schemas.Customer)
|
|
def save(
|
|
data: schemas.CustomerIn, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["customers"]),
|
|
):
|
|
try:
|
|
item = Customer(company=data.company, name=data.name, phone=data.phone, address=data.address)
|
|
db.add(item)
|
|
db.commit()
|
|
return customer_info(item)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.Customer)
|
|
def update(
|
|
id_: uuid.UUID,
|
|
data: schemas.CustomerIn,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["customers"]),
|
|
):
|
|
try:
|
|
item: Customer = db.query(Customer).filter(Customer.id == id_).first()
|
|
item.company = data.company
|
|
item.name = data.name
|
|
item.phone = data.phone
|
|
item.address = data.address
|
|
db.commit()
|
|
return customer_info(item)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.delete("/{id_}")
|
|
def delete(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["customers"]),
|
|
):
|
|
try:
|
|
item: Customer = db.query(Customer).filter(Customer.id == id_).first()
|
|
if item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Sale Category not found",
|
|
)
|
|
db.delete(item)
|
|
db.commit()
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.get("")
|
|
def show_blank(
|
|
db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["customers"]),
|
|
):
|
|
return customer_info(None)
|
|
|
|
|
|
@router.get("/list")
|
|
def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
|
return [customer_info(item) for item in db.query(Customer).order_by(Customer.name).all()]
|
|
|
|
|
|
@router.get("/{id_}")
|
|
def show_id(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["customers"]),
|
|
):
|
|
item: Customer = db.query(Customer).filter(Customer.id == id_).first()
|
|
return customer_info(item)
|
|
|
|
|
|
def customer_info(item: Optional[Customer]):
|
|
if item is None:
|
|
return {"name": "", "company": "", "address": "", "phone": ""}
|
|
return {
|
|
"id": item.id,
|
|
"company": item.company,
|
|
"name": item.name,
|
|
"address": item.address,
|
|
"phone": item.phone,
|
|
}
|