-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.js
81 lines (68 loc) · 2.03 KB
/
pages.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
const Database = require("./database/db");
const saveOrphanage = require("./database/saveOrphanage");
module.exports = {
index(req, res) {
return res.render("index");
},
async orphanage(req, res) {
const id = req.query.id;
try {
const db = await Database;
const results = await db.all(
`SELECT * FROM orphanages WHERE id = "${id}"`
);
const orphanage = results[0];
orphanage.images = orphanage.images.split(",");
orphanage.firstImage = orphanage.images[0];
if (orphanage.open_on_weekends == "0") {
orphanage.open_on_weekends = false;
} else {
orphanage.open_on_weekends = true;
}
return res.render("orphanage", { orphanage });
} catch (error) {
console.log(error);
return res.send("Erro no banco de dados");
}
},
async orphanages(req, res) {
try {
const db = await Database;
const orphanages = await db.all("SELECT * FROM orphanages");
return res.render("orphanages", { orphanages });
} catch (error) {
console.log(error);
return res.send("Erro no banco de dados");
}
},
createOrphanage(req, res) {
return res.render("create-orphanage");
},
async saveOrphanage(req, res) {
const fields = req.body;
//validar se todos os campos estão preenchidos
if (Object.values(fields).includes("")) {
return res.send("Todos os campos devem ser preenchidos!");
}
try {
//salvar um orfanato
const db = await Database;
await saveOrphanage(db, {
lat: fields.lat,
lng: fields.lng,
name: fields.name,
about: fields.about,
whatsapp: fields.whatsapp,
images: fields.images.toString(),
instructions: fields.instructions,
opening_hours: fields.opening_hours,
open_on_weekends: fields.open_on_weekends,
});
//redirecionamento
return res.redirect("/orphanages");
} catch (error) {
console.log(error);
return res.send("Erro no banco de dados!");
}
},
};