Skip to content

Commit fa85ad0

Browse files
committed
feat : 음악 선택 추가
1 parent 9c21c51 commit fa85ad0

3 files changed

Lines changed: 54 additions & 7 deletions

File tree

app/diary/models.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from app.database import Base
55
from app.emotion.models import EmotionType
66

7+
78
class Diary(Base):
89
__tablename__ = "diary"
910

@@ -16,9 +17,17 @@ class Diary(Base):
1617
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
1718
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
1819

20+
main_recommended_song_id = Column(Integer, ForeignKey("recommendedSongs.id"), nullable=True)
21+
1922
user = relationship("User", back_populates="diaries")
2023
emotion = relationship(EmotionType, back_populates="diaries")
21-
recommended_songs = relationship("RecommendedSong", back_populates="diary", cascade="all, delete-orphan")
24+
25+
recommended_songs = relationship(
26+
"RecommendedSong",
27+
back_populates="diary",
28+
cascade="all, delete-orphan",
29+
foreign_keys="[RecommendedSong.diary_id]"
30+
)
2231

2332

2433
class diaryEmbedding(Base):
@@ -43,4 +52,9 @@ class RecommendedSong(Base):
4352
similarity_score = Column(Float)
4453
created_at = Column(DateTime, default=datetime.utcnow)
4554

46-
diary = relationship("Diary", back_populates="recommended_songs")
55+
# ✅ 어떤 FK를 기준으로 연결할지 명시
56+
diary = relationship(
57+
"Diary",
58+
back_populates="recommended_songs",
59+
foreign_keys=[diary_id]
60+
)

app/diary/router.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from app.user.auth import get_current_user
99
from app.diary.models import Diary, RecommendedSong
1010
from app.diary.schemas import DiaryCreateRequest, DiaryUpdateRequest, DiaryResponse, DiaryCountResponse, SongResponse, \
11-
DiaryPreviewResponse
11+
DiaryPreviewResponse, RecommendSongResponse
1212
from app.user.models import User
1313
from app.embedding.models import kobert, save_diary_embedding, split_sentences, get_user_preferred_genres, \
1414
get_songs_by_genre, get_song_embeddings, calculate_similarity
@@ -745,15 +745,14 @@ def get_diary(
745745

746746
return diary
747747

748-
@router.get("/{diary_id}/recommended-songs", response_model=list[SongResponse],
748+
@router.get("/diary/{diary_id}/recommended-songs", response_model=List[RecommendSongResponse],
749749
summary="추천 노래 조회",
750750
description="특정 일기에 대한 추천 노래 리스트를 조회합니다.")
751751
def get_recommended_songs_by_diary(
752752
diary_id: int,
753-
current_user: User = Depends(get_current_user),
753+
current_user = Depends(get_current_user),
754754
db: Session = Depends(get_db)
755755
):
756-
# 1. 해당 일기가 유저의 것인지 검증
757756
diary = db.query(Diary).filter(
758757
Diary.id == diary_id,
759758
Diary.user_id == current_user.id
@@ -762,7 +761,6 @@ def get_recommended_songs_by_diary(
762761
if not diary:
763762
raise HTTPException(status_code=404, detail="일기를 찾을 수 없습니다.")
764763

765-
# 2. 추천곡 조회
766764
songs = db.query(RecommendedSong).filter(
767765
RecommendedSong.diary_id == diary_id
768766
).order_by(RecommendedSong.similarity_score.desc()).all()
@@ -876,3 +874,25 @@ def get_diary_count_by_month(
876874
month=month,
877875
diary_count=diary_count
878876
)
877+
878+
@router.put("/{diary_id}/set-main-song", status_code=200)
879+
async def set_main_song(
880+
diary_id: int,
881+
recommended_song_id: int,
882+
current_user = Depends(get_current_user),
883+
db: Session = Depends(get_db)
884+
):
885+
diary = db.query(Diary).filter(Diary.id == diary_id, Diary.user_id == current_user.id).first()
886+
if not diary:
887+
raise HTTPException(status_code=404, detail="일기를 찾을 수 없습니다.")
888+
889+
song = db.query(RecommendedSong).filter(
890+
RecommendedSong.id == recommended_song_id,
891+
RecommendedSong.diary_id == diary.id
892+
).first()
893+
if not song:
894+
raise HTTPException(status_code=400, detail="추천곡이 일기와 일치하지 않습니다.")
895+
896+
diary.main_recommended_song_id = song.id
897+
db.commit()
898+
return {"message": "대표 음악이 설정되었습니다."}

app/diary/schemas.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ class DiaryResponse(BaseModel):
4141
class Config:
4242
from_attributes = True # pydantic v2용 (orm_mode → from_attributes)
4343

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+
4457
class SentenceEmotion(BaseModel):
4558
sentence: str
4659
predicted_emotion_id: int

0 commit comments

Comments
 (0)