add groups, add items
This commit is contained in:
		@@ -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
 | 
					//sl_id generieren
 | 
				
			||||||
function generate_sl_id() {
 | 
					function generate_sl_id() {
 | 
				
			||||||
@@ -122,6 +149,30 @@ function generate_sl_id() {
 | 
				
			|||||||
    return output;
 | 
					    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) {
 | 
					function items_in_groups(groups, items) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let result = [];
 | 
					    let result = [];
 | 
				
			||||||
@@ -140,5 +191,6 @@ function items_in_groups(groups, items) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist
 | 
					    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist, addGroup,
 | 
				
			||||||
 | 
					    addItem
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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("<tr id='" + item.sl_id +"'><td>" + item.name + "</td><td>" + item.description +
 | 
					 | 
				
			||||||
                 "</td><td> <a class='btn-floating btn-large waves-effect waves-light red btn_detail'><i class='material-icons'>forward</i></a></td>"  
 | 
					 | 
				
			||||||
                 + "<td><a class='btn-floating btn-large waves-effect waves-light red btn_delete'><i class='material-icons'>clear</i></a></td></tr>");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    $.ajax({
 | 
					 | 
				
			||||||
        type: "GET",
 | 
					 | 
				
			||||||
        url: "/sharedshoppinglists",
 | 
					 | 
				
			||||||
        success(data) {
 | 
					 | 
				
			||||||
            for(let item of data) {
 | 
					 | 
				
			||||||
                $(".tb_sharedshoppinglists").append("<tr id='" + item.sl_id +"'><td>" + item.name + "</td><td>" + item.description +
 | 
					 | 
				
			||||||
                "</td><td> <a class='btn-floating btn-large waves-effect waves-light red btn_detail'><i class='material-icons'>forward</i></a></td></tr>");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -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) => {
 | 
					router.get("/test1", (req, res) => {
 | 
				
			||||||
  res.render("index1");
 | 
					  res.render("index1");
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user