http_session_service.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from sqlalchemy.orm import Session
  2. from app.core.biz_exception import NotFoundError, PermissionDeniedError, BizLogicError
  3. from app.models.http_session import HttpSession
  4. from app.schemas.http_session import HttpSessionCreate, HttpSessionUpdate
  5. from typing import Optional
  6. class HttpSessionService:
  7. @staticmethod
  8. def create(db: Session, data: HttpSessionCreate) -> HttpSession:
  9. obj = HttpSession(**data.dict())
  10. db.add(obj)
  11. db.commit()
  12. db.refresh(obj)
  13. return obj
  14. @staticmethod
  15. def get_by_sid(db: Session, session_id: str) -> Optional[HttpSession]:
  16. obj = db.query(HttpSession).filter(HttpSession.session_id == session_id).first()
  17. if not obj:
  18. raise NotFoundError("Session not found")
  19. return obj
  20. @staticmethod
  21. def delete_by_sid(db: Session, session_id: str) -> bool:
  22. obj = db.query(HttpSession).filter(HttpSession.session_id == session_id).first()
  23. if not obj:
  24. raise NotFoundError("Session not found")
  25. db.delete(obj)
  26. db.commit()
  27. return obj
  28. @staticmethod
  29. def update_by_sid(db: Session, session_id: str, data: HttpSessionUpdate):
  30. obj = db.query(HttpSession).filter(HttpSession.session_id == session_id).first()
  31. if not obj:
  32. raise NotFoundError("Session not found")
  33. for k, v in data.dict().items():
  34. if v is not None:
  35. setattr(obj, k, v)
  36. db.commit()
  37. db.refresh(obj)
  38. return obj