-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (38 loc) · 1.19 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
const express = require("express");
const { urlencoded } = require("body-parser");
const client = require("./src/client");
const controller = require("./src/controller");
// Setup API
const port = 3000;
const app = express();
app.use(urlencoded({ extended: true }));
app.use(express.json());
// Define routes
app.get("/products", async (_, res) => {
const products = await controller.getProducts();
res.send(products);
});
app.get("/products/:productId", async (req, res) => {
const { productId } = req.params;
const product = await controller.getProductById(productId);
res.send(product);
});
app.get("/users", async (_, res) => {
const users = await controller.getUsers();
res.send(users);
});
app.get("/users/:userId", async (req, res) => {
const { userId } = req.params;
const user = await controller.getUserById(userId);
res.send(user);
});
app.post("/users/:userId/purchase", async (req, res) => {
const { userId } = req.params;
const productIds = req.body;
const totalPrice = await controller.getProductsPrice(userId, productIds);
res.send({ total: totalPrice });
});
// Start server
app.listen(port, () => {
console.log(`Servidor está executando na porta ${port}.`);
});