-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
146 lines (128 loc) · 3.25 KB
/
index.js
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
const fastify = require("fastify")({ logger: true });
const rateLimit = require('@fastify/rate-limit');
const helmet = require('@fastify/helmet');
const authPlugin = require("./authPlugin");
const items = [
{
id: 1,
name: "Cartable",
price: 4,
},
];
const users = [
{
mail: 'emeric',
password: 'superP4ss'
},
{
mail: 'solo',
password: 'soloP4ss'
}
];
fastify.register(authPlugin, {
users: users
});
fastify.register(rateLimit, {
max: 10,
timeWindow: '1 minute'
});
fastify.register(helmet, {
contentSecurityPolicy: false
});
// Declare a route
fastify.get("/", async () => {
return `hello ${request.user}`;
});
fastify.get("/products", async () => {
return items;
});
fastify.get("/products/:id", async (request, reply) => {
const id = parseInt(request.params.id, 10);
const desiredItem = items.find((item) => item.id === id);
if (desiredItem) {
return desiredItem;
} else {
reply.code(404).send("not found");
}
});
fastify.put("/products/:id", async (request, reply) => {
const id = parseInt(request.params.id, 10)
if (!request.body.name || !request.body.price) {
reply.code(400).send("Product must have a name and a price");
} else {
const existingPorductKey = items.findIndex((item) => item.id === id);
if (existingPorductKey != -1) {
items[existingPorductKey] = {
id: id,
name: request.body.name,
price: request.body.price,
}
return reply
.code(201)
.send(`Product ${request.body.name} has been updated`);
} else {
items.push({
id: id,
name: request.body.name,
price: request.body.price,
});
return reply
.code(201)
.send(`Product ${request.body.name} has been created`);
}
}
});
fastify.post("/products/new-product", async (request, reply) => {
const lastItem = items[items.length - 1];
const id = lastItem.id + 1;
if (!request.body.name || !request.body.price) {
reply.code(400).send("Product must have a name and a price");
} else {
items.push({
id: id,
name: request.body.name,
price: request.body.price,
});
return reply
.code(201)
.send(`Product ${request.body.name} has been created`);
}
});
fastify.patch("/products/:id", async (request, reply) => {
const id = parseInt(request.params.id, 10);
const price = parseInt(request.body.newPrice, 10);
const existingPorductKey = items.findIndex((item) => item.id === id);
if (existingPorductKey != -1) {
items[existingPorductKey].price = price;
return reply
.code(200)
.send(`Product ${id} has been updated`);
} else {
return reply
.code(404)
.send(`Error: no product found`);
}
});
fastify.delete("/products/:id", async (request, reply) => {
const id = parseInt(request.params.id, 10);
const itemToRemoveKey = items.findIndex((item) => item.id === id);
if (itemToRemoveKey != -1) {
items.splice(itemToRemoveKey, 1);
return reply
.code(204);
} else {
return reply
.code(404)
.send(`Error: no product found`);
}
});
// Run the server!
const start = async () => {
try {
await fastify.listen(3000, "0.0.0.0");
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();