11import express from 'express' ;
22import Exhibition from './ExhibitionModel.js' ; // Exhibition 모델 불러오기
3+ import FavoriteExhibition from '../Favorite/FavoriteExhibitionModel.js' ;
34import Artwork from '../Artwork/ArtworkModel.js' ; // 작가의 작품 리스트 가져오기
45import Author from '../Author/AuthorModel.js' ;
56import User from '../User/UserModel.js' ;
@@ -99,6 +100,7 @@ router.post('/exhibitions', verifyToken, upload.array('exhibition_image', 1), as
99100router . get ( '/exhibitions/:exhibition_id' , async ( req , res ) => {
100101 try {
101102 const { exhibition_id } = req . params ;
103+ const user_id = req . user . id ;
102104
103105 const exhibition = await Exhibition . findOne ( {
104106 attributes : [ 'id' , 'title' , 'image_url' , 'author_id' ] ,
@@ -118,6 +120,11 @@ router.get('/exhibitions/:exhibition_id', async (req, res) => {
118120 return sendResponse ( res , status . AUTHOR_NOT_FOUND ) ;
119121 }
120122
123+ // 사용자가 해당 전시를 마이컬렉션에 추가했는지 확인
124+ const isFavorite = await FavoriteExhibition . findOne ( {
125+ where : { user_id, exhibition_id } ,
126+ } ) ;
127+
121128 const authorExhibitions = await Exhibition . findAll ( {
122129 attributes : [ 'id' , 'title' , 'image_url' ] ,
123130 where : {
@@ -140,6 +147,7 @@ router.get('/exhibitions/:exhibition_id', async (req, res) => {
140147 exhibition_id : exhibition . id ,
141148 title : exhibition . title ,
142149 image_url : exhibition . image_url ,
150+ is_favorite : ! ! isFavorite , // 마이컬렉션 추가 여부
143151 } ,
144152 author : {
145153 author_id : exhibition . author_id ,
@@ -163,6 +171,87 @@ router.get('/exhibitions/:exhibition_id', async (req, res) => {
163171 }
164172} ) ;
165173
174+ // 마이컬렉션 추가 API
175+ router . post ( '/exhibitions/:exhibition_id/favorite' , verifyToken , async ( req , res ) => {
176+ try {
177+ const { email } = req . user ;
178+ const user = await User . findOne ( { where : { email } } ) ;
179+
180+ if ( ! user ) {
181+ return res . status ( 404 ) . json ( { message : "User not found" } ) ;
182+ }
183+
184+ const user_id = user . id ;
185+ const exhibition_id = Number ( req . query . exhibition_id ) ; // id는 숫자로 변환
186+
187+ if ( isNaN ( exhibition_id ) ) {
188+ return res . status ( 400 ) . json ( { message : "Invalid exhibition_id" } ) ;
189+ }
190+
191+ // Exhibition 테이블의 id 컬럼과 매칭 확인
192+ const existingExhibition = await Exhibition . findOne ( { where : { id : exhibition_id } } ) ;
193+
194+ if ( ! existingExhibition ) {
195+ return res . status ( 400 ) . json ( { message : "해당 전시는 존재하지 않습니다." } ) ;
196+ }
197+
198+ // FavoriteExhibitions 테이블에서 exhibition_id가 존재하는지 체크
199+ const existingFavorite = await FavoriteExhibition . findOne ( {
200+ where : { user_id, exhibition_id } // exhibition_id가 존재하는지 확인
201+ } ) ;
202+
203+ if ( existingFavorite ) {
204+ return res . status ( 400 ) . json ( { message : "이미 마이컬렉션에 추가된 전시입니다." } ) ;
205+ }
206+
207+ // FavoriteExhibitions 테이블에 데이터 삽입
208+ await FavoriteExhibition . create ( {
209+ user_id,
210+ exhibition_id, // FavoriteExhibitions의 외래 키
211+ createdAt : new Date ( ) ,
212+ updatedAt : new Date ( ) ,
213+ } ) ;
214+
215+ return res . status ( 200 ) . json ( { message : "마이컬렉션에 추가되었습니다." } ) ;
216+ } catch ( error ) {
217+ console . error ( error ) ;
218+ return res . status ( 500 ) . json ( { message : "서버 오류" } ) ;
219+ }
220+ } ) ;
221+
222+ // 마이컬렉션 제거 API
223+ router . delete ( '/exhibitions/:exhibition_id/favorite' , verifyToken , async ( req , res ) => {
224+ try {
225+ const { email } = req . user ;
226+ const user = await User . findOne ( { where : { email } } ) ;
227+
228+ if ( ! user ) {
229+ return res . status ( 404 ) . json ( { message : "User not found" } ) ;
230+ }
231+
232+ const user_id = user . id ;
233+ const exhibition_id = Number ( req . query . exhibition_id ) ; // id를 숫자로 변환
234+
235+ if ( isNaN ( exhibition_id ) ) {
236+ return res . status ( 400 ) . json ( { message : "Invalid exhibition_id" } ) ;
237+ }
238+
239+ // FavoriteExhibitions 테이블에서 해당 전시가 존재하는지 체크
240+ const favorite = await FavoriteExhibition . findOne ( { where : { user_id, exhibition_id } } ) ;
241+
242+ if ( ! favorite ) {
243+ return res . status ( 404 ) . json ( { message : "마이컬렉션에 없는 전시입니다." } ) ;
244+ }
245+
246+ // 데이터 삭제
247+ await favorite . destroy ( ) ;
248+
249+ return res . status ( 200 ) . json ( { message : "마이컬렉션에서 제거되었습니다." } ) ;
250+ } catch ( error ) {
251+ console . error ( error ) ;
252+ return res . status ( 500 ) . json ( { message : "서버 오류" } ) ;
253+ }
254+ } ) ;
166255
167256// 전시 수정 API
168257router . put ( '/exhibitions/:id' , verifyToken , uploadMiddleware , async ( req , res ) => {
0 commit comments