77 lines
1.5 KiB
JavaScript
77 lines
1.5 KiB
JavaScript
'use strict'
|
|
|
|
const _ = require('lodash')
|
|
|
|
const formatQuery = require('warepot/format-query')
|
|
|
|
const Pomodoro = require('./model')
|
|
|
|
function create(req, res, next) {
|
|
Pomodoro.create(Object.assign(req.body, {
|
|
user: req.user && req.user.id || '57b04f50a1eaaf354f3b96a6',
|
|
ip: req.ip,
|
|
userAgent: req.headers['user-agent']
|
|
}), (err, pomodoro) => {
|
|
res.locals.pomodoro = pomodoro
|
|
|
|
next(err)
|
|
})
|
|
}
|
|
|
|
function find(req, res, next) {
|
|
const limit = Math.max(0, req.query.limit) || res.locals.limit
|
|
|
|
const query = Pomodoro.find(_.omit(req.query, 'limit', 'sort', 'page'),
|
|
null,
|
|
{ sort: req.query.sort || '-startDate', lean: true })
|
|
|
|
if (limit)
|
|
query.limit(limit)
|
|
|
|
query.exec((err, pomodoros) => {
|
|
res.locals.pomodoros = pomodoros
|
|
|
|
next(err)
|
|
})
|
|
}
|
|
|
|
function end(req, res, next) {
|
|
Pomodoro.findByIdAndUpdate(req.params.id, { endDate: new Date() }, (err, pomodoro) => {
|
|
if (err) return next(err)
|
|
|
|
res.locals.pomodoro = pomodoro
|
|
|
|
next()
|
|
})
|
|
}
|
|
|
|
function getActive(req, res, next) {
|
|
Pomodoro.find({ user: req.user.id, endDate: { $eq: null } }, (err, pomodoros) => {
|
|
res.locals.pomodoros = pomodoros
|
|
|
|
return next(err)
|
|
})
|
|
}
|
|
|
|
function patch(req, res, next) {
|
|
Pomodoro.findByIdAndUpdate(req.params.id, req.body, (err, pomodoro) => {
|
|
if (err) return next(err)
|
|
|
|
res.locals.pomodoro = pomodoro
|
|
|
|
next()
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
create,
|
|
end,
|
|
find,
|
|
formatQuery: formatQuery([ 'limit', 'sort' ], {
|
|
endDate: 'exists'
|
|
}),
|
|
getActive,
|
|
patch
|
|
}
|
|
|