From 8f9d4e3db3b214fb17ac3009d5de906548589799 Mon Sep 17 00:00:00 2001 From: frab1t <33671357+frab1t@users.noreply.github.com> Date: Sat, 6 Jul 2019 10:51:21 +0200 Subject: [PATCH] improvement: remove unused dependencies --- src/controllers/ticketAdmin.js | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/controllers/ticketAdmin.js diff --git a/src/controllers/ticketAdmin.js b/src/controllers/ticketAdmin.js new file mode 100644 index 0000000..4f3cb7c --- /dev/null +++ b/src/controllers/ticketAdmin.js @@ -0,0 +1,73 @@ +import moment from 'moment'; + +import ticketModel from '../models/tickets'; + +export const getAll = (req, res, next) => { + const { page, status, id } = req.query; + + const filter = {}; + + if (status) { + filter.status = status; + } + + if (id) { + filter.ticketId = id; + } + + const options = { + limit: 5, + page: Number.parseInt(page, 10) || 1, + sort: { created: -1 }, + }; + + + ticketModel.paginate(filter, options, (err, result) => { + if (err) { + next(err); + } + if (result.docs.length) { + res.json({ + status: 'success', + message: 'Tickets found', + currentPage: result.page, + totalPages: result.totalPages, + hasNextPage: result.hasNextPage, + totalDocs: result.totalDocs, + data: result.docs, + }); + } else { + res.json({ + success: false, + message: 'Tickets not found', + }); + } + }); +}; + +export const updateById = (req, res, next) => { + const { ticketId } = req.params; + ticketModel.findOneAndUpdate({ ticketId }, req.body, { new: true }, (err, ticket) => { + if (err) { + next(err); + } + res.json({ + status: 'success', + message: 'Ticket updated', + data: ticket, + }); + }); +}; + +export const deleteById = (req, res, next) => { + ticketModel.findByIdAndRemove(req.params.ticketId, (err, ticket) => { + if (err) { + next(err); + } + res.json({ + status: 'success', + message: 'Ticket deleted', + data: null, + }); + }); +};