schema_service.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # app/services/schema_service.py
  2. from sqlalchemy.orm import Session
  3. from typing import Optional
  4. from app.core.biz_exception import NotFoundError, PermissionDeniedError, BizLogicError
  5. from app.models.schema import VasSchema
  6. from app.schemas.schema import VasSchemaCreate, VasSchemaUpdate
  7. class SchemaService:
  8. def create(db: Session, data: VasSchemaCreate):
  9. rec = VasSchema(**data.dict())
  10. db.add(rec)
  11. db.commit()
  12. db.refresh(rec)
  13. return rec
  14. def get(db: Session, id: int):
  15. obj = db.query(VasSchema).filter_by(id=id).first()
  16. if not obj:
  17. raise NotFoundError('Schema not exist')
  18. return obj
  19. def update(db: Session, id: int, data: VasSchemaUpdate):
  20. obj = db.query(VasSchema).filter_by(id=id).first()
  21. if not obj:
  22. raise NotFoundError('Schema not exist')
  23. for k,v in data.dict(exclude_unset=True).items():
  24. setattr(obj, k, v)
  25. db.commit()
  26. db.refresh(obj)
  27. return obj
  28. def delete(db: Session, id:int):
  29. obj = db.query(VasSchema).filter_by(id=id).first()
  30. if not obj:
  31. raise NotFoundError('Schema not exist')
  32. db.delete(obj)
  33. db.commit()
  34. def list_all(db: Session):
  35. return db.query(VasSchema).all()