Einkaufsliste erstellen + ausgabe mit user acconts
This commit is contained in:
parent
8e7d96310f
commit
bd4053054a
@ -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
|
||||||
}
|
}
|
||||||
|
12
express-server/public/javascripts/ajax.js
Normal file
12
express-server/public/javascripts/ajax.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
//DELETE THIS
|
||||||
|
$.ajax({
|
||||||
|
type: "POST",
|
||||||
|
url: "/shoppinglist",
|
||||||
|
data: {
|
||||||
|
name: "Name Test",
|
||||||
|
description: "Description Test"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// ---
|
||||||
|
});
|
@ -47,6 +47,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 {
|
||||||
@ -59,7 +60,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) {
|
||||||
@ -73,7 +74,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) {
|
||||||
@ -82,4 +83,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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user