-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
74 lines (67 loc) · 1.7 KB
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const db = require('../models');
const util = require('../utilities');
const returnNew = { new: true };
const deactivate = { active: false };
async function create(req, res) {
try {
const data = req.body;
util.Error.validateExists(data.name);
const duplicate = await db.Event.findOne(data);
if (duplicate) {
util.Error.throwError(409);
}
const newEvent = await db.Event.create(data);
res.status(201).json(newEvent);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function search(req, res) {
try {
const query = req.query.query || '';
const options = req.query;
delete options.query;
const foundEvents = await db.Event
.fuzzySearch(query, options)
.select('name organization hosts performers model');
res.json(foundEvents);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function show(req, res) {
try {
const showEvent = await db.Event.findById(req.params.id);
res.json(showEvent);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function update(req, res) {
try {
const { id } = req.params;
const data = req.body;
util.Error.validateUUID(id);
const updateEvent = await db.Event.findByIdAndUpdate(id, data, returnNew);
res.json(updateEvent);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
async function cancel(req, res) {
try {
const { id } = req.params;
util.Error.validateUUID(id);
const deactivateEvent = await db.Event.findByIdAndUpdate(id, deactivate, returnNew);
res.json(deactivateEvent);
} catch (err) {
util.Error.handleErrors(err, res);
}
}
module.exports = {
create,
search,
show,
update,
cancel,
};