박권수

feat. http status and authorization add

......@@ -30,7 +30,7 @@ exports.register = async(ctx) => {
await user.setPassword(password);
await user.save();
ctx.status = 200;
ctx.status = 201;
};
......@@ -66,7 +66,7 @@ exports.login = async(ctx) => {
maxAge : 1000 * 60 * 60 * 24 * 30
});
ctx.status = 201;
ctx.status = 200;
ctx.body = {
userId
};
......@@ -80,5 +80,4 @@ exports.logout = async(ctx) => {
});
ctx.status = 204;
ctx.body = null;
};
\ No newline at end of file
......
......@@ -3,14 +3,18 @@ const Bottle = require('../../models/bottle');
const Hub = require('../../models/hub');
const Medicine = require('../../models/medicine');
const Mqtt = require('../../lib/MqttModule');
const jwt = require('jsonwebtoken');
//약병 등록
exports.bottleConnect = async(ctx) => {
const { bottleId, hubId } = ctx.request.body;
const token = ctx.cookies.get('access_token');
if(!token) {
ctx.status = 401;
return;
}
const newBottle = new Bottle({
bottleId,
hubId
});
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const { bottleId, hubId } = ctx.request.body;
const isExistBottle = await Bottle.findByBottleId(bottleId);
if(isExistBottle) {
......@@ -23,23 +27,41 @@ exports.bottleConnect = async(ctx) => {
ctx.status = 404;
return;
}
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = await hub.getHubHost();
const hosting = hub.getHubHost();
if(!hosting) {
ctx.status = 404;
return;
}
const newBottle = new Bottle({
bottleId,
hubId
});
const client = await Mqtt.mqttOn(hosting);
const topic = 'bottle/' + bottleId + '/bts';
const topic = 'bottle/' + newBottle.getBottleId() + '/bts';
Mqtt.mqttSubscribe(client, topic);
await newBottle.save();
ctx.status = 200;
ctx.status = 201;
};
//약병 등록 해제
exports.bottleDisconnect = 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 { bottleId } = ctx.params;
const bottle = await Bottle.findByBottleId(bottleId);
......@@ -49,7 +71,12 @@ exports.bottleDisconnect = async(ctx) => {
}
const hub = await Hub.findByHubId(bottle.getHubId());
const hosting = await hub.getHubHost();
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = hub.getHubHost();
const client = await Mqtt.mqttOn(hosting);
const topic = 'bottle/' + bottleId + '/bts';
......@@ -57,11 +84,19 @@ exports.bottleDisconnect = async(ctx) => {
await Bottle.deleteOne({ bottleId });
ctx.status = 200;
ctx.status = 204;
};
//약병 정보를 조회 -> 약병에 현재 데이터를 요청한다. message : req
exports.lookupInfo = 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 { bottleId } = ctx.params;
const bottle = await Bottle.findByBottleId(bottleId);
......@@ -70,12 +105,32 @@ exports.lookupInfo = async(ctx) => {
return;
}
const hub = await Hub.findByHubId(bottle.getHubId());
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = hub.getHubHost();
//서버에서 bottle로 데이터를 요청한다.
const client = await Mqtt.mqttOn(hosting);
const topic = 'bottle/' + bottleId + '/stb';
const message = 'req';
Mqtt.mqttPublishMessage(client, { topic, message });
ctx.status = 200;
ctx.body = bottle;
}
//약병의 ID를 찾아서 약의 정보를 등록 : Post
exports.setMedicine = 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 { bottleId } = ctx.params;
const { medicineId } = ctx.request.body;
......@@ -85,6 +140,12 @@ exports.setMedicine = async(ctx) => {
return;
}
const hub = await Hub.findByHubId(bottle.getHubId());
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const medicine = await Medicine.findByMedicineId(medicineId);
if(!medicine) {
ctx.status = 404;
......
......@@ -2,8 +2,16 @@
const Hub = require('../../models/hub');
const Mqtt = require('../../lib/MqttModule');
const DataProcess = require('../../lib/DataProcess');
const jwt = require('jsonwebtoken');
exports.hubConnect = 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 { hubId, host, port } = ctx.request.body;
const isExistHub = await Hub.findByHubId(hubId);
......@@ -21,16 +29,24 @@ exports.hubConnect = async (ctx) => {
const hub = new Hub({
hubId,
hosting
hosting,
userId
});
await hub.save();
ctx.status = 200;
ctx.status = 201;
ctx.body = hub;
};
exports.hubDisconnect = 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 { hubId } = ctx.params;
const hub = await Hub.findByHubId(hubId);
......@@ -38,11 +54,15 @@ exports.hubDisconnect = async(ctx) => {
ctx.status = 404;
return;
}
if(hub.getHub_UserId() !== userId) {
ctx.status = 403;
return;
}
const hosting = await hub.getHubHost();
Mqtt.mqttOff(hosting);
await Hub.deleteOne({ hubId });
ctx.status = 200;
ctx.status = 204;
};
\ No newline at end of file
......
//해당하는 약의 정보를 불러오거나, 약을 검색
//약의 정보를 검색하는 API : 약명, 제조사, 효능
const Medicine = require('../../models/medicine');
exports.medicineSearch = async(ctx) => {
const token = ctx.cookies.get('access_token');
if(!token) {
ctx.status = 401;
return;
}
const { name, company, target } = ctx.request.body;
let result = null;
let result = [];
if (name && name !== '' && name !== undefined)
result = await medicineSearch_ByName(name);
......