-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
44 lines (34 loc) · 1.09 KB
/
app.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
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import { RegisterUser } from "./application/RegisterUser.ts";
import { UserRepository } from "./domain/UserRepository.ts";
export function createApp(userRepository: UserRepository) {
const app = new Application();
const router = new Router();
const registerUser = new RegisterUser(userRepository);
router.post("/register", async (ctx) => {
const body = await ctx.request.body({ type: "json" }).value;
try {
await registerUser.execute(
body.name,
body.email,
body.password,
body.age
);
} catch (error) {
if (error.message === "User already exists") {
ctx.response.status = 409;
ctx.response.body = { message: error.message };
return;
}
if (error.message === "Invalid email") {
ctx.response.status = 400;
ctx.response.body = { message: error.message };
return;
}
throw error;
}
ctx.response.body = { message: "User registered successfully" };
});
app.use(router.routes());
return app;
}