- Habits and the account now accumulate XP and levels from check streaks, with streak/level-up celebration banners and push notifications. - The "yesterday missed" banner on /today now lets a habit be explicitly marked failed (not just checked done), backed by a new HabitLog.status column so completion-rate/streak calculations never count a failed day as done. - The habit management tabs scroll horizontally on narrow screens instead of wrapping to two lines, with a pure-CSS edge shadow indicating more content.
53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
from datetime import date
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import get_db
|
|
from app.models.user import User
|
|
from app.schemas.habit_log import HabitLogOut, MonthlySummaryDay, TodayItem, WeeklyMatrixRow
|
|
from app.security import require_login
|
|
from app.services import habit_service, log_service
|
|
|
|
router = APIRouter(prefix="/api", tags=["logs"], dependencies=[Depends(require_login)])
|
|
|
|
|
|
@router.get("/today", response_model=list[TodayItem])
|
|
def get_today(db: Session = Depends(get_db), current_user: User = Depends(require_login)):
|
|
build_items, quit_items = log_service.get_today_items(db, current_user.id, date.today())
|
|
return build_items + quit_items
|
|
|
|
|
|
@router.post("/logs/{habit_id}/toggle")
|
|
def toggle_log(habit_id: int, db: Session = Depends(get_db), current_user: User = Depends(require_login)):
|
|
habit = habit_service.get_habit(db, habit_id, current_user.id)
|
|
if habit is None:
|
|
raise HTTPException(status_code=404, detail="습관을 찾을 수 없습니다")
|
|
checked, milestone_streak, level_up = log_service.toggle_check_and_celebrate(db, habit, date.today())
|
|
return {"checked": checked, "milestone_streak": milestone_streak, "level_up": level_up}
|
|
|
|
|
|
@router.get("/logs", response_model=list[HabitLogOut])
|
|
def list_logs(
|
|
habit_id: int | None = None,
|
|
start: date | None = None,
|
|
end: date | None = None,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_login),
|
|
):
|
|
return log_service.list_logs(db, current_user.id, habit_id=habit_id, start=start, end=end)
|
|
|
|
|
|
@router.get("/logs/summary/monthly", response_model=list[MonthlySummaryDay])
|
|
def monthly_summary(
|
|
year: int, month: int, db: Session = Depends(get_db), current_user: User = Depends(require_login)
|
|
):
|
|
return log_service.get_monthly_summary(db, current_user.id, year, month)
|
|
|
|
|
|
@router.get("/logs/summary/weekly", response_model=list[WeeklyMatrixRow])
|
|
def weekly_summary(
|
|
start_date: date, db: Session = Depends(get_db), current_user: User = Depends(require_login)
|
|
):
|
|
return log_service.get_weekly_matrix(db, current_user.id, start_date)
|