From 5eac871dd0b969805839423d37b17cf2742868ba Mon Sep 17 00:00:00 2001 From: Lukas Nowy Date: Wed, 28 Nov 2018 21:12:30 +0100 Subject: [PATCH] add groups, add items --- express-server/db-connect/db-connect.js | 54 ++++++++++++++++- express-server/public/javascripts/test.js | 74 ----------------------- express-server/routes/index.js | 25 ++++++++ 3 files changed, 78 insertions(+), 75 deletions(-) diff --git a/express-server/db-connect/db-connect.js b/express-server/db-connect/db-connect.js index 488d36c6..8d7fa015 100644 --- a/express-server/db-connect/db-connect.js +++ b/express-server/db-connect/db-connect.js @@ -109,6 +109,33 @@ async function deleteShoppinglist(sl_id) { } } +//add group into shoppinglist + +async function addGroup(sl_id, name, color) { + try { + let grid = generate_group_id(); + await nonQuery('INSERT INTO "Group" VALUES ($1, $2, $3, $4);', [grid, sl_id, name, color]); + } + + catch (error) { + console.error(error); + } +} + +//add item into group + +async function addItem(group_id, sl_id, name, count) { + try { + let itid = generate_item_id(); + await nonQuery('INSERT INTO "Item" VALUES ($1, $2, $3, $4, $5);', [itid, group_id, sl_id, name, count]); + } + + catch (error) { + console.error(error); + } +} + + //sl_id generieren function generate_sl_id() { @@ -122,6 +149,30 @@ function generate_sl_id() { return output; } +//group_id generieren +function generate_group_id() { + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + var output = ""; + + for(let i = 0; i < 8; i++) { + output += possible.charAt(Math.floor(Math.random() * possible.length)); + } + + return output; +} + +//item_id generieren +function generate_item_id() { + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + var output = ""; + + for(let i = 0; i < 8; i++) { + output += possible.charAt(Math.floor(Math.random() * possible.length)); + } + + return output; +} + function items_in_groups(groups, items) { let result = []; @@ -140,5 +191,6 @@ function items_in_groups(groups, items) { } module.exports = { - getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist + getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist, addGroup, + addItem } diff --git a/express-server/public/javascripts/test.js b/express-server/public/javascripts/test.js index 928d4ede..e69de29b 100644 --- a/express-server/public/javascripts/test.js +++ b/express-server/public/javascripts/test.js @@ -1,74 +0,0 @@ -$(document).ready(function() { - refresh(); - - userinfo(); - - $(document).on("click", ".btn_detail", function() { - window.location.replace("/shoppinglist_json/" + $(this).closest("tr").attr("id")); - }); - - $(".btn_add").click(function() { - $.ajax({ - type: "POST", - url: "/shoppinglist", - data: { - name: $(".name").val(), - description: $(".description").val() - }, success(result) { - refresh(); - } - }); - }); - - $(document).on("click", ".btn_delete", function() { - $.ajax({ - type: "DELETE", - url: "/shoppinglist", - data: { - sl_id: $(this).closest("tr").attr("id") - }, success(result) { - refresh(); - } - }); - }); -}); - -function refresh() { - - $("tbody").empty(); - - $.ajax({ - type: "GET", - url: "/myshoppinglists", - success(data) { - for(let item of data) { - $(".tb_myshoppinglists").append("" + item.name + "" + item.description + - " forward" - + "clear"); - } - } - }); - - $.ajax({ - type: "GET", - url: "/sharedshoppinglists", - success(data) { - for(let item of data) { - $(".tb_sharedshoppinglists").append("" + item.name + "" + item.description + - " forward"); - } - } - }); -} - -function userinfo() { - $.ajax({ - type: "GET", - url: "/userinfo_json", - success(data) { - $(".userinfo").empty(); - $(".userinfo").append("Logged in as: " + data.profile.displayName); - $(".user_img").attr("src", data.profile.photos[0].value); - } - }); -} \ No newline at end of file diff --git a/express-server/routes/index.js b/express-server/routes/index.js index 71a7909b..b251ebd3 100644 --- a/express-server/routes/index.js +++ b/express-server/routes/index.js @@ -130,6 +130,31 @@ router.delete("/shoppinglist", async (req, res) => { } }); +//Group erstellen + +router.post("/group", async (req, res) => { + try { + res.status(200).send(await postgres.addGroup(req.body.sl_id, req.body.name, req.body.color)); + } + + catch(err) { + res.status(400).send(await err); + } + +}); + +//Item erstellen + +router.post("/item", async (req, res) => { + try { + res.status(200).send(postgres.addItem(req.body.group_id, req.body.sl_id, req.body.name, req.body.count)); + } + + catch (err) { + res.status(400).send(err); + } +}); + router.get("/test1", (req, res) => { res.render("index1"); });