Revert "Revert "Merge branch 'master' of https://github.com/LukasNowy/SmartShopper""
This reverts commit 84afb3f32d.
			
			
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
const { query, nonQuery } = require("../db-config/postgresql-common");
 | 
			
		||||
 | 
			
		||||
//SELECT own shopping lists
 | 
			
		||||
async function getShoppinglistsAdmin(username) {
 | 
			
		||||
    try {
 | 
			
		||||
 | 
			
		||||
@@ -14,6 +15,8 @@ async function getShoppinglistsAdmin(username) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//SELECT shared shopping lists
 | 
			
		||||
 | 
			
		||||
async function getShoppinglistsShared(username) {
 | 
			
		||||
    try {
 | 
			
		||||
        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 = {
 | 
			
		||||
    getShoppinglistsAdmin, getShoppinglistsShared
 | 
			
		||||
    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user