forked from moduwa-aac/moduwa-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwords.controller.js
More file actions
157 lines (132 loc) · 3.67 KB
/
words.controller.js
File metadata and controls
157 lines (132 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import wordsService from '../services/words.service.js';
import { GetWordsQueryDto, CreateWordDto, UpdateFavoriteDto, UpdateWordDto, ReorderWordsDto } from '../dto/words.dto.js';
/**
* Words Controller
*/
export class WordsController {
/**
* GET /api/words - 낱말 카드 조회
*/
async getWords(req, res, next) {
try {
const userId = req.user?.userId;
const accountType = req.user?.accountType;
const queryDto = new GetWordsQueryDto({
categoryId: req.query.categoryId,
onlyFavorite: req.query.onlyFavorite
});
const result = await wordsService.getWords(
userId,
accountType,
queryDto.categoryId,
queryDto.onlyFavorite
);
return res.success(result, '낱말 카드 목록 조회 성공');
} catch (error) {
next(error);
}
}
/**
* POST /api/words - 개인 낱말 카드 추가
*/
async createWord(req, res, next) {
try {
const userId = req.user.userId;
const createDto = new CreateWordDto({
categoryId: req.body.categoryId,
word: req.body.word,
imageUrl: req.body.imageUrl
});
// 유효성 검증
createDto.validate();
const createdWord = await wordsService.createWord(
userId,
createDto.categoryId,
createDto.word,
createDto.imageUrl
);
return res.success({ word: createdWord }, '개인 낱말 카드 추가 성공', 201);
} catch (error) {
next(error);
}
}
/**
* PATCH /api/words/:cardId/favorite - 낱말 카드 즐겨찾기 변경
*/
async updateFavorite(req, res, next) {
try {
const userId = req.user.userId;
const { cardId } = req.params;
const updateDto = new UpdateFavoriteDto({
isFavorite: req.body.isFavorite
});
// 유효성 검증
updateDto.validate();
const result = await wordsService.updateFavorite(
userId,
cardId,
updateDto.isFavorite
);
return res.success(result, '낱말 카드 즐겨찾기 변경 성공');
} catch (error) {
next(error);
}
}
/**
* PATCH /api/words/:cardId - 낱말 카드 수정
*/
async updateWord(req, res, next) {
try {
const userId = req.user.userId;
const { cardId } = req.params;
const updateDto = new UpdateWordDto({
word: req.body.word,
imageUrl: req.body.imageUrl,
userCategoryId: req.body.categoryId
});
// 유효성 검증
updateDto.validate();
const updatedWord = await wordsService.updateWord(
userId,
cardId,
updateDto.word,
updateDto.imageUrl,
updateDto.userCategoryId
);
return res.success({ word: updatedWord }, '낱말 카드 수정 성공');
} catch (error) {
next(error);
}
}
/**
* DELETE /api/words/:cardId - 낱말 카드 삭제
*/
async deleteWord(req, res, next) {
try {
const userId = req.user.userId;
const { cardId } = req.params;
await wordsService.deleteWord(userId, cardId);
return res.success(null, '낱말 카드 삭제 성공');
} catch (error) {
next(error);
}
}
/**
* PATCH /api/words/reorder - 낱말 카드 순서 변경
*/
async reorderWords(req, res, next) {
try {
const userId = req.user.userId;
const { categoryId, orderedCardIds } = req.body;
const reorderedWords = await wordsService.reorderWords(
userId,
categoryId,
orderedCardIds
);
return res.success({ words: reorderedWords }, '낱말 카드 순서 변경 성공');
} catch (error) {
next(error);
}
}
}
export default new WordsController();