Spaces:
Sleeping
Sleeping
File size: 1,313 Bytes
14c4d51 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from src.infrastructure.repositories.routine_repository import RoutineRepository
from src.domain.schemas.routine import RoutineCreate
from fastapi import HTTPException
class RoutineUseCase:
def __init__(self, repository: RoutineRepository):
self.repository = repository
def _format_routine(self, d: dict):
if "_id" in d:
d["id"] = str(d.pop("_id"))
return d
async def create_routine(self, routine_in: RoutineCreate):
data = routine_in.dict()
routine = await self.repository.create(data)
return self._format_routine(routine)
async def get_all_routines(self, assigned_to: str = None, date_str: str = None):
routines = await self.repository.get_all(assigned_to, date_str)
return [self._format_routine(r) for r in routines]
async def update_routine_status(self, routine_id: str, status: str):
valid_statuses = ["PENDING", "WAITING", "DONE", "MISSED", "ABANDONED"]
if status not in valid_statuses:
raise HTTPException(status_code=400, detail="Invalid status")
success = await self.repository.update_status(routine_id, status)
if not success:
raise HTTPException(status_code=404, detail="Routine not found")
return {"msg": "Status updated successfully"}
|