박권수

feat. get user's bottle List

...@@ -162,3 +162,40 @@ exports.setMedicine = async(ctx) => { ...@@ -162,3 +162,40 @@ exports.setMedicine = async(ctx) => {
162 ctx.status = 200; 162 ctx.status = 200;
163 } 163 }
164 164
165 +//로그인한 유저의 약병 리스트 가져오기
166 +exports.getBottleList = async(ctx) => {
167 + const token = ctx.cookies.get('access_token');
168 + if(!token) {
169 + ctx.status = 401;
170 + return;
171 + }
172 +
173 + const { userId } = jwt.verify(token, process.env.JWT_SECRET);
174 + const hubList = await Hub.find({ userId })
175 + if(!hubList) {
176 + ctx.status = 404;
177 + return;
178 + }
179 +
180 + const bottleList = await getBottleListByHub(hubList);
181 + if(!bottleList) {
182 + ctx.status = 404;
183 + return;
184 + }
185 +
186 + ctx.status = 200;
187 + ctx.body = bottleList;
188 +}
189 +
190 +const getBottleListByHub = async (hubList) => {
191 + const result = []
192 +
193 + for (const hub of hubList) {
194 + const bottle = await Bottle.find({
195 + hubId : hub.hubId
196 + });
197 + result.push(...bottle)
198 + }
199 +
200 + return result;
201 +}
......
...@@ -35,4 +35,12 @@ bottle.get('/:bottleId', bottleCtrl.lookupInfo); ...@@ -35,4 +35,12 @@ bottle.get('/:bottleId', bottleCtrl.lookupInfo);
35 */ 35 */
36 bottle.patch('/:bottleId', bottleCtrl.setMedicine); 36 bottle.patch('/:bottleId', bottleCtrl.setMedicine);
37 37
38 +/**
39 + * 현재 로그인한 유저의 약병 리스트를 가져옴
40 + * request parameter : x
41 + * url : http://localhost:4000/api/bottle
42 + * return : bottle List(json type List)
43 + */
44 +bottle.get('/', bottleCtrl.getBottleList)
45 +
38 module.exports = bottle; 46 module.exports = bottle;
...\ No newline at end of file ...\ No newline at end of file
......