-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacronymController.js
203 lines (180 loc) · 4.73 KB
/
acronymController.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { sequelize, Acronym } from "./dbConnector";
import { Op } from "sequelize";
import { handleSequelizeError, capitalizeFirstLetter } from "./utils";
export function welcome(req, res) {
const help = `
<pre>
Welcome to the World Texting Foundation, also known as WTF.
Use an Authorization header to work with your own data:
i.e: fetch(url, { headers: { 'Authorization': 'whatever-you-want' }})
The following endpoints are available:
- GET /acronym?from=50&limit=10&search=:search
- GET /acronym/:acronym
- GET /random/:count?
- POST /acronym
- PUT /acronym/:acronym
- DELETE /acronym/:acronym
</pre>
`;
res.status(200).send(help);
}
export function findMultiple(req, res) {
const { from, limit, search } = req.query;
if (!search || !limit || !from) {
res.send({
error: "You must provide: 'from', 'limit' and 'search' in your request.",
example: "/acronym?from=1&limit=10&search=YO",
});
}
Acronym.findAndCountAll({
where: {
[Op.or]: [
{
acronym: {
[Op.like]: `%${capitalizeFirstLetter(search)}%`,
},
},
{
acronym: {
[Op.like]: `%${search.toUpperCase()}%`,
},
},
],
},
limit: parseInt(limit),
offset: parseInt(from),
}).then((results) => {
const moreResults = results.rows.length <= 1 ? "False" : "True";
let nextUrl;
if (moreResults === 'False') {
nextUrl = 'None'
} else {
nextUrl = `${req.protocol}://${req.get('host')}/acronym?from=${parseInt(from) + 1}&limit=${limit}&search=${search}`
}
// Set response headers 'next' to indicate if there are more results
res.set({
moreResults: moreResults,
});
// Also set it in the response object
res.json({
...results,
moreResults,
nextUrl: nextUrl
});
});
}
export function findSingle(req, res) {
const { acronym } = req.params;
if (!acronym) {
res.send({});
}
Acronym.findOne({
where: {
[Op.or]: [
{
acronym: {
[Op.eq]: acronym.toUpperCase(),
},
},
{
acronym: {
[Op.like]: `${capitalizeFirstLetter(acronym)}%`,
},
},
{
acronym: {
[Op.like]: `${acronym.toUpperCase()}%`,
},
},
],
},
})
.then((results) => {
res.json(results);
})
.catch((err) => {
const errorMessage = handleSequelizeError(err);
res.status(403).send(errorMessage);
});
}
export function findRandom(req, res) {
const { count } = req.params;
Acronym.findAll({
limit: count,
order: sequelize.random(),
})
.then((acronyms) => {
res.json(acronyms);
})
.catch((err) => {
const errorMessage = handleSequelizeError(err);
res.status(403).send(errorMessage);
});
}
export function addSingle(req, res) {
const { acronym, definition } = req.body;
/*
https://en.wikipedia.org/wiki/Acronym#Case
Apparently there are many conventions regarding the casing,
so we're not enforcing or transforming anything here.
*/
Acronym.create({
acronym: acronym,
definition: definition,
})
.then((result) => {
res.status(200).send(result);
})
.catch((err) => {
const errorMessage = handleSequelizeError(err);
res.status(403).send(errorMessage);
});
}
export function modifySingle(req, res) {
const { acronym, definition } = req.body;
if (!acronym) {
res
.status(403)
.send("You must supply a valid (existing) acronym to update");
next();
} else if (!definition || definition.length < 1) {
res.status(403).send("Definition must be at least two charcters long");
next();
}
Acronym.update(
{ definition: capitalizeFirstLetter(definition) },
{
where: {
acronym: {
[Op.eq]: acronym.toUpperCase() || capitalizeFirstLetter(acronym),
},
},
}
)
.then((affectedRows) => {
res.status(200).send(`Successfully updated ${affectedRows}.`);
})
.catch((err) => {
const errorMessage = handleSequelizeError(err);
res.status(403).send(errorMessage);
});
}
export function deleteSingle(req, res) {
const { acronym } = req.body;
if (!acronym) {
res
.status(403)
.send("You must supply a valid (existing) acronym to delete");
next();
}
Acronym.destroy({
where: { acronym: { [Op.eq]: acronym.toUpperCase() || "" } },
})
.then((affectedRows) => {
res.status(200).send(`Successfully deleted ${affectedRows}.`);
})
.catch((err) => {
const errorMessage = handleSequelizeError(err);
res.status(403).send(errorMessage);
});
}