jarvis-backend / src /usecases /routine_usecase.py
AdeshRathod
initial commit
14c4d51
Raw History Blame Contribute Delete
1.31 kB
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"}