Skip to content

Commit 0739ec8

Browse files
author
Ben Hanna
committed
Updated new implementation.
1 parent dc8dba0 commit 0739ec8

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

handler.js

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
const HttpStatusError = require('./errors/HttpStatusError');
1+
const _ = require('lodash');
22

33
class Handler {
4-
constructor(model, options = { limit: 50, offset: 0 }) {
5-
this.model = model;
6-
this.limit = options.limit;
7-
this.offset = options.offset;
4+
constructor(Model) {
5+
this.Model = Model;
6+
}
7+
8+
async create(req, res, next) {
9+
try {
10+
const row = await this.Model.create(req.body);
11+
res.send(row);
12+
} catch (error) {
13+
next(error);
14+
}
815
}
916

1017
async get(req, res, next) {
1118
try {
12-
const data = await this.findOne(req.params);
13-
res.send(data);
19+
const row = await this.findOne(req.params);
20+
res.send(row);
1421
} catch (error) {
1522
next(error);
1623
}
1724
}
1825

1926
async destroy(req, res, next) {
2027
try {
21-
const data = await this.findOne(req.params);
22-
await data.destroy();
28+
const row = await this.findOne(req.params);
29+
await row.destroy();
2330
res.sendStatus(204);
2431
} catch (error) {
2532
next(error);
@@ -28,46 +35,44 @@ class Handler {
2835

2936
async update(req, res, next) {
3037
try {
31-
const data = await this.findOne(req, res, next);
32-
const result = await data.updateAttributes(req.body);
33-
res.send(result);
38+
const row = await this.findOne(req, res, next);
39+
const updated = await row.updateAttributes(req.body);
40+
res.send(updated);
3441
} catch (error) {
3542
next(error);
3643
}
3744
}
3845

39-
async findOne(params, options = { where: {} }) {
40-
const { rawAttributes } = this.model;
41-
42-
for (let key in params) {
43-
if (params.hasOwnProperty(key) && rawAttributes.hasOwnProperty(key)) {
44-
options[key] = params[key];
46+
async query(req, res, next) {
47+
try {
48+
const { rows, start, end, count } = await this.findAndCountAll(req.query);
49+
50+
res.set('Content-Range', `${start}-${end}/${count}`);
51+
52+
if (count > end) {
53+
res.status(206);
54+
} else {
55+
res.status(200);
4556
}
46-
}
47-
48-
const data = await this.model.findOne(options);
4957

50-
if (!data) {
51-
throw new HttpStatusError(404, 'Not Found');
58+
res.send(rows);
59+
} catch (error) {
60+
next(error);
5261
}
62+
}
63+
64+
async findOne(params, options = { where: {} }) {
65+
options.where = _.merge(this.filter(params), options.where);
5366

54-
return data;
67+
const row = await this.Model.findOne(options);
68+
69+
return row;
5570
}
5671

5772
async findAndCountAll(params, options = { where: {} }) {
58-
const { fields, limit = this.limit, offset = this.offset, sort, ...filters } = params;
59-
const { rawAttributes } = this.model;
73+
const { fields, limit = 50, offset = 0, sort, ...filters } = params;
6074

61-
for (let key in filters) {
62-
if (filters.hasOwnProperty(key) && rawAttributes.hasOwnProperty(key)) {
63-
const value = filters[key];
64-
try {
65-
options.where[key] = JSON.parse(value);
66-
} catch (error) {
67-
options.where[key] = value.split(',');
68-
}
69-
}
70-
}
75+
options.where = _.merge(this.filter(filters), options.where);
7176

7277
if (fields) {
7378
options.attributes = fields.split(',');
@@ -85,25 +90,27 @@ class Handler {
8590
});
8691
}
8792

88-
const { count, rows } = await this.Mode.findAndCountAll(options);
93+
const { count, rows } = await this.Model.findAndCountAll(options);
8994

90-
return {
91-
start: options.offset,
92-
end: Math.max(count, options.offset + offset.limit),
93-
count,
94-
rows
95-
};
95+
const start = offset;
96+
const end = Math.max(count, offset + limit);
97+
98+
return { rows, start, end, count };
9699
}
97100

98-
async query(req, res, next) {
99-
try {
100-
const { start, end, count, rows } = await this.findAndCountAll(req.query);
101-
102-
res.set('Content-Range', `${start}-${end}/${count}`);
103-
res.status(end === count ? 200 : 206);
104-
res.send(rows);
105-
} catch (error) {
106-
next(error);
107-
}
101+
filter(params, filters = {}) {
102+
const { rawAttributes } = this.Model;
103+
104+
_.forOwn(params, (value, key) => {
105+
if (rawAttributes.hasOwnProperty(key)) {
106+
try {
107+
filters[key] = JSON.parse(value);
108+
} catch (error) {
109+
filters[key] = value.split(',');
110+
}
111+
}
112+
});
113+
114+
return filters;
108115
}
109116
}

0 commit comments

Comments
 (0)