-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.module.ts
More file actions
193 lines (164 loc) · 3.12 KB
/
posts.module.ts
File metadata and controls
193 lines (164 loc) · 3.12 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {
IsOptional,
IsString,
MinLength,
} from "class-validator";
import {
Body,
Controller,
Delete,
Get,
Injectable,
Middleware,
Module,
NotFoundException,
Param,
Post,
Put,
Returns,
Route,
Summary,
Tags,
} from "../../src/index.js";
import {
DatabaseModule,
DatabaseService,
} from "./database.module.js";
import { rateLimiter } from "./server.js";
import {
UsersModule,
UsersService,
} from "./users.module.js";
// --- Service ---
@Injectable()
export class PostsService {
constructor(
private readonly db: DatabaseService,
private readonly usersService: UsersService,
) {}
findAll() {
void this.db;
void this.usersService;
return [];
}
findById(_id: string) {
return null;
}
create(_data: { title: string; body: string; authorId: string; }) {
return { id: "1" };
}
update(_id: string, _data: object) {
return {};
}
remove(_id: string) {}
}
// --- Middleware ---
function validateOwnership(_req: Request, next: () => Promise<Response>) {
return next();
}
// --- Controller ---
class PostParams {
@IsString()
id!: string;
}
class CreatePost {
@IsString()
@MinLength(1)
title!: string;
@IsString()
@MinLength(1)
body!: string;
@IsString()
authorId!: string;
}
class UpdatePost {
@IsOptional()
@IsString()
@MinLength(1)
title?: string;
@IsOptional()
@IsString()
@MinLength(1)
body?: string;
@IsOptional()
@IsString()
authorId?: string;
}
class PostRecord {
@IsString()
id!: string;
@IsString()
title!: string;
@IsString()
body!: string;
}
class ErrorResponse {
@IsString()
message!: string;
}
@Route("/posts")
@Tags("Posts")
@Middleware(rateLimiter)
export class PostsController extends Controller {
constructor(private readonly postsService: PostsService) {
super();
}
@Get()
@Summary("List all posts")
@Returns(200, [PostRecord], "Posts list")
list() {
return this.postsService.findAll();
}
@Get("/:id")
@Summary("Get post by ID")
@Returns(200, PostRecord, "The post")
@Returns(404, ErrorResponse, "Not found")
getById(@Param(PostParams) params: PostParams) {
const post = this.postsService.findById(params.id);
if (!post) {
throw new NotFoundException("Post not found");
}
return post;
}
@Post()
@Middleware(validateOwnership)
@Summary("Create post")
@Returns(201, PostRecord, "Created post")
create(@Body(CreatePost) body: CreatePost) {
this.setStatus(201);
return {
id: crypto.randomUUID(),
title: body.title,
body: body.body,
};
}
@Put("/:id")
@Middleware(validateOwnership)
@Summary("Update post")
@Returns(200, PostRecord, "Updated post")
update(
@Param(PostParams) params: PostParams,
@Body(UpdatePost) body: UpdatePost,
) {
return {
id: params.id,
title: body.title ?? "Updated title",
body: body.body ?? "Updated body",
};
}
@Delete("/:id")
@Summary("Delete post")
@Returns(204, undefined, "Deleted")
remove(@Param(PostParams) params: PostParams) {
this.postsService.remove(params.id);
this.setStatus(204);
return undefined;
}
}
// --- Module ---
@Module({
imports: [UsersModule, DatabaseModule],
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {}