박권수

feat. get user's bottle List

......@@ -162,3 +162,40 @@ exports.setMedicine = async(ctx) => {
ctx.status = 200;
}
//로그인한 유저의 약병 리스트 가져오기
exports.getBottleList = async(ctx) => {
const token = ctx.cookies.get('access_token');
if(!token) {
ctx.status = 401;
return;
}
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const hubList = await Hub.find({ userId })
if(!hubList) {
ctx.status = 404;
return;
}
const bottleList = await getBottleListByHub(hubList);
if(!bottleList) {
ctx.status = 404;
return;
}
ctx.status = 200;
ctx.body = bottleList;
}
const getBottleListByHub = async (hubList) => {
const result = []
for (const hub of hubList) {
const bottle = await Bottle.find({
hubId : hub.hubId
});
result.push(...bottle)
}
return result;
}
......
......@@ -35,4 +35,12 @@ bottle.get('/:bottleId', bottleCtrl.lookupInfo);
*/
bottle.patch('/:bottleId', bottleCtrl.setMedicine);
/**
* 현재 로그인한 유저의 약병 리스트를 가져옴
* request parameter : x
* url : http://localhost:4000/api/bottle
* return : bottle List(json type List)
*/
bottle.get('/', bottleCtrl.getBottleList)
module.exports = bottle;
\ No newline at end of file
......