-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from lobophf/database
Database
- Loading branch information
Showing
9 changed files
with
185 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,76 @@ | ||
const orphanages = require('./database/fakedata.js'); | ||
const Database = require("./database/db"); | ||
const saveOrphanage = require("./database/saveOrphanage"); | ||
|
||
module.exports = { | ||
|
||
index(req, res){ | ||
return res.render('index') | ||
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"); | ||
} | ||
}, | ||
orphanage(req, res){ | ||
return res.render('orphanage') | ||
|
||
createOrphanage(req, res) { | ||
return res.render("create-orphanage"); | ||
}, | ||
orphanages(req, res){ | ||
return res.render('orphanages', { orphanages }) | ||
|
||
async saveOrphanage(req, res) { | ||
const fields = req.body; | ||
|
||
if (Object.values(fields).includes("")) { | ||
return res.send("Todos os campos devem ser preenchidos"); | ||
} | ||
|
||
try { | ||
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, | ||
}); | ||
return res.redirect("/orphanages"); | ||
} catch (error) { | ||
console.log(error); | ||
return res.send("Erro no banco de dados"); | ||
} | ||
}, | ||
createOrphanage(req, res){ | ||
return res.render('create-orphanage') | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.