From 7513f79b9311dadee51b2851013afc3b21771cb5 Mon Sep 17 00:00:00 2001 From: muerwre Date: Thu, 7 Mar 2019 10:54:24 +0700 Subject: [PATCH] backend: drop and modify --- backend/app.js | 1 + backend/routes/route.js | 4 ++++ backend/routes/route/drop.js | 19 +++++++++++++++++++ backend/routes/route/patch.js | 24 ++++++++++++++++++++++++ src/constants/api.ts | 3 +++ src/redux/user/sagas.ts | 21 +++++++++++++++++++-- src/utils/api.ts | 11 +++++++++++ 7 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 backend/routes/route/drop.js create mode 100644 backend/routes/route/patch.js diff --git a/backend/app.js b/backend/app.js index 556530f..b85af36 100644 --- a/backend/app.js +++ b/backend/app.js @@ -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(); }); diff --git a/backend/routes/route.js b/backend/routes/route.js index f9976bb..65c49a3 100644 --- a/backend/routes/route.js +++ b/backend/routes/route.js @@ -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; diff --git a/backend/routes/route/drop.js b/backend/routes/route/drop.js new file mode 100644 index 0000000..d875361 --- /dev/null +++ b/backend/routes/route/drop.js @@ -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 }); +}; + diff --git a/backend/routes/route/patch.js b/backend/routes/route/patch.js new file mode 100644 index 0000000..756627a --- /dev/null +++ b/backend/routes/route/patch.js @@ -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 }); +}; + diff --git a/src/constants/api.ts b/src/constants/api.ts index e429aa8..f8b8b05 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -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`, }; diff --git a/src/redux/user/sagas.ts b/src/redux/user/sagas.ts index 74ecfc2..5e04735 100644 --- a/src/redux/user/sagas.ts +++ b/src/redux/user/sagas.ts @@ -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): 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): 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); } diff --git a/src/utils/api.ts b/src/utils/api.ts index d6be1ca..702a525 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -101,3 +101,14 @@ export const getRouteList = ({ export const checkOSRMService = (bounds: LatLngLiteral[]): Promise => ( 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 => ( + 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 => ( + axios.patch(API.DROP_ROUTE, { address, id, token, title, is_public }) +);