This repository has been archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-> Back-end and database completed - Knowledge Acquired - --NodeJS / Express -> More export/require functionalities / dev hacks --HTTP Requests -> Get / Post details / req.body / req.query details --Database SQLite -> Sqlite-async dependency / Running queries from JS
- Loading branch information
1 parent
722a9e6
commit bc4da45
Showing
13 changed files
with
756 additions
and
105 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module.exports = async (database, { proffyValue, classValue, classScheduleValues }) => { | ||
// Proffys table INSERT | ||
const insertedProffy = await database.run(` | ||
INSERT INTO proffys ( | ||
name, | ||
avatar, | ||
whatsapp, | ||
bio | ||
) VALUES ( | ||
"${proffyValue.name}", | ||
"${proffyValue.avatar}", | ||
"${proffyValue.whatsapp}", | ||
"${proffyValue.bio}" | ||
); | ||
`); | ||
|
||
const proffy_id = insertedProffy.lastID; | ||
|
||
// Classes table INSERT | ||
const insertedClass = await database.run(` | ||
INSERT INTO classes ( | ||
subject, | ||
cost, | ||
proffy_id | ||
) VALUES ( | ||
"${classValue.subject}", | ||
"${classValue.cost}", | ||
"${proffy_id}" | ||
); | ||
`); | ||
|
||
const class_id = insertedClass.lastID; | ||
|
||
// ClassSchedule table INSERT | ||
const insertedClassesSchedule = classScheduleValues.map((classScheduleValue) => { | ||
return database.run(` | ||
INSERT INTO class_schedule ( | ||
class_id, | ||
weekday, | ||
time_from, | ||
time_to | ||
) VALUES ( | ||
"${class_id}", | ||
"${classScheduleValue.weekday}", | ||
"${classScheduleValue.time_from}", | ||
"${classScheduleValue.time_to}" | ||
); | ||
`); | ||
}); | ||
|
||
await Promise.all(insertedClassesSchedule); | ||
|
||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const db = require('sqlite-async'); | ||
|
||
module.exports = db.open(__dirname + '/database.sqlite').then((database) => { | ||
// Creating tables | ||
return database.exec(` | ||
CREATE TABLE IF NOT EXISTS proffys ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT, | ||
avatar TEXT, | ||
whatsapp TEXT, | ||
bio TEXT | ||
); | ||
CREATE TABLE IF NOT EXISTS classes ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
subject INTEGER, | ||
cost TEXT, | ||
proffy_id INTEGER | ||
); | ||
CREATE TABLE IF NOT EXISTS class_schedule ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
class_id INTEGER, | ||
weekday INTEGER, | ||
time_from INTEGER, | ||
time_to INTEGER | ||
); | ||
`); | ||
}); | ||
|
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Data collections | ||
const proffys = [ | ||
{ | ||
name: "Kauan Schaeffer", | ||
avatar: "https://avatars0.githubusercontent.com/u/37464395?s=460&u=463274b9ecfdf58b59713229addaf5545c0c034c&v=4", | ||
whatsapp: "4199999999", | ||
bio: "Amante da busca constante pelo saber.<br><br>Apaixonado pela filosofia e suas reflexões, sempre buscando escrever e refletir acerca de questões epistemológicas, metafísicas, antropológicas e axiológicas.", | ||
subject: "Filosofia", | ||
cost: "1,00", | ||
weekday: [0], | ||
time_from: [720], | ||
time_to: [940] | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const db = require('./db'); | ||
const createProffy = require('./createProffy'); | ||
|
||
db.then(async (database) => { | ||
// Data | ||
proffyValue = { | ||
name: 'Kauan Schaeffer', | ||
avatar: 'https://avatars0.githubusercontent.com/u/37464395?s=460&u=463274b9ecfdf58b59713229addaf5545c0c034c&v=4', | ||
whatsapp: '4199999999', | ||
bio: 'Amante da busca constante pelo saber.<br><br>Apaixonado pela filosofia e suas reflexões, sempre buscando escrever e refletir acerca de questões epistemológicas, metafísicas, antropológicas e axiológicas.' | ||
} | ||
|
||
classValue = { | ||
subject: 4, | ||
cost: '1', | ||
} | ||
|
||
classScheduleValues = [ | ||
{ | ||
weekday: 1, | ||
time_from: 720, | ||
time_to: 940 | ||
}, | ||
{ | ||
weekday: 3, | ||
time_from: 520, | ||
time_to: 1220 | ||
} | ||
] | ||
|
||
// Insert | ||
// await createProffy(database, { proffyValue, classValue, classScheduleValues }); | ||
|
||
// Select All | ||
const proffysSelect = await database.all("SELECT * FROM proffys"); | ||
//console.log(proffysSelect); | ||
|
||
// Select proffy with class | ||
const proffyAndClassesSelect = await database.all(` | ||
SELECT classes.*, proffys.* | ||
FROM proffys | ||
JOIN classes ON (classes.proffy_id = proffys.id) | ||
WHERE classes.proffy_id = 1; | ||
`); | ||
//console.log(proffyAndClassesSelect); | ||
|
||
const classesScheduleSelect = await database.all(` | ||
SELECT * | ||
FROM class_schedule | ||
WHERE class_schedule.class_id = 1 | ||
AND class_schedule.weekday = "3" | ||
AND class_schedule.time_from <= "620" | ||
AND class_schedule.time_to > "620"; | ||
`); | ||
console.log(classesScheduleSelect); | ||
|
||
}); |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const Database = require('./database/db'); | ||
|
||
const { subjects, weekdays, getSubject, convertHoursToMinutes } = require('./utils/format'); // Object disruption so I dont need to call it from a variable | ||
|
||
// Pages functionalities | ||
function pageLanding(req, res) { | ||
return res.render("index.html"); | ||
} | ||
|
||
async function pageStudy(req, res) { | ||
const filters = req.query; | ||
|
||
if (!filters.subject || !filters.weekday || !filters.time) { | ||
return res.render("study.html", { filters, subjects, weekdays }); | ||
} | ||
|
||
const timeToMinutes = convertHoursToMinutes(filters.time); | ||
const query = ` | ||
SELECT classes.*, proffys.* | ||
FROM proffys | ||
JOIN classes ON (classes.proffy_id = proffys.id) | ||
WHERE EXISTS( | ||
SELECT * | ||
FROM class_schedule | ||
WHERE class_schedule.class_id = classes.id | ||
AND class_schedule.weekday = ${filters.weekday} | ||
AND class_schedule.time_from <= ${timeToMinutes} | ||
AND class_schedule.time_to > ${timeToMinutes} | ||
) | ||
AND classes.subject = '${filters.subject}'; | ||
`; | ||
|
||
try { | ||
const db = await Database; | ||
const proffys = await db.all(query); | ||
|
||
proffys.map((proffy) => { | ||
proffy.subject = getSubject(proffy.subject); | ||
}); | ||
|
||
return res.render("study.html", { proffys, filters, subjects, weekdays }); | ||
|
||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
} | ||
|
||
function pageGiveClasses(req, res) { | ||
return res.render("give-classes.html", { subjects, weekdays }); | ||
} | ||
|
||
async function saveGiveClasses(req, res) { | ||
const createProffy = require('./database/createProffy'); | ||
|
||
const proffyValue = { | ||
name: req.body.name, | ||
avatar: req.body.avatar, | ||
whatsapp: req.body.whatsapp, | ||
bio: req.body.bio | ||
}; | ||
|
||
const classValue = { | ||
subject: req.body.subject, | ||
cost: req.body.cost | ||
} | ||
|
||
const classScheduleValues = req.body.weekday.map((weekday, index) => { | ||
return { | ||
weekday, | ||
time_from: convertHoursToMinutes(req.body.time_from[index]), | ||
time_to: convertHoursToMinutes(req.body.time_to[index]) | ||
} | ||
}); | ||
|
||
try { | ||
const db = await Database; | ||
await createProffy(db, { proffyValue, classValue, classScheduleValues }); | ||
|
||
let queryString = "?subject=" + req.body.subject; | ||
queryString += "&weekday=" + req.body.weekday[0]; | ||
queryString += "&time=" + req.body.time_from[0]; | ||
return res.redirect("/study" + queryString); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
pageLanding, | ||
pageStudy, | ||
pageGiveClasses, | ||
saveGiveClasses | ||
}; | ||
|
Oops, something went wrong.