Skip to content

Commit a5cc5d5

Browse files
authored
Merge pull request #181 from prgrms-web-devcourse-final-project/feat/179-post-add-videoid
[feat] 스포티파이 ID로 비디오 ID 조회 기능 추가
2 parents e4f08f1 + f253589 commit a5cc5d5

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/apis/emotionRecord.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export const postEmotionRecord = async (emotionRecord: EmotionRecordRequest) =>
2222
return data;
2323
};
2424

25+
// spotifyId로 videoId 조회
26+
export const getSpotifyVideoId = async (spotifyId: string) => {
27+
const { data } = await axiosInstance.get(`/emotion/spotify-video`, {
28+
params: { spotifyId },
29+
});
30+
return data;
31+
};
32+
2533
// 감정 기록 수정
2634
export const putEmotionRecord = async (recordId: number, emotionRecord: EmotionRecordRequest) => {
2735
const { data } = await axiosInstance.put(`/emotion/${recordId}`, emotionRecord);

src/pages/post/Post.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import MusicCard from '@/components/MusicCard';
44
import Comment from '@/pages/post/components/Comment';
55
import { useEffect, useState } from 'react';
66
import { useSheetStore } from '@/store/sheetStore';
7-
import { getEmotionRecordById, postEmotionRecord, putEmotionRecord } from '@/apis/emotionRecord';
7+
import {
8+
getEmotionRecordById,
9+
getSpotifyVideoId,
10+
postEmotionRecord,
11+
putEmotionRecord,
12+
} from '@/apis/emotionRecord';
813
import { useModalStore } from '@/store/modalStore';
914
import { useNavigate, useParams } from 'react-router';
1015
import { useMusicCardStore } from '@/store/MusicCardStore';
1116
import SpinLoading from '@/components/loading/SpinLoading';
1217
import Complete from '@/components/loading/Complete';
1318
import ErrorShake from '@/components/loading/ErrorShake';
19+
import { searchYoutubeVideo } from '@/apis/youtube';
1420

1521
export default function Post() {
1622
const navigate = useNavigate();
@@ -111,15 +117,43 @@ export default function Post() {
111117
});
112118
};
113119

120+
// spotifyId로 videoId 조회
121+
const fetchSpotifyVideoId = async (spotifyId: string, artist: string, title: string) => {
122+
try {
123+
const data = await getSpotifyVideoId(spotifyId); // 서버에 videoId 조회
124+
console.log('videoId 조회 결과:', data);
125+
126+
// 서버에 videoId 가 있으면
127+
if (data.code === 200 && data.data) return data.videoId;
128+
// 서버에 videoId 가 없으면
129+
else {
130+
// youtube 검색
131+
const videoId = await searchYoutubeVideo(`${artist} - ${title} lyrics`);
132+
console.log('유튜브 videoId:', videoId);
133+
return videoId;
134+
}
135+
} catch (error) {
136+
console.error(error);
137+
}
138+
};
139+
114140
// 기록 완료
115141
const onCompletePost = async () => {
116142
if (!isCompletePost) return;
117143

144+
// 음악 선택 시 videoId 조회
145+
const videoId = await fetchSpotifyVideoId(
146+
selectedPostMusic?.spotifyId,
147+
selectedPostMusic?.artistName,
148+
selectedPostMusic?.songTitle,
149+
);
150+
118151
try {
119152
setIsLoading(true);
120153

121154
const requestData = {
122155
spotifyId: selectedPostMusic?.spotifyId,
156+
videoId: videoId,
123157
title: selectedPostMusic?.songTitle,
124158
artist: selectedPostMusic?.artistName,
125159
albumImage: selectedPostMusic?.albumImage,
@@ -136,6 +170,7 @@ export default function Post() {
136170
data = await postEmotionRecord(requestData);
137171
}
138172
console.log(isEditMode ? '수정 완료' : '기록 완료:', data);
173+
console.log('요청 데이터:', requestData);
139174

140175
setIsComplete(true);
141176
handlePostSuccessModal();

0 commit comments

Comments
 (0)