forked from premku70/swagger-mongoose-crud
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparam.controller.js
166 lines (145 loc) · 5.84 KB
/
param.controller.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
var _ = require('lodash');
var ObjectID = require('mongoose').Types.ObjectId;
var util = require('util');
var CrudController = require('./crud.controller');
/**
* Constructor function for ParamController.
* @classdesc Controller for basic CRUD operations on mongoose models.
* Using a route parameter object (request property) if possible.
* The parameter name is passed as the third argument.
* @constructor
* @inherits CrudController
* @param {Object} model - The mongoose model to operate on
* @param {String} [idName] - The name of the id request parameter to use,
* defaults to the lowercase model name with 'Id' appended.
* @param {String} [paramName] - The name of the request property to use,
* defaults to the lowercase model name with 'Param' appended.
* @param {Object} logger - logger instance
*/
function ParamController(model, idName, logger, defaultFilter, permanentDeleteData) {
var modelName = model.modelName.toLowerCase();
var paramName = modelName + 'Param';
// call super constructor
CrudController.call(this, model, logger, defaultFilter, permanentDeleteData);
// only set param if it is set, will default to 'id'
if (!paramName) {
paramName = modelName + 'Param';
}
this.paramName = String(paramName);
this.paramString = ':' + this.paramName;
this.logger = logger;
// register param name route parameter
// if (typeof router === 'function') {
// router.param(this.paramName, this.registerRequestParameter.bind(this));
// }
}
var ParamPrototype = {
/**
* Set our own constructor property for instanceof checks
* @private
*/
constructor: ParamController,
/**
* The route parameter name
* @default
*/
paramName: undefined,
/**
* Flag indicating that a mongo db id is used as a parameter. The parameter
* function will check for a valid mongo id then.
* @default
*/
mongoId: true,
/**
* Get a single document, returning the request
* property named {@link ParamController#paramName}.
* @param {IncomingMessage} req - The request message object
* @param {ServerResponse} res - The outgoing response object
* @returns {ServerResponse} A single document or NOT FOUND if no document has been found
*/
// show: function (req, res) {
// if (req[this.paramName]) {
// return res.ok(this.getResponseObject(req[this.paramName]));
// }
// return res.notFound();
// },
/**
* Updates an existing document in the DB using the request
* property named {@link ParamController#paramName}.
* @param {IncomingMessage} req - The request message object
* @param {ServerResponse} res - The outgoing response object
* @returns {ServerResponse} The updated document or NOT FOUND if no document has been found
*/
// update: function (req, res) {
// if (req.body._id) {
// delete req.body._id;
// }
// var self = this;
// var bodyData = _.omit(req.body, this.omit);
// var updated = _.merge(req[this.paramName], bodyData);
// updated.save(function (err) {
// if (err) {
// return res.handleError(err);
// }
// req[self.paramName] = updated;
// return res.ok(self.getResponseObject(updated));
// });
// },
// /**
// * Deletes an document from the DB using the request
// * property named {@link ParamController#paramName}.
// * @param {IncomingMessage} req - The request message object
// * @param {ServerResponse} res - The outgoing response object
// * @returns {ServerResponse} The response status 201 CREATED or an error response
// */
// destroy: function (req, res) {
// var self = this;
// req[this.paramName].remove(function (err) {
// if (err) {
// return res.handleError(err);
// }
// delete req[self.paramName];
// return res.noContent();
// });
// },
// /**
// * Register the default id parameter for route requests.
// * Add a property to the current request which is the
// * document returned by the controller Model for the param configures
// * paramter name available in the processed request.
// * @param {http.IncomingMessage} req - The request message object
// * @param {http.ServerResponse} res - The outgoing response object
// * @param next {function} - The next handler function to call when done
// * @param id {String} - The id parameter parsed from the current request
// * @returns {function} This function sets a status of 400 for malformed MongoDB
// * id's and a status of 404 if no document has been found for the passed
// * parameter value. Calls the passed next function when done.
// */
// registerRequestParameter: function (req, res, next, id) {
// var self = this;
// // check if a custom id is used, when not only process a valid object id
// if (this.mongoId && !ObjectID.isValid(id)) {
// res.badRequest();
// return next();
// }
// // attach the document as this.paramName to the request
// this.model.findOne({'_id': id}, function (err, doc) {
// if (err) {
// return next(err);
// }
// if (!doc) {
// res.notFound();
// return next('route');
// }
// req[self.paramName] = doc;
// return next();
// });
// }
};
// set properties explicit on ParamController.prototype to allow doc generation
ParamController.prototype = _.create(CrudController.prototype, ParamPrototype);
/**
* The CrudController for basic CRUD functionality on Mongoose models
* @type {ParamController}
*/
module.exports = ParamController;