add contract storage fix various bugs and translations
This commit is contained in:
65
backend/src/contracts/service.py
Normal file
65
backend/src/contracts/service.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
forms: list[str]
|
||||
) -> list[models.ContractPublic]:
|
||||
statement = select(models.Contract)
|
||||
if len(forms) > 0:
|
||||
statement = statement.join(models.Form).where(models.Form.name.in_(forms))
|
||||
return session.exec(statement.order_by(models.Contract.id)).all()
|
||||
|
||||
def get_one(session: Session, contract_id: int) -> models.ContractPublic:
|
||||
return session.get(models.Contract, contract_id)
|
||||
|
||||
def create_one(session: Session, contract: models.ContractCreate) -> models.ContractPublic:
|
||||
contract_create = contract.model_dump(exclude_unset=True, exclude=["products", "cheques"])
|
||||
new_contract = models.Contract(**contract_create)
|
||||
|
||||
new_contract.cheques = [
|
||||
models.Cheque(
|
||||
name=cheque.name,
|
||||
value=cheque.value,
|
||||
contract_id=new_contract.id
|
||||
) for cheque in contract.cheques
|
||||
]
|
||||
|
||||
new_contract.products = [
|
||||
models.ContractProduct(
|
||||
contract_id=new_contract.id,
|
||||
product_id=contract_product.product_id,
|
||||
shipment_id=contract_product.shipment_id,
|
||||
quantity=contract_product.quantity
|
||||
) for contract_product in contract.products
|
||||
]
|
||||
|
||||
session.add(new_contract)
|
||||
session.commit()
|
||||
session.refresh(new_contract)
|
||||
return new_contract
|
||||
|
||||
def update_one(session: Session, id: int, contract: models.ContractUpdate) -> models.ContractPublic:
|
||||
statement = select(models.Contract).where(models.Contract.id == id)
|
||||
result = session.exec(statement)
|
||||
new_contract = result.first()
|
||||
if not new_contract:
|
||||
return None
|
||||
contract_updates = contract.model_dump(exclude_unset=True)
|
||||
for key, value in contract_updates.items():
|
||||
setattr(new_contract, key, value)
|
||||
session.add(new_contract)
|
||||
session.commit()
|
||||
session.refresh(new_contract)
|
||||
return new_contract
|
||||
|
||||
def delete_one(session: Session, id: int) -> models.ContractPublic:
|
||||
statement = select(models.Contract).where(models.Contract.id == id)
|
||||
result = session.exec(statement)
|
||||
contract = result.first()
|
||||
if not contract:
|
||||
return None
|
||||
result = models.ContractPublic.model_validate(contract)
|
||||
session.delete(contract)
|
||||
session.commit()
|
||||
return result
|
||||
Reference in New Issue
Block a user