Skip to content

Commit 73b59e0

Browse files
Updates for controller inheritance sample to highlight the benefits of unified CRUD operations extended from a base controller (template method pattern)
1 parent 11dbf79 commit 73b59e0

File tree

11 files changed

+186
-81
lines changed

11 files changed

+186
-81
lines changed

sample/sample17-controllers-inheritance/app.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ useExpressServer(app, {
88
});
99
app.listen(3001); // run express app
1010

11-
console.log("Express server is running on port 3001. Open http://localhost:3001/blogs/ or http://localhost:3002/posts/");
11+
console.log(
12+
"Possible GET endpoints you may see from a browser",
13+
"http://localhost:3001/article",
14+
"http://localhost:3001/article/1000",
15+
"http://localhost:3001/product",
16+
"http://localhost:3001/product/1000",
17+
"http://localhost:3001/category",
18+
"http://localhost:3001/category/1000",
19+
);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {Res} from "../../../src/decorator/Res";
2+
import {Put} from "../../../src/decorator/Put";
3+
import {Post} from "../../../src/decorator/Post";
4+
import {Param} from "../../../src/decorator/Param";
5+
import {Get} from "../../../src/decorator/Get";
6+
import {Delete} from "../../../src/decorator/Delete";
7+
import {Body} from "../../../src/decorator/Body";
8+
9+
import {MockedRepository} from "../repository/MockedRepository";
10+
import {IInstance} from "../interface/IInstance";
11+
12+
export abstract class AbstractControllerTemplate {
13+
protected domain: string;
14+
protected repository: MockedRepository;
15+
16+
@Post()
17+
public async create(
18+
@Body() payload: any,
19+
@Res() res: any
20+
): Promise<{}> {
21+
const item = await this.repository.create(payload);
22+
23+
res.status(201);
24+
res.location(`/${this.domain}/${item.id}`);
25+
26+
return {};
27+
}
28+
29+
@Put("/:id")
30+
public async updated(
31+
@Param("id") id: number,
32+
@Body() payload: any,
33+
@Res() res: any
34+
): Promise<{}> {
35+
await this.repository.update(id, payload);
36+
res.status(204);
37+
38+
return {};
39+
}
40+
41+
@Get("/:id")
42+
public read(
43+
@Param("id") id: number,
44+
@Res() res: any
45+
): Promise<IInstance> {
46+
return this.repository.find(id);
47+
}
48+
49+
@Get()
50+
public readCollection(
51+
@Res() res: any
52+
): Promise<IInstance[]> {
53+
return this.repository.getCollection();
54+
}
55+
56+
@Delete("/:id")
57+
public async delete(
58+
@Param("id") id: number,
59+
@Res() res: any
60+
): Promise<{}> {
61+
await this.repository.delete(id);
62+
63+
return {};
64+
}
65+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Controller} from "../../../src/decorator/Controller";
2+
import {AbstractControllerTemplate} from "./AbstractContollerTemplate";
3+
import {MockedRepository} from "../repository/MockedRepository";
4+
5+
const domain = "article";
6+
7+
@Controller(`/${domain}`)
8+
export class ArticleController extends AbstractControllerTemplate {
9+
protected constructor() {
10+
super();
11+
12+
this.domain = domain;
13+
this.repository = new MockedRepository(domain);
14+
}
15+
}

sample/sample17-controllers-inheritance/controllers/BaseController.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

sample/sample17-controllers-inheritance/controllers/BlogController.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Controller} from "../../../src/decorator/Controller";
2+
import {AbstractControllerTemplate} from "./AbstractContollerTemplate";
3+
import {MockedRepository} from "../repository/MockedRepository";
4+
5+
const domain = "category";
6+
7+
@Controller(`/${domain}`)
8+
export class CategoryController extends AbstractControllerTemplate {
9+
protected constructor() {
10+
super();
11+
12+
this.domain = domain;
13+
this.repository = new MockedRepository(domain);
14+
}
15+
}

sample/sample17-controllers-inheritance/controllers/PostController.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Controller} from "../../../src/decorator/Controller";
2+
import {AbstractControllerTemplate} from "./AbstractContollerTemplate";
3+
import {MockedRepository} from "../repository/MockedRepository";
4+
5+
const domain = "product";
6+
7+
@Controller(`/${domain}`)
8+
export class CategoryController extends AbstractControllerTemplate {
9+
protected constructor() {
10+
super();
11+
12+
this.domain = domain;
13+
this.repository = new MockedRepository(domain);
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IInstance {
2+
id: number;
3+
type: string;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface IPayload {
2+
id: number;
3+
}

0 commit comments

Comments
 (0)