-
Notifications
You must be signed in to change notification settings - Fork 0
/
concern.js
136 lines (125 loc) · 4.24 KB
/
concern.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
const express = require('express');
const _ = require('lodash');
const mongoose = require('mongoose');
const ConcernController = require('./concern-controller.js');
const AutomovilModel = require('./concern-model.js');
const Controller = new ConcernController({});
const router = express.Router();
const Concern = function Concern(service) {
this.service = service;
};
Concern.prototype.routes = function concernRoutes() {
router.get('/automoviles', (req, res) => {
Controller
.findAll({
fieldsToFetch: req.fieldsToFetch,
recordsPerPage: parseInt(req.query.records_per_page, 10)
})
.then((automoviles) => {
if (automoviles.length === 0) {
res.status(404).send(automoviles);
} else {
res.status(200).send(automoviles);
}
})
.catch((reason) => {
this.service.log.error(`${this.service.name}: error when serving ${JSON.stringify(req.method)} ${req.path}`);
this.service.log.error(`${this.service.name}: ${reason}`);
res.status(500).send({ error: true, result: null, msg: reason });
});
});
router.get('/automoviles/:id', (req, res) => {
Controller
.findOne({
fieldsToFetch: req.fieldsToFetch,
recordsPerPage: parseInt(req.query.records_per_page, 10),
automovilId: req.params.id
})
.then((automovil) => {
if (automovil === null) {
res.status(404).send(automovil);
} else {
res.status(200).send(automovil);
}
})
.catch((reason) => {
this.service.log.error(`${this.service.name}: error when serving ${JSON.stringify(req.method)} ${req.path}`);
this.service.log.error(`${this.service.name}: ${reason}`);
res.status(500).send({ error: true, result: null, msg: reason });
});
});
router.post('/automoviles', (req, res) => {
Controller
.addNew({
automovilValues: req.body
})
.then((savedautomovil) => {
res.status(201).send(savedautomovil);
})
.catch((reason) => {
this.service.log.error(`${this.service.name}: error when serving ${JSON.stringify(req.method)} ${req.path}`);
this.service.log.error(`${this.service.name}: ${reason}`);
const errorBody = {
errors: [{
source: { pointer: `http://${process.env.GATEWAY_HOST}:8080/automoviles` },
detail: reason.errors
}]
};
res.status(400).send(errorBody);
});
});
router.patch('/automoviles/:id', (req, res) => {
if (!req.params.id || !mongoose.Types.ObjectId.isValid(req.params.id)) {
res.status(422).send({
errors: [{
source: { pointer: `http://${process.env.GATEWAY_HOST}:8080/automoviles` },
detail: '_missing_data_member_in_document_body_'
}]
});
} else if (!req.body) {
const errorBody = {
errors: [{
source: { pointer: `http://${process.env.GATEWAY_HOST}:8080/automoviles` },
detail: '_missing_data_member_in_document_body_'
}]
};
res.status(422).send(errorBody);
} else {
Controller
.patch({
automovilValues: req.body,
automovilId: req.params.id
})
.then((savedautomovil) => {
res.status(200).send();
})
.catch((reason) => {
this.service.log.error(`${this.service.name}: error when serving ${JSON.stringify(req.method)} ${req.path}`);
this.service.log.error(`${this.service.name}: ${reason}`);
const errorBody = {
errors: [{
source: { pointer: `http://${process.env.GATEWAY_HOST}:8080/automoviles` },
detail: reason.errors
}]
};
res.status(500).send(errorBody);
});
}
});
router.delete('/automoviles/:id', (req, res) => {
Controller.delete({ _id: mongoose.Types.ObjectId(req.params.id) })
.then((result) => {
res.status(200).send({ deleteResult: result });
})
.catch((reason) => {
res.send({
errors: [{
source: { pointer: `http://${process.env.GATEWAY_HOST}:8080/automoviles` },
detail: reason.errors[0].message
}]
});
});
});
return router;
};
module.exports = Concern;