You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB
Python

# backend/app/routers/life.py
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import Session
from ..db import get_session
from ..models import FinanceInsight, FinanceSub, HealthHabit
from ..schemas import (
FinanceOut,
HabitTickResponse,
HealthOut,
InsightActResponse,
KnowledgeAskRequest,
KnowledgeAskResponse,
KnowledgeOut,
KnowledgeSuggestActRequest,
KnowledgeSuggestActResponse,
LifeOverviewOut,
SubPauseRequest,
SubPauseResponse,
)
from ..services import life_insights, life_read
router = APIRouter() # 내부 prefix 없음. main.py 에서 /api 부착.
@router.get("/life/overview", response_model=LifeOverviewOut)
def life_overview(s: Session = Depends(get_session)):
return life_read.build_overview(s)
@router.get("/life/health", response_model=HealthOut)
def life_health(s: Session = Depends(get_session)):
return life_read.build_health(s)
@router.get("/life/finance", response_model=FinanceOut)
def life_finance(s: Session = Depends(get_session)):
return life_read.build_finance(s)
@router.get("/life/knowledge", response_model=KnowledgeOut)
def life_knowledge(s: Session = Depends(get_session), q: str = "", type: str = ""):
return life_read.build_knowledge(s, q=q, type_=type)
@router.post("/life/habits/{habit_id}/tick", response_model=HabitTickResponse)
def tick_habit(habit_id: str, s: Session = Depends(get_session)):
h = s.get(HealthHabit, habit_id)
if not h:
raise HTTPException(404, "habit not found")
if h.done < h.total:
h.done += 1
s.add(h)
s.commit()
s.refresh(h)
return HabitTickResponse(habit=life_read.habit_out(h))
@router.post("/life/subs/{sub_id}/pause", response_model=SubPauseResponse)
def pause_sub(sub_id: str, body: SubPauseRequest, s: Session = Depends(get_session)):
sub = s.get(FinanceSub, sub_id)
if not sub:
raise HTTPException(404, "sub not found")
sub.paused = body.paused
s.add(sub)
s.commit()
s.refresh(sub)
approval_id = None
if body.paused:
approval_id = life_insights.enqueue_sub_pause(s, sub)
return SubPauseResponse(sub=life_read.sub_out(sub), approval_id=approval_id)
@router.post("/life/insights/{insight_id}/act", response_model=InsightActResponse)
def act_insight(insight_id: str, s: Session = Depends(get_session)):
ins = s.get(FinanceInsight, insight_id)
if not ins:
raise HTTPException(404, "insight not found")
return life_insights.act(s, ins)
@router.post("/life/knowledge/ask", response_model=KnowledgeAskResponse)
def knowledge_ask(body: KnowledgeAskRequest, s: Session = Depends(get_session)):
return life_insights.knowledge_ask(s, body.q)
@router.post("/life/knowledge/suggest/act", response_model=KnowledgeSuggestActResponse)
def knowledge_suggest_act(body: KnowledgeSuggestActRequest, s: Session = Depends(get_session)):
return life_insights.act_suggestion(s, body.item_id, body.target)