Skip to content

Commit

Permalink
fix: add log data to single routine get API
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa committed Oct 2, 2024
1 parent 7b07629 commit 944e2b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
10 changes: 6 additions & 4 deletions app/api/controllers/routines.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from contextlib import contextmanager
from datetime import datetime
from fastapi import APIRouter, Depends, status
from pytz import timezone

from app.core.auth import get_current_user

Expand Down Expand Up @@ -50,11 +52,11 @@ def create_routine(
operation_id="getRoutine",
)
def get_routine(routine_id: int, dao: RoutineDAO = Depends(get_routine_dao)):
routine = dao.get_routine_with_elements_by_id(routine_id)
routine = dao.get_routine_with_elements_by_id(
routine_id, datetime.now(timezone("Asia/Seoul")).date()
)

response = RoutinePublic.from_routine(routine, routine.routine_elements)

return response
return routine


@router.delete(
Expand Down
42 changes: 39 additions & 3 deletions app/api/dao/routine_dao.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import date
from typing import List
from fastapi import HTTPException, status
from sqlalchemy import asc, func
from app.api.errors import DATA_DOES_NOT_EXIST
from app.schemas.routine import RoutineItem, RoutinePublic
from .base import ProtectedBaseDAO
from app.models.models import Routine
from app.models.models import Routine, RoutineLog


class RoutineDAO(ProtectedBaseDAO):
Expand Down Expand Up @@ -34,7 +37,9 @@ def get_routines_by_weekday(self, weekday: int) -> List[Routine]:

return routines

def get_routine_with_elements_by_id(self, routine_id: int) -> Routine:
def get_routine_with_elements_by_id(
self, routine_id: int, date: date
) -> RoutinePublic:
routine = (
self.db.query(Routine)
.filter(
Expand All @@ -50,7 +55,38 @@ def get_routine_with_elements_by_id(self, routine_id: int) -> Routine:
detail=DATA_DOES_NOT_EXIST,
)

return routine
routine_logs = (
self.db.query(RoutineLog)
.filter(
RoutineLog.routine_id == routine_id,
func.date(RoutineLog.completed_at) == date,
)
.order_by(
asc(RoutineLog.routine_element_id),
asc(RoutineLog.completed_at),
)
.all()
)

routine_with_logs: RoutinePublic = RoutinePublic.from_routine_with_log(
routine=routine, routine_items=[]
)
routine_log_map = dict()
for log in routine_logs:
routine_log_map[log.routine_element_id] = log

for element in routine.routine_elements:
routine_item = RoutineItem.from_routine_element(element, None)

if routine_log_map.get(routine_item.id) is not None:
log = routine_log_map[element.id]
routine_item.completed_at = log.completed_at
routine_item.completed_duration_seconds = log.duration_seconds
routine_item.is_skipped = log.is_skipped

routine_with_logs.routine_elements.append(routine_item)

return routine_with_logs

def update_routine(
self,
Expand Down

0 comments on commit 944e2b3

Please sign in to comment.