62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
from typing import Any, Dict, List, Optional, Union
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.warehouse import Warehouse
|
|
from app.schemas.warehouse import WarehouseCreate, WarehouseUpdate
|
|
|
|
|
|
def get(db: Session, warehouse_id: int) -> Optional[Warehouse]:
|
|
return db.query(Warehouse).filter(Warehouse.id == warehouse_id).first()
|
|
|
|
|
|
def get_by_code(db: Session, code: str) -> Optional[Warehouse]:
|
|
return db.query(Warehouse).filter(Warehouse.code == code).first()
|
|
|
|
|
|
def get_multi(
|
|
db: Session, *, skip: int = 0, limit: int = 100, active_only: bool = False
|
|
) -> List[Warehouse]:
|
|
query = db.query(Warehouse)
|
|
if active_only:
|
|
query = query.filter(Warehouse.is_active == True)
|
|
return query.offset(skip).limit(limit).all()
|
|
|
|
|
|
def create(db: Session, *, obj_in: WarehouseCreate) -> Warehouse:
|
|
db_obj = Warehouse(
|
|
name=obj_in.name,
|
|
code=obj_in.code,
|
|
address=obj_in.address,
|
|
city=obj_in.city,
|
|
state=obj_in.state,
|
|
country=obj_in.country,
|
|
postal_code=obj_in.postal_code,
|
|
is_active=obj_in.is_active,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def update(
|
|
db: Session, *, db_obj: Warehouse, obj_in: Union[WarehouseUpdate, Dict[str, Any]]
|
|
) -> Warehouse:
|
|
if isinstance(obj_in, dict):
|
|
update_data = obj_in
|
|
else:
|
|
update_data = obj_in.dict(exclude_unset=True)
|
|
for field in update_data:
|
|
setattr(db_obj, field, update_data[field])
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def remove(db: Session, *, warehouse_id: int) -> Warehouse:
|
|
obj = db.query(Warehouse).get(warehouse_id)
|
|
db.delete(obj)
|
|
db.commit()
|
|
return obj |