Skip to content

Commit e895383

Browse files
authored
Merge pull request #82 from ArtNeplatform/feature/#12-getExhibitionList
[fix] 작품구매자 마이페이지 조회 오류 수정
2 parents 8b291c0 + b01a1c5 commit e895383

5 files changed

Lines changed: 182 additions & 14 deletions

File tree

src/domain/Exhibition/ExhibitionModel.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,45 @@ class Exhibition extends Model {
122122
throw error;
123123
}
124124
}
125+
126+
// 특정 전시가 사용자의 "마이컬렉션"에 추가되었는지 체크
127+
static async getExhibitionDetail(exhibition_id, user_id) {
128+
try {
129+
const exhibition = await Exhibition.findOne({
130+
attributes: ['id', 'title', 'image_url', 'author_id'],
131+
where: { id: exhibition_id },
132+
});
133+
134+
if (!exhibition) {
135+
return null;
136+
}
137+
138+
const author = await Author.findOne({
139+
attributes: ['author_name', 'author_image_url'],
140+
where: { id: exhibition.author_id },
141+
});
142+
143+
const isFavorite = await FavoriteExhibition.findOne({
144+
where: { user_id, exhibition_id },
145+
});
146+
147+
return {
148+
exhibition: {
149+
exhibition_id: exhibition.id,
150+
title: exhibition.title,
151+
image_url: exhibition.image_url,
152+
is_favorite: !!isFavorite, // `true` or `false`
153+
},
154+
author: {
155+
author_id: exhibition.author_id,
156+
name: author.author_name,
157+
image_url: author.author_image_url,
158+
},
159+
};
160+
} catch (error) {
161+
throw error;
162+
}
163+
}
125164
}
126165
// 모델 정의
127166
Exhibition.init(

src/domain/Exhibition/exhibitionRoutes.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import Exhibition from './ExhibitionModel.js'; // Exhibition 모델 불러오기
3+
import FavoriteExhibition from '../Favorite/FavoriteExhibitionModel.js';
34
import Artwork from '../Artwork/ArtworkModel.js'; // 작가의 작품 리스트 가져오기
45
import Author from '../Author/AuthorModel.js';
56
import User from '../User/UserModel.js';
@@ -99,6 +100,7 @@ router.post('/exhibitions', verifyToken, upload.array('exhibition_image', 1), as
99100
router.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
168257
router.put('/exhibitions/:id', verifyToken, uploadMiddleware, async (req, res) => {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { DataTypes, Model } from 'sequelize';
2+
import sequelize from '../sequelize.js';
3+
import User from '../User/UserModel.js';
4+
import Exhibition from '../Exhibition/ExhibitionModel.js';
5+
6+
class FavoriteExhibition extends Model {}
7+
8+
FavoriteExhibition.init(
9+
{
10+
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
11+
user_id: {
12+
type: DataTypes.BIGINT,
13+
references: { model: User, key: 'id' }
14+
},
15+
exhibition_id: {
16+
type: DataTypes.BIGINT,
17+
references: { model: Exhibition, key: 'id' }
18+
},
19+
createdAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
20+
updatedAt: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW }
21+
},
22+
{
23+
sequelize,
24+
modelName: 'FavoriteExhibition',
25+
tableName: 'FavoriteExhibitions',
26+
timestamps: true
27+
}
28+
);
29+
30+
export default FavoriteExhibition;

src/domain/MyPage/MyPageModel.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Exhibition from '../Exhibition/ExhibitionModel.js';
77
import Auction from '../Auction/AuctionModel.js';
88
import AuctionBid from '../Auction/AuctionbidModel.js';
99
import FavoriteArtwork from '../Favorite/FavoriteArtworkModel.js';
10-
import FavoriteExhibition from '../Favorite/FavoriteExhibition.js';
11-
import Payment from '../Payment/Payment.js';
10+
import FavoriteExhibition from '../Favorite/FavoriteExhibitionModel.js';
11+
import Payment from '../Payment/PaymentModel.js';
1212

1313
class MyPage extends Model {
1414
// 작품 구매자의 마이페이지 데이터 조회
@@ -22,10 +22,11 @@ class MyPage extends Model {
2222

2323
// 결제 진행 상황 카운트
2424
const paymentCounts = await Payment.findAll({
25-
where: { buyer_id: user_id },
25+
where: { user_id: user_id },
2626
attributes: [
27-
[sequelize.fn('SUM', sequelize.literal("status = '결제 대기중'")), 'pending'],
28-
[sequelize.fn('SUM', sequelize.literal("status = '결제 완료'")), 'completed'],
27+
[sequelize.fn('SUM', sequelize.literal("payment_status = '결제 대기중'")), 'pending'],
28+
[sequelize.fn('SUM', sequelize.literal("payment_status = '결제 완료'")), 'completed'],
29+
[sequelize.fn('SUM', sequelize.literal("payment_status = '수령 완료'")), 'received'],
2930
],
3031
raw: true,
3132
});
@@ -50,15 +51,21 @@ class MyPage extends Model {
5051

5152
// 결제 내역 (최대 3개) + `payment_id` 추가
5253
const payments = await Payment.findAll({
53-
where: { buyer_id: user_id },
54+
where: { user_id: user_id },
5455
attributes: ['id', 'auction_id', 'payment_price', 'created_at', 'payment_status'],
5556
include: [{
56-
model: Artwork,
57-
as: 'artwork',
58-
attributes: ['title'],
59-
include: [{ model: User, as: 'author', attributes: ['author_name', 'profile_image_url'] }]
57+
model: Auction,
58+
as: 'auction',
59+
attributes: ['artwork_id'],
60+
include: [{
61+
model: Artwork,
62+
as: 'artwork',
63+
attributes: ['title'],
64+
include: [{ model: Author, as: 'author', attributes: ['author_name', 'author_image_url'] }] // 수정된 부분
65+
}]
6066
}],
61-
limit: 3, order: [['created_at', 'DESC']],
67+
limit: 3,
68+
order: [['created_at', 'DESC']],
6269
});
6370

6471
// 좋아요한 작품 조회
@@ -69,14 +76,13 @@ class MyPage extends Model {
6976
model: Artwork,
7077
as: 'artwork',
7178
attributes: ['id', 'title', 'thumbnail_image_url', 'height', 'width'],
72-
include: [{ model: Author, as: 'author', attributes: ['author_name', 'profile_image_url'] }]
79+
include: [{ model: Author, as: 'author', attributes: ['author_name', 'author_image_url'] }]
7380
}]
7481
});
7582

7683
// 좋아요한 전시 조회
7784
const likedExhibitions = await FavoriteExhibition.findAll({
7885
where: { user_id: user_id },
79-
attributes: ['exhibition_id'],
8086
include: [{
8187
model: Exhibition,
8288
as: 'exhibition',

src/domain/sequelizeRelations.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Payment from './Payment/PaymentModel.js';
77
import FavoriteArtwork from './Favorite/FavoriteArtworkModel.js';
88
import FavoriteAuction from './Favorite/FavoriteAuctionModel.js';
99
import Exhibition from './Exhibition/ExhibitionModel.js';
10+
import FavoriteExhibition from './Favorite/FavoriteExhibitionModel.js';
1011

1112
Author.belongsTo(User, { foreignKey: 'user_id' });
1213
Artwork.belongsTo(Author, { foreignKey: 'author_id', as: 'author' });
@@ -24,4 +25,7 @@ FavoriteAuction.belongsTo(Auction, { foreignKey: 'auction_id' });
2425
Author.hasMany(Exhibition, { foreignKey: 'author_id' });
2526
Exhibition.belongsTo(Author, { foreignKey: 'author_id' });
2627
Exhibition.hasMany(Artwork, { foreignKey: 'exhibition_id', as: 'artworks' });
27-
Artwork.belongsTo(Exhibition, { foreignKey: 'exhibition_id', as: 'exhibition' });
28+
Artwork.belongsTo(Exhibition, { foreignKey: 'exhibition_id', as: 'exhibition' });
29+
Payment.belongsTo(Artwork, { foreignKey: 'auction_id', targetKey: 'id', as: 'artwork', through: Auction });
30+
FavoriteExhibition.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
31+
FavoriteExhibition.belongsTo(Exhibition, { foreignKey: 'exhibition_id', as: 'exhibition' });

0 commit comments

Comments
 (0)