smooth moves
This commit is contained in:
parent
413aa0f63c
commit
ea1e1a91a3
7 changed files with 159 additions and 3 deletions
19
routes/api/index.js
Normal file
19
routes/api/index.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
const verifyAuth = require('../../middlewares/verifyAuth');
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = ({ db, config }) => {
|
||||
const router = express.Router();
|
||||
|
||||
router.use(verifyAuth())
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send({
|
||||
api: true
|
||||
})
|
||||
})
|
||||
|
||||
router.use('/wishlist', require('./wishlist')({ db }));
|
||||
|
||||
return router;
|
||||
}
|
42
routes/api/wishlist/index.js
Normal file
42
routes/api/wishlist/index.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
const verifyAuth = require('../../../middlewares/verifyAuth')
|
||||
const express = require('express')
|
||||
const path = require('path')
|
||||
|
||||
module.exports = ({ db, config }) => {
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send({
|
||||
route: 'wishlist'
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/:user/:id/move/:direction', async (req, res) => {
|
||||
try {
|
||||
if (req.user._id !== req.params.user) return res.json({ error: 'Not correct user' })
|
||||
const doc = await db.get(req.user._id)
|
||||
const wishlist = doc.wishlist
|
||||
if (req.params.direction === 'up') wishlist.reverse()
|
||||
let moveFromIndex
|
||||
wishlist.forEach(wish => {
|
||||
if (wish.id === req.params.id) return moveFromIndex = wishlist.indexOf(wish)
|
||||
})
|
||||
const moveToIndex = wishlist.findIndex(wish => {
|
||||
return ( wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id )
|
||||
})
|
||||
if (moveToIndex < 0 || moveToIndex > wishlist.length) return res.send({ error: 'Invalid move '})
|
||||
const original = wishlist[moveToIndex]
|
||||
wishlist[moveToIndex] = wishlist[moveFromIndex]
|
||||
wishlist[moveFromIndex] = original
|
||||
if (req.params.direction === 'up') wishlist.reverse()
|
||||
doc.wishlist = wishlist
|
||||
await db.put(doc)
|
||||
res.send({ error: false })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.send({ error: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue