This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.ts
144 lines (125 loc) · 5.61 KB
/
webserver.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
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
import { Application, Context, Router, send } from "https://deno.land/x/[email protected]/mod.ts";
import { Session } from "https://deno.land/x/[email protected]/mod.ts";
import { Product, ShoppingCart } from "./classes/types.ts";
const app = new Application();
const router = new Router();
const session = new Session({ framework: "oak" });
await session.init();
const products:Product[] = JSON.parse(await Deno.readTextFile(`frontend/assets/products.json`));
router
//HTML
.get("/", async (context) => {
await send(context, "/frontend/html/index.html");
})
.get("/product/:id", async (context) => {
await send(context, "/frontend/html/index.html");
})
.get("/shoppingcart", async (context) => {
await send(context, "/frontend/html/index.html");
})
.get("/checkout", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
let cart:ShoppingCart = await context.state.session.get("shoppingcart");
if (cart.products.length == 0) {
context.response.redirect("/shoppingcart");
}
await send(context, "frontend/html/index.html");
})
.get("/successfullorder", async (context) => {
await send(context, "frontend/html/index.html");
})
.get("/unsuccessfullorder", async (context) => {
await send(context, "frontend/html/index.html");
})
//Assets
.get("/assets/:folder/:file", async (context) => {
await send(context, "/frontend/assets/" + context.params.folder + "/" + context.params.file);
})
.get("/jsassets/:folder/:file", async (context) => {
await send(context, "/frontend/" + context.params.folder + "/" + context.params.file);
})
//API
.get("/api/products", async (context) => {
context.response.body = products;
})
.post("/api/shoppingcart", async (context) => {
let cart:ShoppingCart = await context.request.body({ type: "json" }).value;
await context.state.session.set("shoppingcart", cart);
context.response.status = 200
context.response.body = { message: 'OK' }
})
.get("/api/shoppingcart", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
context.response.body = await context.state.session.get("shoppingcart");
context.response.status = 200;
})
.post("/api/shoppingcart/add/:id", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
let shoppingcart:ShoppingCart = await context.state.session.get("shoppingcart");
const product = products.find(x => Number(x.id) === Number(context.params.id));
if (!product) {
context.response.status = 404;
return;
}
shoppingcart.products.push(product);
context.response.status = 200;
})
.post("/api/shoppingcart/remove/:id", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
let shoppingcart:ShoppingCart = await context.state.session.get("shoppingcart");
const product = products.find(x => Number(x.id) === Number(context.params.id));
if (!product) {
context.response.status = 404;
return;
}
let index = shoppingcart.products.indexOf(product);
shoppingcart.products.splice(index, 1);
context.response.status = 200;
})
.post("/api/order", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
let cart:ShoppingCart = await context.state.session.get("shoppingcart");
if (cart.products.length == 0) {
context.response.redirect("/shoppingcart");
}
const body = await context.request.body({ type: "form"})
const values = await body.value;
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const firstname:string|null = values.get("prename");
const lastname:string|null = values.get("lastname");
const email:string|null = values.get("email");
let isinvalid:boolean = false;
if (!(firstname && lastname && email)) {
isinvalid = true;
} else {
isinvalid = re.test(String(email).toLowerCase());
}
if (isinvalid) {
context.response.status = 200;
context.response.redirect("/unsuccessfullorder");
}
context.cookies.delete("sid");
context.response.status = 200;
context.response.redirect("/successfullorder");
})
.get("/api/totalprice", async (context) => {
if (await context.state.session.get("shoppingcart") == undefined) {
await context.state.session.set("shoppingcart", new ShoppingCart());
}
let shoppingcart:ShoppingCart = await context.state.session.get("shoppingcart");
context.response.body = shoppingcart.getTotalPrice();
})
app.use(<any>session.use()(session));
app.use(router.routes());
app.listen({ port: 8000 });
console.log("Server running on http://localhost:8000");