박권수

feat. get all of bottle list -> get bottle list of hub

...@@ -4,6 +4,7 @@ const Hub = require('../../models/hub'); ...@@ -4,6 +4,7 @@ const Hub = require('../../models/hub');
4 const Medicine = require('../../models/medicine'); 4 const Medicine = require('../../models/medicine');
5 const Mqtt = require('../../lib/MqttModule'); 5 const Mqtt = require('../../lib/MqttModule');
6 const jwt = require('jsonwebtoken'); 6 const jwt = require('jsonwebtoken');
7 +const { createIndexes } = require('../../models/bottle');
7 8
8 //약병 등록 9 //약병 등록
9 exports.bottleConnect = async(ctx) => { 10 exports.bottleConnect = async(ctx) => {
...@@ -171,13 +172,20 @@ exports.getBottleList = async(ctx) => { ...@@ -171,13 +172,20 @@ exports.getBottleList = async(ctx) => {
171 } 172 }
172 173
173 const { userId } = jwt.verify(token, process.env.JWT_SECRET); 174 const { userId } = jwt.verify(token, process.env.JWT_SECRET);
174 - const hubList = await Hub.find({ userId }) 175 + const { hubId } = ctx.params;
175 - if(!hubList || !hubList.length) { 176 +
177 + const hub = await Hub.findByHubId(hubId);
178 + if(!hub) {
176 ctx.status = 404; 179 ctx.status = 404;
177 return; 180 return;
178 } 181 }
179 182
180 - const bottleList = await getBottleListByHub(hubList); 183 + if(hub.getHub_UserId() !== userId) {
184 + ctx.status = 403;
185 + return;
186 + }
187 +
188 + const bottleList = await Bottle.find({ hubId });
181 if(!bottleList || !bottleList.length) { 189 if(!bottleList || !bottleList.length) {
182 ctx.status = 404; 190 ctx.status = 404;
183 return; 191 return;
...@@ -185,17 +193,5 @@ exports.getBottleList = async(ctx) => { ...@@ -185,17 +193,5 @@ exports.getBottleList = async(ctx) => {
185 193
186 ctx.status = 200; 194 ctx.status = 200;
187 ctx.body = bottleList; 195 ctx.body = bottleList;
188 -} 196 +
189 - 197 +}
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 -}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -36,11 +36,11 @@ bottle.get('/:bottleId', bottleCtrl.lookupInfo); ...@@ -36,11 +36,11 @@ bottle.get('/:bottleId', bottleCtrl.lookupInfo);
36 bottle.patch('/:bottleId', bottleCtrl.setMedicine); 36 bottle.patch('/:bottleId', bottleCtrl.setMedicine);
37 37
38 /** 38 /**
39 - * 현재 로그인한 유저의 약병 리스트를 가져옴 39 + * 현재 로그인한 유저의 허브 중, 해당 허브에 등록된 약병 리스트를 가져옴
40 * request parameter : x 40 * request parameter : x
41 - * url : http://localhost:4000/api/bottle 41 + * url : http://localhost:4000/api/bottle/hub/:hubId
42 * return : bottle List(json type List) 42 * return : bottle List(json type List)
43 */ 43 */
44 -bottle.get('/', bottleCtrl.getBottleList) 44 +bottle.get('/hub/:hubId', bottleCtrl.getBottleList)
45 45
46 module.exports = bottle; 46 module.exports = bottle;
...\ No newline at end of file ...\ No newline at end of file
......