This commit is contained in:
Georg Reisinger 2018-10-29 08:59:00 +01:00
commit 6ee5de3477
3 changed files with 70 additions and 3 deletions

View File

@ -1,5 +1,6 @@
const { query, nonQuery } = require("../db-config/postgresql-common"); const { query, nonQuery } = require("../db-config/postgresql-common");
//SELECT own shopping lists
async function getShoppinglistsAdmin(username) { async function getShoppinglistsAdmin(username) {
try { try {
@ -14,6 +15,8 @@ async function getShoppinglistsAdmin(username) {
} }
} }
//SELECT shared shopping lists
async function getShoppinglistsShared(username) { async function getShoppinglistsShared(username) {
try { try {
let result = await query('SELECT row_to_json("Shoppinglist") AS obj FROM "Shoppinglist" JOIN "Shoppinglist_member" USING (sl_id) WHERE \ let result = await query('SELECT row_to_json("Shoppinglist") AS obj FROM "Shoppinglist" JOIN "Shoppinglist_member" USING (sl_id) WHERE \
@ -27,7 +30,46 @@ async function getShoppinglistsShared(username) {
} }
} }
//INSERT new Shoppinglist
async function newShoppinglist(name, description, username) {
//generate sl_id
let sl_id = generate_sl_id();
//insert shoppinglist
try {
await nonQuery('INSERT INTO "Shoppinglist" (sl_id, name, description) VALUES ($1, $2, $3);', [sl_id, name, description]);
}
catch (error) {
console.error(error)
}
//insert admin
try {
await nonQuery('INSERT INTO "Shoppinglist_admin" (username, sl_id) VALUES ($1, $2);', [username, sl_id]);
}
catch (error) {
console.error(error)
}
}
//sl_id generieren
function generate_sl_id() {
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var output = "";
for(let i = 0; i < 8; i++) {
output += possible.charAt(Math.floor(Math.random() * possible.length));
}
return output;
}
module.exports = { module.exports = {
getShoppinglistsAdmin, getShoppinglistsShared getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist
} }

View File

@ -0,0 +1,12 @@
$(document).ready(function() {
//DELETE THIS
$.ajax({
type: "POST",
url: "/shoppinglist",
data: {
name: "Name Test",
description: "Description Test"
}
});
// ---
});

View File

@ -49,6 +49,7 @@ router.get('/logout', (req, res) => {
router.get('/dash', function(req, res, next) { router.get('/dash', function(req, res, next) {
console.log("Session: ", req.session.passport.user.token); console.log("Session: ", req.session.passport.user.token);
console.log("User ID: " + req.session.passport.user.profile.id);
if (req.session.passport.user.token) { if (req.session.passport.user.token) {
res.render('index'); res.render('index');
} else { } else {
@ -61,7 +62,7 @@ router.get('/dash', function(req, res, next) {
router.get("/myshoppinglists", async function(req, res, next) { router.get("/myshoppinglists", async function(req, res, next) {
try { try {
//Get user id: req.session.passport.user.profile.id //Get user id: req.session.passport.user.profile.id
res.status(200).send(await postgres.getShoppinglistsAdmin("testuser")); res.status(200).send(await postgres.getShoppinglistsAdmin(req.session.passport.user.profile.id));
} }
catch(err) { catch(err) {
@ -75,7 +76,7 @@ router.get("/myshoppinglists", async function(req, res, next) {
router.get("/sharedshoppinglists", async function(req, res, next) { router.get("/sharedshoppinglists", async function(req, res, next) {
try { try {
//Get user id: req.session.passport.user.profile.id //Get user id: req.session.passport.user.profile.id
res.status(200).send(await postgres.getShoppinglistsShared("testuser")); res.status(200).send(await postgres.getShoppinglistsShared(req.session.passport.user.profile.id));
} }
catch(err) { catch(err) {
@ -84,4 +85,16 @@ router.get("/sharedshoppinglists", async function(req, res, next) {
}); });
//POST new shoppinglist
router.post("/shoppinglist", async function(req, res, next) {
try {
res.status(200).send(await postgres.newShoppinglist(req.body.name, req.body.description, req.session.passport.user.profile.id));
}
catch(err) {
res.status(400).send(await err);
}
});
module.exports = router; module.exports = router;