express-ext 0.1.29 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -9,6 +9,7 @@ var GenericSearchController_1 = require("./GenericSearchController");
9
9
  exports.GenericSearchHandler = GenericSearchController_1.GenericSearchController;
10
10
  var HealthController_1 = require("./HealthController");
11
11
  exports.HealthHandler = HealthController_1.HealthController;
12
+ var http_1 = require("./http");
12
13
  var LoadController_1 = require("./LoadController");
13
14
  exports.LoadHandler = LoadController_1.LoadController;
14
15
  exports.ViewHandler = LoadController_1.LoadController;
@@ -77,23 +78,116 @@ var SavedController = (function () {
77
78
  this.item = item;
78
79
  this.id = (id && id.length > 0 ? id : 'id');
79
80
  this.save = this.save.bind(this);
81
+ this.unsave = this.unsave.bind(this);
80
82
  this.load = this.load.bind(this);
81
83
  }
82
84
  SavedController.prototype.save = function (req, res) {
85
+ var _this = this;
83
86
  var id = req.params[this.id];
84
87
  var itemId = req.params[this.item];
88
+ if (!id || id.length === 0) {
89
+ res.status(400).end("'" + this.id + "' cannot be empty");
90
+ return;
91
+ }
92
+ if (!itemId || itemId.length === 0) {
93
+ res.status(400).end("'" + this.item + "' cannot be empty");
94
+ return;
95
+ }
85
96
  this.service.save(id, itemId).then(function (data) {
86
97
  res.status(200).json(data).end();
87
98
  })
88
- .catch(function (err) { return console.log(err); });
99
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
100
+ };
101
+ SavedController.prototype.unsave = function (req, res) {
102
+ var _this = this;
103
+ var id = req.params[this.id];
104
+ var itemId = req.params[this.item];
105
+ if (!id || id.length === 0) {
106
+ res.status(400).end("'" + this.id + "' cannot be empty");
107
+ return;
108
+ }
109
+ if (!itemId || itemId.length === 0) {
110
+ res.status(400).end("'" + this.item + "' cannot be empty");
111
+ return;
112
+ }
113
+ this.service.unsave(id, itemId).then(function (data) {
114
+ res.status(200).json(data).end();
115
+ })
116
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
89
117
  };
90
118
  SavedController.prototype.load = function (req, res) {
119
+ var _this = this;
91
120
  var id = req.params[this.id];
121
+ if (!id || id.length === 0) {
122
+ res.status(400).end("'" + this.id + "' cannot be empty");
123
+ return;
124
+ }
92
125
  this.service.load(id).then(function (data) {
93
126
  res.status(200).json(data).send();
94
127
  })
95
- .catch(function (err) { return console.log(err); });
128
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
96
129
  };
97
130
  return SavedController;
98
131
  }());
99
132
  exports.SavedController = SavedController;
