Spaces:
Running
Running
Merge pull request #3 from yuvabe-ai-labs/feat/home-screen
Browse files- src/home/router.py +16 -11
- src/home/schemas.py +8 -4
- src/home/service.py +3 -6
src/home/router.py
CHANGED
|
@@ -3,7 +3,7 @@ from sqlmodel import Session
|
|
| 3 |
|
| 4 |
from src.core.database import engine
|
| 5 |
|
| 6 |
-
from .schemas import
|
| 7 |
from .service import add_or_update_emotion, get_emotions, get_home_data
|
| 8 |
|
| 9 |
router = APIRouter(prefix="/home", tags=["Home"])
|
|
@@ -14,26 +14,31 @@ def get_session():
|
|
| 14 |
yield session
|
| 15 |
|
| 16 |
|
| 17 |
-
@router.get("/{user_id}", response_model=
|
| 18 |
def fetch_home_data(user_id: str, session: Session = Depends(get_session)):
|
| 19 |
try:
|
| 20 |
-
|
|
|
|
| 21 |
except ValueError as e:
|
| 22 |
raise HTTPException(status_code=404, detail=str(e))
|
| 23 |
|
| 24 |
|
| 25 |
-
@router.post("/emotion", response_model=
|
| 26 |
def create_or_update_emotion(
|
| 27 |
data: EmotionLogCreate, session: Session = Depends(get_session)
|
| 28 |
):
|
| 29 |
record = add_or_update_emotion(data, session)
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
-
@router.get("/emotion/{user_id}", response_model=
|
| 38 |
def get_user_emotions(user_id: str, session: Session = Depends(get_session)):
|
| 39 |
-
|
|
|
|
|
|
| 3 |
|
| 4 |
from src.core.database import engine
|
| 5 |
|
| 6 |
+
from .schemas import BaseResponse, EmotionLogCreate
|
| 7 |
from .service import add_or_update_emotion, get_emotions, get_home_data
|
| 8 |
|
| 9 |
router = APIRouter(prefix="/home", tags=["Home"])
|
|
|
|
| 14 |
yield session
|
| 15 |
|
| 16 |
|
| 17 |
+
@router.get("/{user_id}", response_model=BaseResponse)
|
| 18 |
def fetch_home_data(user_id: str, session: Session = Depends(get_session)):
|
| 19 |
try:
|
| 20 |
+
data = get_home_data(user_id, session)
|
| 21 |
+
return {"code": 200, "data": data}
|
| 22 |
except ValueError as e:
|
| 23 |
raise HTTPException(status_code=404, detail=str(e))
|
| 24 |
|
| 25 |
|
| 26 |
+
@router.post("/emotion", response_model=BaseResponse)
|
| 27 |
def create_or_update_emotion(
|
| 28 |
data: EmotionLogCreate, session: Session = Depends(get_session)
|
| 29 |
):
|
| 30 |
record = add_or_update_emotion(data, session)
|
| 31 |
+
return {
|
| 32 |
+
"code": 200,
|
| 33 |
+
"data": {
|
| 34 |
+
"log_date": record.log_date,
|
| 35 |
+
"morning_emotion": record.morning_emotion,
|
| 36 |
+
"evening_emotion": record.evening_emotion,
|
| 37 |
+
},
|
| 38 |
+
}
|
| 39 |
|
| 40 |
|
| 41 |
+
@router.get("/emotion/{user_id}", response_model=BaseResponse)
|
| 42 |
def get_user_emotions(user_id: str, session: Session = Depends(get_session)):
|
| 43 |
+
data = get_emotions(user_id, session)
|
| 44 |
+
return {"code": 200, "data": data}
|
src/home/schemas.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
# pylint: disable=no-name-in-module
|
| 2 |
# pylint: disable=no-self-argument
|
| 3 |
-
|
| 4 |
from datetime import date
|
| 5 |
-
from typing import List, Optional
|
| 6 |
|
| 7 |
from pydantic import BaseModel
|
| 8 |
|
|
@@ -17,11 +16,16 @@ class EmotionLogCreate(BaseModel):
|
|
| 17 |
class EmotionLogResponse(BaseModel):
|
| 18 |
log_date: date
|
| 19 |
morning_emotion: Optional[int]
|
| 20 |
-
|
| 21 |
|
| 22 |
|
| 23 |
-
class
|
| 24 |
user_id: str
|
| 25 |
user_name: str
|
| 26 |
philosophy_text: str
|
| 27 |
recent_emotions: List[EmotionLogResponse]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# pylint: disable=no-name-in-module
|
| 2 |
# pylint: disable=no-self-argument
|
|
|
|
| 3 |
from datetime import date
|
| 4 |
+
from typing import List, Optional, Union
|
| 5 |
|
| 6 |
from pydantic import BaseModel
|
| 7 |
|
|
|
|
| 16 |
class EmotionLogResponse(BaseModel):
|
| 17 |
log_date: date
|
| 18 |
morning_emotion: Optional[int]
|
| 19 |
+
evening_emotion: Optional[int]
|
| 20 |
|
| 21 |
|
| 22 |
+
class HomeResponseData(BaseModel):
|
| 23 |
user_id: str
|
| 24 |
user_name: str
|
| 25 |
philosophy_text: str
|
| 26 |
recent_emotions: List[EmotionLogResponse]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class BaseResponse(BaseModel):
|
| 30 |
+
code: int
|
| 31 |
+
data: Union[dict, list, None] = None
|
src/home/service.py
CHANGED
|
@@ -4,18 +4,16 @@ from sqlmodel import Session, select
|
|
| 4 |
|
| 5 |
from src.core.models import EmotionLogs, Users
|
| 6 |
|
| 7 |
-
from .schemas import EmotionLogCreate, EmotionLogResponse,
|
| 8 |
|
| 9 |
PHILOSOPHY_TEXT = "Your mind is your greatest asset — train it daily."
|
| 10 |
|
| 11 |
|
| 12 |
-
def get_home_data(user_id: str, session: Session) ->
|
| 13 |
-
# Fetch user info
|
| 14 |
user = session.exec(select(Users).where(Users.id == user_id)).first()
|
| 15 |
if not user:
|
| 16 |
raise ValueError("User not found")
|
| 17 |
|
| 18 |
-
# Fetch last 7 days of emotion logs
|
| 19 |
seven_days_ago = date.today() - timedelta(days=7)
|
| 20 |
emotion_logs = session.exec(
|
| 21 |
select(EmotionLogs)
|
|
@@ -33,7 +31,7 @@ def get_home_data(user_id: str, session: Session) -> HomeResponse:
|
|
| 33 |
for log in emotion_logs
|
| 34 |
]
|
| 35 |
|
| 36 |
-
return
|
| 37 |
user_id=str(user.id),
|
| 38 |
user_name=user.user_name,
|
| 39 |
philosophy_text=PHILOSOPHY_TEXT,
|
|
@@ -49,7 +47,6 @@ def add_or_update_emotion(data: EmotionLogCreate, session: Session):
|
|
| 49 |
).first()
|
| 50 |
|
| 51 |
if existing_log:
|
| 52 |
-
# Update existing record
|
| 53 |
if data.morning_emotion is not None:
|
| 54 |
existing_log.morning_emotion = data.morning_emotion
|
| 55 |
if data.evening_emotion is not None:
|
|
|
|
| 4 |
|
| 5 |
from src.core.models import EmotionLogs, Users
|
| 6 |
|
| 7 |
+
from .schemas import EmotionLogCreate, EmotionLogResponse, HomeResponseData
|
| 8 |
|
| 9 |
PHILOSOPHY_TEXT = "Your mind is your greatest asset — train it daily."
|
| 10 |
|
| 11 |
|
| 12 |
+
def get_home_data(user_id: str, session: Session) -> HomeResponseData:
|
|
|
|
| 13 |
user = session.exec(select(Users).where(Users.id == user_id)).first()
|
| 14 |
if not user:
|
| 15 |
raise ValueError("User not found")
|
| 16 |
|
|
|
|
| 17 |
seven_days_ago = date.today() - timedelta(days=7)
|
| 18 |
emotion_logs = session.exec(
|
| 19 |
select(EmotionLogs)
|
|
|
|
| 31 |
for log in emotion_logs
|
| 32 |
]
|
| 33 |
|
| 34 |
+
return HomeResponseData(
|
| 35 |
user_id=str(user.id),
|
| 36 |
user_name=user.user_name,
|
| 37 |
philosophy_text=PHILOSOPHY_TEXT,
|
|
|
|
| 47 |
).first()
|
| 48 |
|
| 49 |
if existing_log:
|
|
|
|
| 50 |
if data.morning_emotion is not None:
|
| 51 |
existing_log.morning_emotion = data.morning_emotion
|
| 52 |
if data.evening_emotion is not None:
|