-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.ts
54 lines (49 loc) · 1.89 KB
/
routes.ts
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
import { Application, Request, Response } from 'express';
import { ArticleController, HealthStatusController, UserController } from './lib/controllers';
class Route {
path: string;
action: (request: Request, response: Response) => {};
verb: RestVerb;
constructor(verb: RestVerb, path: string, action: (request: Request, response: Response) => {}) {
this.path = path;
this.action = action;
this.verb = verb;
}
}
enum RestVerb {
show = 'get',
index = 'get',
create = 'post',
edit = 'patch',
update = "put",
delete = "delete"
}
export class Routes {
private app: Application;
routes: Array<Route> = [];
private controllers = {
HealthStatusController: new HealthStatusController(),
ArticleController: new ArticleController(),
UserController: new UserController()
}
constructor(app: Application) {
this.app = app;
}
static call(app: any) {
const instance = new Routes(app);
instance.call();
}
call() {
this.routes.push(new Route(RestVerb.show, '/api/v1/status(||/0)', this.controllers.HealthStatusController.show));
this.routes.push(new Route(RestVerb.show, '/article/:id', this.controllers.ArticleController.show));
this.routes.push(new Route(RestVerb.create, '/article', this.controllers.ArticleController.create));
this.routes.push(new Route(RestVerb.update, '/article/:id', this.controllers.ArticleController.update));
this.routes.push(new Route(RestVerb.create, '/newUpdates', this.controllers.ArticleController.newUpdates));
this.routes.push(new Route(RestVerb.show, '/user/:id', this.controllers.UserController.show));
this.routes.push(new Route(RestVerb.create, '/user', this.controllers.UserController.create));
this.routes.push(new Route(RestVerb.update, '/user/:id', this.controllers.UserController.update));
this.routes.forEach((route) => {
this.app[route.verb](route.path, route.action);
});
}
}