forked from netsamma/food-delivery-be
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (55 loc) · 1.86 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
const express = require('express');
const path = require("path");
const cors = require('cors');
const fs = require("fs");
const app = express();
const port = 3000;
app.use(express.json());
app.use(cors());
app.post('/login', (req, res) => {
console.log(req);
const { username, password } = req.body;
if (username === 'admin' && password === 'pa123') {
res.json({ token: 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VybmFtZSIsImlhdCI6MTY1OTk0NDMyMiwiZXhwIjoxNjU5OTUyODMyfQ.5-6F0o-zV3z0_a_a_0_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a_a' });
} else {
res.status(401).json({ error: 'Credenziali errate' });
}
});
app.get('/shops', (req, res)=>{
fs.readFile(process.cwd()+'/data/shops.json', (err, data) => {
if (err) throw err;
res.send(JSON.parse(data));
});
})
app.get('/shops/:id', (req, res) => {
const shopId = req.params.id;
fs.readFile(path.join(process.cwd(), 'data', 'shops.json'), (err, data) => {
if (err) throw err;
const shops = JSON.parse(data);
const shop = shops.find(s => +s.id === +shopId);
if (shop) {
res.send(shop);
} else {
res.status(404).send({ error: 'Shop non trovato' });
}
});
});
app.post('/shops', (req, res) => {
const newShop = req.body;
fs.readFile(path.join(process.cwd(), 'data', 'shops.json'), (err, data) => {
if (err) throw err;
let shops = JSON.parse(data);
const maxId = shops.reduce((max, shop) => Math.max(max, parseInt(shop.id, 10)), 0);
newShop.id = maxId + 1;
// Aggiungi il nuovo shop alla lista
shops.push(newShop);
// Scrivi la lista aggiornata nel file JSON
fs.writeFile(path.join(process.cwd(), 'data', 'shops.json'), JSON.stringify(shops, null, 2), (err) => {
if (err) throw err;
res.status(201).send(newShop);
});
});
})
app.listen(port, () => {
console.log(`Fake API listening on port ${port}`);
});