starred content

This commit is contained in:
muerwre 2019-03-21 17:58:38 +07:00
parent a920217959
commit baa41f707d
19 changed files with 159 additions and 22 deletions

View file

@ -13,6 +13,7 @@ const RouteSchema = new Schema(
owner: { type: Schema.Types.ObjectId, ref: 'User' },
distance: { type: Number, default: 0 },
is_public: { type: Boolean, default: false },
is_starred: { type: Boolean, default: false },
is_deleted: { type: Boolean, default: false },
created_at: { type: Date, default: Date.now() },
updated_at: { type: Date, default: Date.now() },

View file

@ -4,9 +4,11 @@ const get = require('./route/get');
const list = require('./route/list');
const drop = require('./route/drop');
const patch = require('./route/patch');
const star = require('./route/star');
const router = express.Router();
router.post('/star', star);
router.post('/', post);
router.get('/', get);
router.patch('/', patch);

View file

@ -3,10 +3,12 @@ const { Route, User } = require('../../models');
module.exports = async (req, res) => {
const {
query: {
id, token, title, distance, author, step = 20, shift = 0,
id, token, title, distance, author, step = 20, shift = 0, starred,
}
} = req;
const is_starred = parseInt(starred, 10) === 1;
const user = await User.findOne({ _id: id, token });
let criteria = { is_deleted: false };
@ -14,13 +16,21 @@ module.exports = async (req, res) => {
if (title) {
criteria = {
...criteria,
$or: [
$and: [
{ title: new RegExp(title.trim(), 'ig') },
{ _id: new RegExp(title.trim(), 'ig') },
],
};
}
console.log('is starred?', starred);
if (is_starred) {
criteria = {
...criteria,
is_starred: true,
};
}
if (!author || !user || (user._id !== author)) {
criteria = {
...criteria,
@ -32,7 +42,7 @@ module.exports = async (req, res) => {
{
...criteria,
},
'_id title distance owner updated_at is_public is_deleted',
'_id title distance owner updated_at is_public is_deleted is_starred',
{
limit: 9000,
sort: { updated_at: -1 },

View file

@ -17,7 +17,7 @@ module.exports = async (req, res) => {
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();
await exists.set({ title, is_public }).save();
return res.send({ success: true, ...exists });
};

View file

@ -27,7 +27,7 @@ module.exports = async (req, res) => {
if (exists && !force) return res.send({ success: false, mode: 'overwriting' });
if (exists) {
exists.set({
await exists.set({
title, route, stickers, logo, distance, updated_at: Date.now(), provider, is_public,
}).save();

View file

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