-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgrantsSavedSearch.js
82 lines (66 loc) · 2.32 KB
/
grantsSavedSearch.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
75
76
77
78
79
80
81
82
const express = require('express');
const router = express.Router({ mergeParams: true });
const db = require('../db');
const { requireUser } = require('../lib/access-helpers');
router.get('/', requireUser, async (req, res) => {
const { user } = req.session;
const paginationParams = {
currentPage: req.query.currentPage || 1,
perPage: req.query.perPage || 10,
isLengthAware: req.query.isLengthAware || true,
};
const savedSearches = await db.getSavedSearches(user.id, paginationParams);
res.json(savedSearches);
});
router.post('/', requireUser, async (req, res) => {
const { user } = req.session;
try {
const result = await db.createSavedSearch({
name: req.body.name,
userId: user.id,
criteria: req.body.criteria,
});
res.json(result);
} catch (e) {
if (e.constraint && e.constraint.includes('grants_saved_searches_name_created_by_idx')) {
console.warn(e);
res.status(400).send(`Title '${req.body.name}' already exists`);
return;
}
console.error(e);
res.status(500).send('Unable to create saved search. Please reach out to [email protected]');
}
});
router.put('/:searchId', requireUser, async (req, res) => {
const { user } = req.session;
try {
const result = await db.updateSavedSearch({
id: req.params.searchId,
name: req.body.name,
userId: user.id,
criteria: req.body.criteria,
});
res.json(result);
} catch (e) {
console.error(e);
res.status(500).send('Unable to update saved search. Please reach out to [email protected]');
}
});
router.delete('/:searchId', requireUser, async (req, res) => {
const { searchId } = req.params;
const { user } = req.session;
let deleteSuccess = false;
try {
deleteSuccess = await db.deleteSavedSearch(searchId, user.id);
} catch (e) {
console.error(`Error deleting saved search: ${e}`);
res.status(500).send('Error deleting saved search');
return;
}
if (deleteSuccess) {
res.status(200).json({ status: 'OK' });
} else {
res.status(404).send('Could not find the saved search');
}
});
module.exports = router;