Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (81 loc) · 2.3 KB
/
Copy pathindex.js
File metadata and controls
100 lines (81 loc) · 2.3 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
const express = require('express');
const uuid = require('uuid');
const fs = require('fs')
const db = require('./db.json');
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.static('public'));
app.get('/tickets', (_, res) => {
res.send(db);
});
app.get('/tickets/pendientes', (req, res) => {
const filtradas = db.tickets.filter((el) => el.completada === false);
res.send({
tickets: filtradas,
});
});
app.get('/ticket/:id', (req, res) => {
const { id } = req.params;
const ticket = db.tickets.find((el) => el.id === id);
res.send({
ticket,
});
});
app.get('/borraticket/:id', (req, res) => {
const { id } = req.params;
const idx = db.tickets.findIndex((el) => el.id === id);
db.tickets.splice(idx, 1);
fs.writeFile('./db.json', JSON.stringify(db, null, 2), () => {} );
res.send(db);
});
app.post('/nueva', (req, res) => {
const { cliente, solicitud } = req.body;
let f = new Date();
const fechastr = f.getFullYear() + "-" + (f.getMonth()+1) + "-" + f.getDate();
if (cliente && solicitud) {
const nuevoTicket = {
id: uuid.v4(),
cliente,
solicitud,
fecha: fechastr,
completada: false,
};
db.tickets.push(nuevoTicket);
fs.writeFile('./db.json', JSON.stringify(db, null, 2), () => {} );
res.redirect("/");
} else {
res.send({
error: 'No se ha podido crear el ticket :(',
});
}
});
app.get('/actualizaticket/:id/:completada', (req, res) => {
const {
id,
completada,
} = req.params;
const ticketIdx = db.tickets.findIndex((el) => el.id === id);
db.tickets[ticketIdx].completada = completada === 'true';
fs.writeFile('./db.json', JSON.stringify(db, null, 2), () => {} );
res.send({});
});
app.post('/editar', (req, res) => {
const {
cliente,
fecha,
solicitud,
completada,
id,
} = req.body;
const ticketIdx = db.tickets.findIndex((el) => el.id === id);
db.tickets[ticketIdx].cliente = cliente;
db.tickets[ticketIdx].fecha = fecha;
db.tickets[ticketIdx].solicitud = solicitud;
db.tickets[ticketIdx].completada = completada ? true : false;
fs.writeFile('./db.json', JSON.stringify(db, null, 2), () => {} );
res.redirect("/");
})
app.listen(3000, () => {
console.log('Server started: http://localhost:3000/');
});