mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 11:06:40 +07:00
backend: populated routes
This commit is contained in:
parent
723ab51eef
commit
6ffdca22d7
5 changed files with 25 additions and 9 deletions
|
@ -4,7 +4,13 @@ const { generateGuest, generateRandomUrl } = require('./guest');
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const { id, token } = req.query;
|
const { id, token } = req.query;
|
||||||
|
|
||||||
const user = await User.findOne({ _id: id, token });
|
const user = await User.findOne({ _id: id, token }).populate({
|
||||||
|
path: 'routes',
|
||||||
|
options: {
|
||||||
|
limit: 100,
|
||||||
|
sort: { updated: 1 },
|
||||||
|
}
|
||||||
|
});
|
||||||
const random_url = await generateRandomUrl();
|
const random_url = await generateRandomUrl();
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
const { User, Route } = require('../../models');
|
const { Route } = require('../../models');
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const { query: { id, token, name } } = req;
|
const { query: { name } } = req;
|
||||||
|
|
||||||
if (!name) return res.send({ success: false, mode: 'not_found_1' });
|
if (!name) return res.send({ success: false, mode: 'not_found_1' });
|
||||||
|
|
||||||
const exists = await Route.findOne({ _id: name }).populate('owner');
|
const exists = await Route.findOne({ _id: name }).populate('owner', '_id');
|
||||||
|
|
||||||
if (!exists) return res.send({ success: false, mode: 'not_found_2' });
|
if (!exists) return res.send({ success: false, mode: 'not_found_2' });
|
||||||
const data = exists.toObject();
|
const data = exists.toObject();
|
||||||
|
|
|
@ -5,7 +5,7 @@ const { parseRoute, parseStickers, parseString, parseNumber } = require('../../u
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const { body, body: { id, token, force } } = req;
|
const { body, body: { id, token, force } } = req;
|
||||||
|
|
||||||
const owner = await User.findOne({ _id: id, token });
|
const owner = await User.findOne({ _id: id, token }).populate('routes');
|
||||||
if (!owner) return res.send({ success: false, reason: 'unauthorized', id, token });
|
if (!owner) return res.send({ success: false, reason: 'unauthorized', id, token });
|
||||||
|
|
||||||
const title = parseString(body.title, 32);
|
const title = parseString(body.title, 32);
|
||||||
|
@ -19,7 +19,7 @@ module.exports = async (req, res) => {
|
||||||
return res.send({ success: false, mode: 'empty' });
|
return res.send({ success: false, mode: 'empty' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const exists = await Route.findOne({ _id: address }).populate('owner');
|
const exists = await Route.findOne({ _id: address }).populate('owner', '_id');
|
||||||
|
|
||||||
if (exists && exists.owner._id !== id) return res.send({ success: false, mode: 'exists' });
|
if (exists && exists.owner._id !== id) return res.send({ success: false, mode: 'exists' });
|
||||||
if (exists && !force) return res.send({ success: false, mode: 'overwriting' });
|
if (exists && !force) return res.send({ success: false, mode: 'overwriting' });
|
||||||
|
@ -32,10 +32,13 @@ module.exports = async (req, res) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await Route.create({
|
const created = await Route.create({
|
||||||
_id: address, title, route, stickers, owner, logo, distance,
|
_id: address, title, route, stickers, owner, logo, distance,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await owner.routes.push(created);
|
||||||
|
await owner.save();
|
||||||
|
|
||||||
return res.send({
|
return res.send({
|
||||||
success: true, title, address, route, stickers
|
success: true, title, address, route, stickers
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,7 +8,7 @@ export const DEFAULT_USER = {
|
||||||
place_types: {},
|
place_types: {},
|
||||||
random_url: '',
|
random_url: '',
|
||||||
role: ROLES.guest,
|
role: ROLES.guest,
|
||||||
routes: [],
|
routes: {},
|
||||||
success: false,
|
success: false,
|
||||||
id: null,
|
id: null,
|
||||||
token: null,
|
token: null,
|
||||||
|
|
|
@ -2,9 +2,16 @@ import axios from 'axios/index';
|
||||||
|
|
||||||
import { API } from '$constants/api';
|
import { API } from '$constants/api';
|
||||||
|
|
||||||
|
const arrayToObject = (array, key) => array.reduce((obj, el) => ({ ...obj, [el[key]]: el }));
|
||||||
|
|
||||||
export const checkUserToken = ({ id, token }) => axios.get(API.CHECK_TOKEN, {
|
export const checkUserToken = ({ id, token }) => axios.get(API.CHECK_TOKEN, {
|
||||||
params: { id, token }
|
params: { id, token }
|
||||||
}).then(result => (result && result.data && { ...result.data, id, token }));
|
}).then(result => (result && result.data && {
|
||||||
|
...result.data,
|
||||||
|
id,
|
||||||
|
token,
|
||||||
|
routes: (result.data.routes && result.data.routes.length > 0 && arrayToObject(result.data.routes, '_id')) || {},
|
||||||
|
}));
|
||||||
|
|
||||||
export const getGuestToken = () => axios.get(API.GET_GUEST).then(result => (result && result.data));
|
export const getGuestToken = () => axios.get(API.GET_GUEST).then(result => (result && result.data));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue