add crud for forms, templates, shipment, users and auth with keycloak
This commit is contained in:
@@ -1,41 +1,51 @@
|
||||
from sqlmodel import Session, select
|
||||
from src.models import Product
|
||||
import src.models as models
|
||||
|
||||
def get_all(session: Session) -> list[Product]:
|
||||
statement = select(Product)
|
||||
def get_all(session: Session) -> list[models.ProductPublic]:
|
||||
statement = select(models.Product)
|
||||
return session.exec(statement).all()
|
||||
|
||||
def get_one(session: Session, product_id: int) -> Product:
|
||||
return session.get(Product, product_id)
|
||||
def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
return session.get(models.Product, product_id)
|
||||
|
||||
def create_one(session: Session, product: Product) -> Product:
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = Product(**product_create)
|
||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
||||
shipments = session.exec(select(models.Shipment).where(models.Shipment.id.in_(product.shipment_ids))).all()
|
||||
|
||||
product_create = product.model_dump(exclude_unset=True, exclude={'shipment_ids'})
|
||||
new_product = models.Product(**product_create, shipments=shipments)
|
||||
session.add(new_product)
|
||||
session.commit()
|
||||
session.refresh(new_product)
|
||||
return new_product
|
||||
|
||||
def update_one(session: Session, id: int, product: Product) -> Product:
|
||||
statement = select(Product).where(Product.id == id)
|
||||
def update_one(session: Session, id: int, product: models.ProductUpdate) -> models.ProductPublic:
|
||||
statement = select(models.Product).where(models.Product.id == id)
|
||||
result = session.exec(statement)
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
return None
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
|
||||
shipments_to_add = session.exec(select(models.Shipment).where(models.Shipment.id.in_(product.shipment_ids))).all()
|
||||
new_product.shipments.clear()
|
||||
for add in shipments_to_add:
|
||||
new_product.shipments.append(add)
|
||||
|
||||
product_updates = product.model_dump(exclude_unset=True, exclude={"shipment_ids"})
|
||||
for key, value in product_updates.items():
|
||||
setattr(new_product, key, value)
|
||||
|
||||
session.add(new_product)
|
||||
session.commit()
|
||||
session.refresh(new_product)
|
||||
return new_product
|
||||
|
||||
def delete_one(session: Session, id: int) -> Product:
|
||||
statement = select(Product).where(Product.id == id)
|
||||
def delete_one(session: Session, id: int) -> models.ProductPublic:
|
||||
statement = select(models.Product).where(models.Product.id == id)
|
||||
result = session.exec(statement)
|
||||
product = result.first()
|
||||
if not product:
|
||||
return None
|
||||
result = models.ProductPublic.model_validate(product)
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
return product
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user