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 });
};

View file

@ -7,4 +7,7 @@ export const API: { [x: string]: string } = {
GET_MAP: `${CLIENT.API_ADDR}/route`,
POST_MAP: `${CLIENT.API_ADDR}/route`,
GET_ROUTE_LIST: `${CLIENT.API_ADDR}/route/list`,
DROP_ROUTE: `${CLIENT.API_ADDR}/route`,
MODIFY_ROUTE: `${CLIENT.API_ADDR}/route/modify`,
};

View file

@ -3,9 +3,9 @@ import { delay, SagaIterator } from 'redux-saga';
import { takeLatest, select, call, put, takeEvery, race, take } from 'redux-saga/effects';
import {
checkIframeToken, checkOSRMService,
checkUserToken,
checkUserToken, dropRoute,
getGuestToken, getRouteList,
getStoredMap,
getStoredMap, modifyRoute,
postMap
} from '$utils/api';
import {
@ -647,6 +647,20 @@ function* mapsLoadMoreSaga() {
yield put(searchSetLoading(false));
}
function* dropRouteSaga({ _id }: ReturnType<typeof ActionCreators.dropRoute>): SagaIterator {
const { id, token } = yield select(getUser);
const result = yield call(dropRoute, { address: _id, id, token });
console.log('result', result);
}
function* modifyRouteSaga({ _id, title, is_public }: ReturnType<typeof ActionCreators.modifyRoute>): SagaIterator {
const { id, token } = yield select(getUser);
const result = yield call(modifyRoute, { address: _id, id, token, title, is_public });
console.log('result', result);
}
export function* userSaga() {
yield takeLatest(REHYDRATE, authCheckSaga);
yield takeEvery(ACTIONS.SET_MODE, setModeSaga);
@ -691,4 +705,7 @@ export function* userSaga() {
yield takeLatest(ACTIONS.GET_GPX_TRACK, getGPXTrackSaga);
yield takeLatest(ACTIONS.MAPS_LOAD_MORE, mapsLoadMoreSaga);
yield takeLatest(ACTIONS.DROP_ROUTE, dropRouteSaga);
yield takeLatest(ACTIONS.MODIFY_ROUTE, modifyRouteSaga);
}

View file

@ -101,3 +101,14 @@ export const getRouteList = ({
export const checkOSRMService = (bounds: LatLngLiteral[]): Promise<boolean> => (
CLIENT && CLIENT.OSRM_URL && axios.get(CLIENT.OSRM_TEST_URL(bounds)).then(() => true).catch(() => false)
);
export const dropRoute = ({ address, id, token }: { address: string, id: string, token: string }): AxiosPromise<any> => (
axios.delete(API.DROP_ROUTE, { data: { address: '1123123123123', id, token } })
);
export const modifyRoute = (
{ address, id, token, title, is_public }:
{ address: string, id: string, token: string, title: string, is_public: boolean }
): AxiosPromise<any> => (
axios.patch(API.DROP_ROUTE, { address, id, token, title, is_public })
);