-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisks.ts
95 lines (88 loc) · 2.62 KB
/
risks.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
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
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
interface Risk {
id: number;
description: string;
probability: number;
impact: number;
status: string;
createdAt: string;
createdBy: string;
updatedAt?: string;
updatedBy?: string;
}
let risks: Risk[] = [];
let currentRiskId = 1;
export async function handleRiskRequest(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname === "/api/risks" && req.method === "GET") {
return new Response(JSON.stringify(risks), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (pathname === "/api/risks" && req.method === "POST") {
const body = await req.json();
const { description, probability, impact, status, createdBy } = body;
const newRisk = {
id: currentRiskId++,
description,
probability,
impact,
status,
createdAt: new Date().toISOString(),
createdBy,
};
risks.push(newRisk);
return new Response(JSON.stringify(newRisk), {
status: 201,
headers: { "Content-Type": "application/json" }
});
}
if (pathname.startsWith("/api/risks/")) {
const id = parseInt(pathname.replace("/api/risks/", ""));
if (!isNaN(id)) {
const risk = risks.find(r => r.id === id);
if (!risk) {
return new Response(JSON.stringify({ message: "Risco não encontrado" }), {
status: 404,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "GET") {
return new Response(JSON.stringify(risk), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "PUT") {
const body = await req.json();
const { description, probability, impact, status, updatedBy } = body;
Object.assign(risk, {
description, probability, impact, status, updatedAt: new Date().toISOString(),
updatedBy
});
return new Response(JSON.stringify(risk), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "DELETE") {
const index = risks.findIndex((r) => r.id === id);
if (index !== -1) {
risks.splice(index, 1);
return new Response(JSON.stringify({ message: "Risco deletado" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
}
}
}
return serveDir(req, {
fsRoot: ".",
urlRoot: "",
showDirListing: true,
enableCors: true,
});
}