Skip to content

Commit b71744f

Browse files
committed
feat : 조회 Response 추가
1 parent fa85ad0 commit b71744f

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

app/diary/router.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,7 @@ async def create_diary_with_emotion_based_recommendation(
727727
return response_data
728728

729729

730-
@router.get("/{diary_id}", response_model=DiaryResponse,
731-
summary="일기 조회",
732-
description="특정 일기의 상세 정보를 조회합니다.")
730+
@router.get("/{diary_id}", response_model=DiaryResponse)
733731
def get_diary(
734732
diary_id: int,
735733
current_user: User = Depends(get_current_user),
@@ -743,9 +741,26 @@ def get_diary(
743741
if not diary:
744742
raise HTTPException(status_code=404, detail="일기를 찾을 수 없습니다.")
745743

746-
return diary
744+
recommended_songs = db.query(RecommendedSong).filter(
745+
RecommendedSong.diary_id == diary.id
746+
).order_by(RecommendedSong.similarity_score.desc()).all()
747747

748-
@router.get("/diary/{diary_id}/recommended-songs", response_model=List[RecommendSongResponse],
748+
main_song = db.query(RecommendedSong).get(diary.main_recommended_song_id)
749+
750+
return DiaryResponse(
751+
id=diary.id,
752+
user_id=diary.user_id,
753+
content=diary.content,
754+
emotiontype_id=diary.emotiontype_id,
755+
confidence=diary.confidence,
756+
created_at=diary.created_at,
757+
updated_at=diary.updated_at,
758+
recommended_songs=recommended_songs,
759+
main_recommend_song=main_song,
760+
top_emotions=[]
761+
)
762+
763+
@router.get("/{diary_id}/recommended-songs", response_model=List[RecommendSongResponse],
749764
summary="추천 노래 조회",
750765
description="특정 일기에 대한 추천 노래 리스트를 조회합니다.")
751766
def get_recommended_songs_by_diary(
@@ -770,9 +785,7 @@ def get_recommended_songs_by_diary(
770785

771786
return songs
772787

773-
@router.get("", response_model=List[DiaryResponse],
774-
summary="내 일기 목록 조회",
775-
description="로그인한 사용자가 작성한 모든 일기를 조회합니다.")
788+
@router.get("", response_model=List[DiaryResponse], summary="내 일기 목록 조회", description="로그인한 사용자가 작성한 모든 일기를 조회합니다.")
776789
def get_all_diaries(
777790
current_user: User = Depends(get_current_user),
778791
db: Session = Depends(get_db)
@@ -781,7 +794,28 @@ def get_all_diaries(
781794
Diary.user_id == current_user.id
782795
).all()
783796

784-
return diaries
797+
results = []
798+
for diary in diaries:
799+
recommended_songs = db.query(RecommendedSong).filter(
800+
RecommendedSong.diary_id == diary.id
801+
).order_by(RecommendedSong.similarity_score.desc()).all()
802+
803+
main_song = db.query(RecommendedSong).get(diary.main_recommended_song_id)
804+
805+
results.append(DiaryResponse(
806+
id=diary.id,
807+
user_id=diary.user_id,
808+
content=diary.content,
809+
emotiontype_id=diary.emotiontype_id,
810+
confidence=diary.confidence,
811+
created_at=diary.created_at,
812+
updated_at=diary.updated_at,
813+
recommended_songs=recommended_songs,
814+
main_recommend_song=main_song,
815+
top_emotions=[]
816+
))
817+
818+
return results
785819

786820
@router.put("/{diary_id}", response_model=DiaryResponse,
787821
summary="일기 수정",

app/diary/schemas.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydantic import BaseModel
1+
from pydantic import BaseModel, ConfigDict
22
from datetime import datetime
33
from typing import Optional, List, Dict, Union
44

@@ -27,33 +27,34 @@ class SongResponse(BaseModel):
2727
class Config:
2828
allow_population_by_field_name = True
2929

30+
class RecommendSongResponse(BaseModel):
31+
id: int
32+
song_id: int
33+
song_name: str
34+
artist: List[str]
35+
genre: str
36+
album_image: str
37+
best_lyric: str
38+
similarity_score: float
39+
40+
class Config:
41+
from_attributes = True
42+
3043
class DiaryResponse(BaseModel):
3144
id: int
3245
user_id: int
3346
content: str
3447
emotiontype_id: Optional[int] = None
3548
confidence: Optional[float] = None
36-
recommended_songs: list[SongResponse]
49+
recommended_songs: List[RecommendSongResponse]
50+
main_recommend_song: Optional[RecommendSongResponse] = None
3751
top_emotions: list[EmotionScore] = None
3852
created_at: datetime
3953
updated_at: datetime
4054

4155
class Config:
4256
from_attributes = True # pydantic v2용 (orm_mode → from_attributes)
4357

44-
class RecommendSongResponse(BaseModel):
45-
id: int
46-
song_id: int
47-
song_name: str
48-
artist: List[str]
49-
genre: str
50-
album_image: str
51-
best_lyric: str
52-
similarity_score: float
53-
54-
class Config:
55-
orm_mode = True
56-
5758
class SentenceEmotion(BaseModel):
5859
sentence: str
5960
predicted_emotion_id: int

0 commit comments

Comments
 (0)