49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
'use strict'
|
|
|
|
const rest = require('midwest/middleware/rest')
|
|
const formatQuery = require('midwest/middleware/format-query')
|
|
const paginate = require('midwest/middleware/paginate')
|
|
|
|
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 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)
|
|
})
|
|
}
|
|
|
|
module.exports = Object.assign(rest(Pomodoro), {
|
|
create,
|
|
end,
|
|
formatQuery: formatQuery([ 'limit', 'sort' ], {
|
|
endDate: 'exists'
|
|
}),
|
|
getActive,
|
|
paginate: paginate(Pomodoro, 50)
|
|
})
|
|
|