backend: drop and modify

This commit is contained in:
muerwre 2019-03-07 10:54:24 +07:00
parent 17a929956c
commit 7513f79b93
7 changed files with 81 additions and 2 deletions

View file

@ -27,6 +27,7 @@ app.use(lessMiddleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, PATCH');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});

View file

@ -2,11 +2,15 @@ const express = require('express');
const post = require('./route/post');
const get = require('./route/get');
const list = require('./route/list');
const drop = require('./route/drop');
const patch = require('./route/patch');
const router = express.Router();
router.post('/', post);
router.get('/', get);
router.patch('/', patch);
router.delete('/', drop);
router.get('/list', list);
module.exports = router;

View file

@ -0,0 +1,19 @@
const { User, Route } = require('../../models');
module.exports = async (req, res) => {
const { body: { id, token, address } } = req;
const owner = await User.findOne({ _id: id, token }).populate('routes');
if (!owner) return res.send({ success: false, reason: 'unauthorized' });
const exists = await Route.findOne({ _id: address }).populate('owner', '_id');
if (!exists) return res.send({ success: false, mode: 'not_exists' });
if (exists && exists.owner._id !== id) return res.send({ success: false, mode: 'not_yours' });
exists.delete();
return res.send({ success: true, address });
};

View file

@ -0,0 +1,24 @@
const { User, Route } = require('../../models');
const { parseString } = require('../../utils/parse');
module.exports = async (req, res) => {
const { body, body: { id, token, address } } = req;
const owner = await User.findOne({ _id: id, token }).populate('routes');
if (!owner) return res.send({ success: false, reason: 'unauthorized' });
const title = parseString(body.title, 32);
const is_public = !!body.is_public;
const exists = await Route.findOne({ _id: address }).populate('owner', '_id');
if (!exists) return res.send({ success: false, mode: 'not_exists' });
if (exists && exists.owner._id !== id) return res.send({ success: false, mode: 'not_yours' });
exists.set({ title, is_public }).save();
return res.send({ success: true, ...exists });
};