diff --git a/express-server/db-connect/db-connect.js b/express-server/db-connect/db-connect.js index ea01d163..488d36c6 100644 --- a/express-server/db-connect/db-connect.js +++ b/express-server/db-connect/db-connect.js @@ -79,6 +79,7 @@ async function generateUser() { } } +//get shoppinglist content async function displayShoppinglist(sl_id) { try { let groups = await query('SELECT row_to_json("Group") AS obj FROM "Group" JOIN "Shoppinglist" USING (sl_id) WHERE sl_id = $1;', [sl_id]); @@ -93,6 +94,21 @@ async function displayShoppinglist(sl_id) { } } +//delete shoppinglist + +async function deleteShoppinglist(sl_id) { + + try { + await nonQuery('DELETE FROM "Shoppinglist_admin" WHERE sl_id = $1', [sl_id]); + await nonQuery('DELETE FROM "Shoppinglist_member" WHERE sl_id = $1', [sl_id]); + await nonQuery('DELETE FROM "Shoppinglist" WHERE sl_id = $1', [sl_id]); + } + + catch (error) { + console.error(error); + } +} + //sl_id generieren function generate_sl_id() { @@ -124,5 +140,5 @@ function items_in_groups(groups, items) { } module.exports = { - getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist + getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist } diff --git a/express-server/public/javascripts/test.js b/express-server/public/javascripts/test.js index c0f784c4..ca6a2528 100644 --- a/express-server/public/javascripts/test.js +++ b/express-server/public/javascripts/test.js @@ -1,27 +1,60 @@ $(document).ready(function() { - $.ajax({ - type: "GET", - url: "/myshoppinglists", - success(data) { - for(let item of data) { - $(".tb_myshoppinglists").append("" + item.name + "" + item.description + - " forward"); - } - } - }); - - $.ajax({ - type: "GET", - url: "/sharedshoppinglists", - success(data) { - for(let item of data) { - $(".tb_sharedshoppinglists").append("" + item.name + "" + item.description + - " forward"); - } - } - }); + refresh(); $(document).on("click", ".btn_detail", function() { window.location.replace("/shoppinglist/" + $(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"); + } + } + }); + } }); \ No newline at end of file diff --git a/express-server/routes/index.js b/express-server/routes/index.js index ec9c306c..e132f673 100644 --- a/express-server/routes/index.js +++ b/express-server/routes/index.js @@ -110,6 +110,18 @@ router.get("/shoppinglist/:sl_id", async (req, res) => { } }); +//DELETE Shoppinglist + +router.delete("/shoppinglist", async (req, res) => { + try { + res.status(200).send(await postgres.deleteShoppinglist(req.body.sl_id)); + } + + catch(err) { + res.status(400).send(await err); + } +}); + router.get("/test1", (req, res) => { res.render("index1"); }); diff --git a/express-server/views/index1.ejs b/express-server/views/index1.ejs index 6813f8ac..ef387043 100644 --- a/express-server/views/index1.ejs +++ b/express-server/views/index1.ejs @@ -12,7 +12,21 @@
-

Logged in as: testuser1

+

Logged in as: testuser1

+

+
+
+ + +
+
+ + +
+
+ add +
+

My Shoppinglists

@@ -21,6 +35,7 @@ Name Description Open + Delete