Showing
4 changed files
with
104 additions
and
18 deletions
| ... | @@ -30,7 +30,7 @@ exports.register = async(ctx) => { | ... | @@ -30,7 +30,7 @@ exports.register = async(ctx) => { |
| 30 | await user.setPassword(password); | 30 | await user.setPassword(password); |
| 31 | await user.save(); | 31 | await user.save(); |
| 32 | 32 | ||
| 33 | - ctx.status = 200; | 33 | + ctx.status = 201; |
| 34 | 34 | ||
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| ... | @@ -66,7 +66,7 @@ exports.login = async(ctx) => { | ... | @@ -66,7 +66,7 @@ exports.login = async(ctx) => { |
| 66 | maxAge : 1000 * 60 * 60 * 24 * 30 | 66 | maxAge : 1000 * 60 * 60 * 24 * 30 |
| 67 | }); | 67 | }); |
| 68 | 68 | ||
| 69 | - ctx.status = 201; | 69 | + ctx.status = 200; |
| 70 | ctx.body = { | 70 | ctx.body = { |
| 71 | userId | 71 | userId |
| 72 | }; | 72 | }; |
| ... | @@ -80,5 +80,4 @@ exports.logout = async(ctx) => { | ... | @@ -80,5 +80,4 @@ exports.logout = async(ctx) => { |
| 80 | }); | 80 | }); |
| 81 | 81 | ||
| 82 | ctx.status = 204; | 82 | ctx.status = 204; |
| 83 | - ctx.body = null; | ||
| 84 | }; | 83 | }; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -3,14 +3,18 @@ const Bottle = require('../../models/bottle'); | ... | @@ -3,14 +3,18 @@ const Bottle = require('../../models/bottle'); |
| 3 | const Hub = require('../../models/hub'); | 3 | 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 | 7 | ||
| 8 | +//약병 등록 | ||
| 7 | exports.bottleConnect = async(ctx) => { | 9 | exports.bottleConnect = async(ctx) => { |
| 8 | - const { bottleId, hubId } = ctx.request.body; | 10 | + const token = ctx.cookies.get('access_token'); |
| 11 | + if(!token) { | ||
| 12 | + ctx.status = 401; | ||
| 13 | + return; | ||
| 14 | + } | ||
| 9 | 15 | ||
| 10 | - const newBottle = new Bottle({ | 16 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); |
| 11 | - bottleId, | 17 | + const { bottleId, hubId } = ctx.request.body; |
| 12 | - hubId | ||
| 13 | - }); | ||
| 14 | 18 | ||
| 15 | const isExistBottle = await Bottle.findByBottleId(bottleId); | 19 | const isExistBottle = await Bottle.findByBottleId(bottleId); |
| 16 | if(isExistBottle) { | 20 | if(isExistBottle) { |
| ... | @@ -23,23 +27,41 @@ exports.bottleConnect = async(ctx) => { | ... | @@ -23,23 +27,41 @@ exports.bottleConnect = async(ctx) => { |
| 23 | ctx.status = 404; | 27 | ctx.status = 404; |
| 24 | return; | 28 | return; |
| 25 | } | 29 | } |
| 30 | + if(hub.getHub_UserId() !== userId) { | ||
| 31 | + ctx.status = 403; | ||
| 32 | + return; | ||
| 33 | + } | ||
| 26 | 34 | ||
| 27 | - const hosting = await hub.getHubHost(); | 35 | + const hosting = hub.getHubHost(); |
| 28 | if(!hosting) { | 36 | if(!hosting) { |
| 29 | ctx.status = 404; | 37 | ctx.status = 404; |
| 30 | return; | 38 | return; |
| 31 | } | 39 | } |
| 32 | 40 | ||
| 41 | + | ||
| 42 | + const newBottle = new Bottle({ | ||
| 43 | + bottleId, | ||
| 44 | + hubId | ||
| 45 | + }); | ||
| 46 | + | ||
| 33 | const client = await Mqtt.mqttOn(hosting); | 47 | const client = await Mqtt.mqttOn(hosting); |
| 34 | - const topic = 'bottle/' + bottleId + '/bts'; | 48 | + const topic = 'bottle/' + newBottle.getBottleId() + '/bts'; |
| 35 | Mqtt.mqttSubscribe(client, topic); | 49 | Mqtt.mqttSubscribe(client, topic); |
| 36 | 50 | ||
| 37 | await newBottle.save(); | 51 | await newBottle.save(); |
| 38 | 52 | ||
| 39 | - ctx.status = 200; | 53 | + ctx.status = 201; |
| 40 | }; | 54 | }; |
| 41 | 55 | ||
| 56 | +//약병 등록 해제 | ||
| 42 | exports.bottleDisconnect = async(ctx) => { | 57 | exports.bottleDisconnect = async(ctx) => { |
| 58 | + const token = ctx.cookies.get('access_token'); | ||
| 59 | + if(!token) { | ||
| 60 | + ctx.status = 401; | ||
| 61 | + return; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
| 43 | const { bottleId } = ctx.params; | 65 | const { bottleId } = ctx.params; |
| 44 | 66 | ||
| 45 | const bottle = await Bottle.findByBottleId(bottleId); | 67 | const bottle = await Bottle.findByBottleId(bottleId); |
| ... | @@ -49,7 +71,12 @@ exports.bottleDisconnect = async(ctx) => { | ... | @@ -49,7 +71,12 @@ exports.bottleDisconnect = async(ctx) => { |
| 49 | } | 71 | } |
| 50 | 72 | ||
| 51 | const hub = await Hub.findByHubId(bottle.getHubId()); | 73 | const hub = await Hub.findByHubId(bottle.getHubId()); |
| 52 | - const hosting = await hub.getHubHost(); | 74 | + if(hub.getHub_UserId() !== userId) { |
| 75 | + ctx.status = 403; | ||
| 76 | + return; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + const hosting = hub.getHubHost(); | ||
| 53 | 80 | ||
| 54 | const client = await Mqtt.mqttOn(hosting); | 81 | const client = await Mqtt.mqttOn(hosting); |
| 55 | const topic = 'bottle/' + bottleId + '/bts'; | 82 | const topic = 'bottle/' + bottleId + '/bts'; |
| ... | @@ -57,11 +84,19 @@ exports.bottleDisconnect = async(ctx) => { | ... | @@ -57,11 +84,19 @@ exports.bottleDisconnect = async(ctx) => { |
| 57 | 84 | ||
| 58 | await Bottle.deleteOne({ bottleId }); | 85 | await Bottle.deleteOne({ bottleId }); |
| 59 | 86 | ||
| 60 | - ctx.status = 200; | 87 | + ctx.status = 204; |
| 61 | 88 | ||
| 62 | }; | 89 | }; |
| 63 | 90 | ||
| 91 | +//약병 정보를 조회 -> 약병에 현재 데이터를 요청한다. message : req | ||
| 64 | exports.lookupInfo = async(ctx) => { | 92 | exports.lookupInfo = async(ctx) => { |
| 93 | + const token = ctx.cookies.get('access_token'); | ||
| 94 | + if(!token) { | ||
| 95 | + ctx.status = 401; | ||
| 96 | + return; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
| 65 | const { bottleId } = ctx.params; | 100 | const { bottleId } = ctx.params; |
| 66 | 101 | ||
| 67 | const bottle = await Bottle.findByBottleId(bottleId); | 102 | const bottle = await Bottle.findByBottleId(bottleId); |
| ... | @@ -70,12 +105,32 @@ exports.lookupInfo = async(ctx) => { | ... | @@ -70,12 +105,32 @@ exports.lookupInfo = async(ctx) => { |
| 70 | return; | 105 | return; |
| 71 | } | 106 | } |
| 72 | 107 | ||
| 108 | + const hub = await Hub.findByHubId(bottle.getHubId()); | ||
| 109 | + if(hub.getHub_UserId() !== userId) { | ||
| 110 | + ctx.status = 403; | ||
| 111 | + return; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + const hosting = hub.getHubHost(); | ||
| 115 | + //서버에서 bottle로 데이터를 요청한다. | ||
| 116 | + const client = await Mqtt.mqttOn(hosting); | ||
| 117 | + const topic = 'bottle/' + bottleId + '/stb'; | ||
| 118 | + const message = 'req'; | ||
| 119 | + Mqtt.mqttPublishMessage(client, { topic, message }); | ||
| 120 | + | ||
| 73 | ctx.status = 200; | 121 | ctx.status = 200; |
| 74 | ctx.body = bottle; | 122 | ctx.body = bottle; |
| 75 | } | 123 | } |
| 76 | 124 | ||
| 77 | //약병의 ID를 찾아서 약의 정보를 등록 : Post | 125 | //약병의 ID를 찾아서 약의 정보를 등록 : Post |
| 78 | exports.setMedicine = async(ctx) => { | 126 | exports.setMedicine = async(ctx) => { |
| 127 | + const token = ctx.cookies.get('access_token'); | ||
| 128 | + if(!token) { | ||
| 129 | + ctx.status = 401; | ||
| 130 | + return; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
| 79 | const { bottleId } = ctx.params; | 134 | const { bottleId } = ctx.params; |
| 80 | const { medicineId } = ctx.request.body; | 135 | const { medicineId } = ctx.request.body; |
| 81 | 136 | ||
| ... | @@ -85,6 +140,12 @@ exports.setMedicine = async(ctx) => { | ... | @@ -85,6 +140,12 @@ exports.setMedicine = async(ctx) => { |
| 85 | return; | 140 | return; |
| 86 | } | 141 | } |
| 87 | 142 | ||
| 143 | + const hub = await Hub.findByHubId(bottle.getHubId()); | ||
| 144 | + if(hub.getHub_UserId() !== userId) { | ||
| 145 | + ctx.status = 403; | ||
| 146 | + return; | ||
| 147 | + } | ||
| 148 | + | ||
| 88 | const medicine = await Medicine.findByMedicineId(medicineId); | 149 | const medicine = await Medicine.findByMedicineId(medicineId); |
| 89 | if(!medicine) { | 150 | if(!medicine) { |
| 90 | ctx.status = 404; | 151 | ctx.status = 404; | ... | ... |
| ... | @@ -2,8 +2,16 @@ | ... | @@ -2,8 +2,16 @@ |
| 2 | const Hub = require('../../models/hub'); | 2 | const Hub = require('../../models/hub'); |
| 3 | const Mqtt = require('../../lib/MqttModule'); | 3 | const Mqtt = require('../../lib/MqttModule'); |
| 4 | const DataProcess = require('../../lib/DataProcess'); | 4 | const DataProcess = require('../../lib/DataProcess'); |
| 5 | +const jwt = require('jsonwebtoken'); | ||
| 5 | 6 | ||
| 6 | exports.hubConnect = async (ctx) => { | 7 | exports.hubConnect = async (ctx) => { |
| 8 | + const token = ctx.cookies.get('access_token'); | ||
| 9 | + if(!token) { | ||
| 10 | + ctx.status = 401; | ||
| 11 | + return; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
| 7 | const { hubId, host, port } = ctx.request.body; | 15 | const { hubId, host, port } = ctx.request.body; |
| 8 | 16 | ||
| 9 | const isExistHub = await Hub.findByHubId(hubId); | 17 | const isExistHub = await Hub.findByHubId(hubId); |
| ... | @@ -21,16 +29,24 @@ exports.hubConnect = async (ctx) => { | ... | @@ -21,16 +29,24 @@ exports.hubConnect = async (ctx) => { |
| 21 | 29 | ||
| 22 | const hub = new Hub({ | 30 | const hub = new Hub({ |
| 23 | hubId, | 31 | hubId, |
| 24 | - hosting | 32 | + hosting, |
| 33 | + userId | ||
| 25 | }); | 34 | }); |
| 26 | 35 | ||
| 27 | await hub.save(); | 36 | await hub.save(); |
| 28 | 37 | ||
| 29 | - ctx.status = 200; | 38 | + ctx.status = 201; |
| 30 | ctx.body = hub; | 39 | ctx.body = hub; |
| 31 | }; | 40 | }; |
| 32 | 41 | ||
| 33 | exports.hubDisconnect = async(ctx) => { | 42 | exports.hubDisconnect = async(ctx) => { |
| 43 | + const token = ctx.cookies.get('access_token'); | ||
| 44 | + if(!token) { | ||
| 45 | + ctx.status = 401; | ||
| 46 | + return; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
| 34 | const { hubId } = ctx.params; | 50 | const { hubId } = ctx.params; |
| 35 | 51 | ||
| 36 | const hub = await Hub.findByHubId(hubId); | 52 | const hub = await Hub.findByHubId(hubId); |
| ... | @@ -38,11 +54,15 @@ exports.hubDisconnect = async(ctx) => { | ... | @@ -38,11 +54,15 @@ exports.hubDisconnect = async(ctx) => { |
| 38 | ctx.status = 404; | 54 | ctx.status = 404; |
| 39 | return; | 55 | return; |
| 40 | } | 56 | } |
| 57 | + if(hub.getHub_UserId() !== userId) { | ||
| 58 | + ctx.status = 403; | ||
| 59 | + return; | ||
| 60 | + } | ||
| 41 | 61 | ||
| 42 | const hosting = await hub.getHubHost(); | 62 | const hosting = await hub.getHubHost(); |
| 43 | Mqtt.mqttOff(hosting); | 63 | Mqtt.mqttOff(hosting); |
| 44 | 64 | ||
| 45 | await Hub.deleteOne({ hubId }); | 65 | await Hub.deleteOne({ hubId }); |
| 46 | 66 | ||
| 47 | - ctx.status = 200; | 67 | + ctx.status = 204; |
| 48 | }; | 68 | }; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | -//해당하는 약의 정보를 불러오거나, 약을 검색 | 1 | +//약의 정보를 검색하는 API : 약명, 제조사, 효능 |
| 2 | const Medicine = require('../../models/medicine'); | 2 | const Medicine = require('../../models/medicine'); |
| 3 | 3 | ||
| 4 | exports.medicineSearch = async(ctx) => { | 4 | exports.medicineSearch = async(ctx) => { |
| 5 | + const token = ctx.cookies.get('access_token'); | ||
| 6 | + if(!token) { | ||
| 7 | + ctx.status = 401; | ||
| 8 | + return; | ||
| 9 | + } | ||
| 10 | + | ||
| 5 | const { name, company, target } = ctx.request.body; | 11 | const { name, company, target } = ctx.request.body; |
| 6 | 12 | ||
| 7 | - let result = null; | 13 | + let result = []; |
| 8 | 14 | ||
| 9 | if (name && name !== '' && name !== undefined) | 15 | if (name && name !== '' && name !== undefined) |
| 10 | result = await medicineSearch_ByName(name); | 16 | result = await medicineSearch_ByName(name); | ... | ... |
-
Please register or login to post a comment