133
+ var FollowController = (function () {
134
+ function FollowController(log, service, target, id) {
135
+ this.log = log;
136
+ this.service = service;
137
+ this.target = target;
138
+ this.id = (id && id.length > 0 ? id : 'id');
139
+ this.follow = this.follow.bind(this);
140
+ this.unfollow = this.unfollow.bind(this);
141
+ this.checkfollow = this.checkfollow.bind(this);
142
+ }
143
+ FollowController.prototype.follow = function (req, res) {
144
+ var _this = this;
145
+ var id = req.params.id;
146
+ var target = req.params.target;
147
+ if (!id || id.length === 0) {
148
+ res.status(400).end("'" + this.id + "' cannot be empty");
149
+ return;
150
+ }
151
+ if (!target || target.length === 0) {
152
+ res.status(400).end("'" + this.target + "' cannot be empty");
153
+ return;
154
+ }
155
+ this.service.follow(id, target).then(function (count) {
156
+ return res.status(200).json(count).end();
157
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
158
+ };
159
+ FollowController.prototype.unfollow = function (req, res) {
160
+ var _this = this;
161
+ var id = req.params.id;
162
+ var target = req.params.target;
163
+ if (!id || id.length === 0) {
164
+ res.status(400).end("'" + this.id + "' cannot be empty");
165
+ return;
166
+ }
167
+ if (!target || target.length === 0) {
168
+ res.status(400).end("'" + this.target + "' cannot be empty");
169
+ return;
170
+ }
171
+ this.service.unfollow(id, target).then(function (count) {
172
+ return res.status(200).json(count).end();
173
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
174
+ };
175
+ FollowController.prototype.checkfollow = function (req, res) {
176
+ var _this = this;
177
+ var id = req.params.id;
178
+ var target = req.params.target;
179
+ if (!id || id.length === 0) {
180
+ res.status(400).end("'" + this.id + "' cannot be empty");
181
+ return;
182
+ }
183
+ if (!target || target.length === 0) {
184
+ res.status(400).end("'" + this.target + "' cannot be empty");
185
+ return;
186
+ }
187
+ this.service.checkFollow(id, target).then(function (count) {
188
+ return res.status(200).json(count).end();
189
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
190
+ };
191
+ return FollowController;
192
+ }());
193
+ exports.FollowController = FollowController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import {NextFunction, Request, Response} from 'express';
2
2
  import {GenericController} from './GenericController';
3
3
  import {GenericSearchController} from './GenericSearchController';
4
4
  import {HealthController} from './HealthController';
5
+ import {handleError, Log} from './http';
5
6
  import {LoadController} from './LoadController';
6
7
  import {LoadSearchController} from './LoadSearchController';
7
8
  import {LogController} from './LogController';
@@ -82,27 +83,117 @@ export function allow(access: AccessConfig): (req: Request, res: Response, next:
82
83
  export interface SavedService<T> {
83
84
  load(id: string): Promise<T[]>;
84
85
  save(id: string, itemId: string): Promise<number>;
86
+ unsave(id: string, itemId: string): Promise<number>;
85
87
  }
86
88
  export class SavedController<T> {
87
89
  constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
88
90
  this.id = (id && id.length > 0 ? id : 'id');
89
91
  this.save = this.save.bind(this);
92
+ this.unsave = this.unsave.bind(this);
90
93
  this.load = this.load.bind(this);
91
94
  }
92
95
  id: string;
93
96
  save(req: Request, res: Response) {
94
97
  const id = req.params[this.id];
95
98
  const itemId = req.params[this.item];
99
+ if (!id || id.length === 0) {
100
+ res.status(400).end(`'${this.id}' cannot be empty`);
101
+ return;
102
+ }
103
+ if (!itemId || itemId.length === 0) {
104
+ res.status(400).end(`'${this.item}' cannot be empty`);
105
+ return;
106
+ }
96
107
  this.service.save(id, itemId).then(data => {
97
108
  res.status(200).json(data).end();
98
109
  })
99
- .catch(err => console.log(err));
110
+ .catch(err => handleError(err, res, this.log));
111
+ }
112
+ unsave(req: Request, res: Response) {
113
+ const id = req.params[this.id];
114
+ const itemId = req.params[this.item];
115
+ if (!id || id.length === 0) {
116
+ res.status(400).end(`'${this.id}' cannot be empty`);
117
+ return;
118
+ }
119
+ if (!itemId || itemId.length === 0) {
120
+ res.status(400).end(`'${this.item}' cannot be empty`);
121
+ return;
122
+ }
123
+ this.service.unsave(id, itemId).then(data => {
124
+ res.status(200).json(data).end();
125
+ })
126
+ .catch(err => handleError(err, res, this.log));
100
127
  }
101
128
  load(req: Request, res: Response) {
102
129
  const id = req.params[this.id];
130
+ if (!id || id.length === 0) {
131
+ res.status(400).end(`'${this.id}' cannot be empty`);
132
+ return;
133
+ }
103
134
  this.service.load(id).then(data => {
104
135
  res.status(200).json(data).send();
105
136
  })
106
- .catch(err => console.log(err));
137
+ .catch(err => handleError(err, res, this.log));
138
+ }
139
+ }
140
+ export interface FollowService {
141
+ follow(id: string, target: string): Promise<number | undefined>;
142
+ unfollow(id: string, target: string): Promise<number>;
143
+ checkFollow(id: string, target: string): Promise<number>;
144
+ }
145
+ // tslint:disable-next-line:max-classes-per-file
146
+ export class FollowController {
147
+ constructor(public log: Log, public service: FollowService, public target: string, id: string) {
148
+ this.id = (id && id.length > 0 ? id : 'id');
149
+ this.follow = this.follow.bind(this);
150
+ this.unfollow = this.unfollow.bind(this);
151
+ this.checkfollow = this.checkfollow.bind(this);
152
+ }
153
+ id: string;
154
+ follow(req: Request, res: Response): void {
155
+ const id = req.params.id;
156
+ const target = req.params.target;
157
+ if (!id || id.length === 0) {
158
+ res.status(400).end(`'${this.id}' cannot be empty`);
159
+ return;
160
+ }
161
+ if (!target || target.length === 0) {
162
+ res.status(400).end(`'${this.target}' cannot be empty`);
163
+ return;
164
+ }
165
+ this.service.follow(id, target).then(count => {
166
+ return res.status(200).json(count).end();
167
+ }).catch(err => handleError(err, res, this.log));
168
+ }
169
+ unfollow(req: Request, res: Response): void {
170
+ const id = req.params.id;
171
+ const target = req.params.target;
172
+ if (!id || id.length === 0) {
173
+ res.status(400).end(`'${this.id}' cannot be empty`);
174
+ return;
175
+ }
176
+ if (!target || target.length === 0) {
177
+ res.status(400).end(`'${this.target}' cannot be empty`);
178
+ return;
179
+ }
180
+ this.service.unfollow(id, target).then(count => {
181
+ return res.status(200).json(count).end();
182
+ }).catch(err => handleError(err, res, this.log));
183
+ }
184
+ checkfollow(req: Request, res: Response): void {
185
+ const id = req.params.id;
186
+ const target = req.params.target;
187
+ if (!id || id.length === 0) {
188
+ res.status(400).end(`'${this.id}' cannot be empty`);
189
+ return;
190
+ }
191
+ if (!target || target.length === 0) {
192
+ res.status(400).end(`'${this.target}' cannot be empty`);
193
+ return;
194
+ }
195
+ this.service.checkFollow(id, target).then(count => {
196
+ return res.status(200).json(count).end();
197
+ }).catch(err => handleError(err, res, this.log));
107
198
  }
108
199
  }