-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.ts
More file actions
103 lines (94 loc) · 2.83 KB
/
processes.ts
File metadata and controls
103 lines (94 loc) · 2.83 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
import { serveDir } from "https://deno.land/std@0.224.0/http/file_server.ts";
interface Process {
id: number;
name: string;
version: string;
objective: string;
owner: string;
users: string[];
status: string;
createdAt: string;
createdBy: string;
updatedAt?: string;
updatedBy?: string;
diagramXML?: string;
}
let processes: Process[] = [];
let currentProcessId = 1;
export async function handleProcessRequest(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname === "/api/processes" && req.method === "GET") {
return new Response(JSON.stringify(processes), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (pathname === "/api/processes" && req.method === "POST") {
const body = await req.json();
const { name, version, objective, owner, users, status, diagramXML } = body;
const newProcess = {
id: currentProcessId++,
name,
version,
objective,
owner,
users,
status,
createdAt: new Date().toISOString(),
createdBy: owner,
diagramXML
};
processes.push(newProcess);
return new Response(JSON.stringify(newProcess), {
status: 201,
headers: { "Content-Type": "application/json" }
});
}
if (pathname.startsWith("/api/processes/")) {
const id = parseInt(pathname.replace("/api/processes/", ""));
if (!isNaN(id)) {
const process = processes.find(p => p.id === id);
if (!process) {
return new Response(JSON.stringify({ message: "Processo não encontrado" }), {
status: 404,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "GET") {
return new Response(JSON.stringify(process), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "PUT") {
const body = await req.json();
const { name, version, objective, owner, users, status, diagramXML } = body;
Object.assign(process, {
name, version, objective, owner, users, status, updatedAt: new Date().toISOString(),
updatedBy: owner, diagramXML
});
return new Response(JSON.stringify(process), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (req.method === "DELETE") {
const index = processes.findIndex((p) => p.id === id);
if (index !== -1) {
processes.splice(index, 1);
return new Response(JSON.stringify({ message: "Processo deletado" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
}
}
}
return serveDir(req, {
fsRoot: ".",
urlRoot: "",
showDirListing: true,
enableCors: true,
});
}