Repository navigation
Expand file tree
/
Copy pathServerController.ts
More file actions
180 lines (146 loc) · 4.14 KB
/
Copy pathServerController.ts
File metadata and controls
180 lines (146 loc) · 4.14 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
import { Content, Redirect } from "alosaur/mod.ts";
import { Essentials } from "dent/ts-types/mod.ts";
import {
Document,
DocumentCollection,
DocumentKeySelector,
} from "dent/object-doc/mod.ts";
import { ServerConfig } from "../Configuration/ServerConfig.ts";
interface Transform<T> {
(config: T): T;
}
export abstract class ServerController {
constructor(protected readonly config: ServerConfig) {}
protected async getAll<T extends Document>(
collection: DocumentCollection<T>,
transform: Transform<T>
) {
try {
const items = await collection.all();
return this.OK(items.map((item) => transform(item)));
} catch (error) {
return this.NoContent([]);
}
}
protected async getDocument<T extends Document>(
collection: DocumentCollection<T>,
document_id: string,
transform: Transform<T>
) {
try {
const item = await collection.get(document_id);
if (item === null) {
return this.NotFound({ document_id });
}
return this.OK(transform(item));
} catch {
return this.InternalServerError({ document_id });
}
}
protected async createDocument<T extends Document>(
collection: DocumentCollection<T>,
document: Essentials.DeepPartial<T>,
document_key: DocumentKeySelector<T>,
transform: Transform<T>
) {
try {
if (document.name) {
const existing = await collection.get(document.name);
if (existing === null) {
const created = await collection.update(document, document_key);
return this.Created(transform(created));
}
return this.Conflict(transform(existing));
}
} catch {
return this.NotAcceptable(document);
}
}
protected async deleteDocument<T extends Document>(
collection: DocumentCollection<T>,
document_id: string
) {
try {
if ((await collection.get(document_id)) === null) {
return this.NotFound({ document_id });
}
await collection.delete(document_id);
return this.OK({ document_id });
} catch {
return this.InternalServerError({ document_id });
}
}
protected async patchDocument<T extends Document>(
collection: DocumentCollection<T>,
document: Essentials.DeepPartial<T>,
document_id: string,
transform: Transform<T>
) {
try {
const item = await collection.get(document_id);
if (item === null) {
return this.NotFound(document);
}
return this.Accepted(transform(item));
} catch {
return this.InternalServerError(document);
}
}
protected async putDocument<T extends Document>(
collection: DocumentCollection<T>,
document: Essentials.DeepPartial<T>,
document_id: string,
document_key: DocumentKeySelector<T>,
transform: Transform<T>
) {
try {
const item = await collection.get(document_id);
if (item === null) {
const updated = await collection.update(document, document_key);
return this.Created(transform(updated));
}
return this.Accepted(transform(item));
} catch {
return this.InternalServerError(document);
}
}
protected Accepted<T>(resposne: T) {
return Content(resposne, 202);
}
protected BadRequest<T>(response: T) {
return Content(response, 400);
}
protected Conflict<T>(response: T) {
return Content(response, 409);
}
protected Created<T>(resposne: T) {
return Content(resposne, 201);
}
protected Forbidden<T>(response: T) {
return Content(response, 403);
}
protected InternalServerError<T>(response: T) {
return Content(response, 500);
}
protected MovedPermanently(redirect: string) {
return Redirect(redirect);
}
protected NotAcceptable<T>(response: T) {
return Content(response, 406);
}
protected NoContent<T>(response: T) {
return Content(response, 204);
}
protected NotFound<T>(response: T) {
return Content(response, 404);
}
protected PartialContent<T>(response: T) {
return Content(response, 206);
}
protected OK<T>(response: T) {
return Content(response, 200);
}
protected Unauthorized<T>(response: T) {
return Content(response, 401);
}
}