Userdaten in db einfügen, user suchfunktion
This commit is contained in:
		@@ -4,9 +4,9 @@ const stringSimilarity = require('string-similarity');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
//Create User Info
 | 
					//Create User Info
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function updateUser(uid, mid) {
 | 
					async function updateUser(uid, mid, name, picture, email) {
 | 
				
			||||||
    try {      
 | 
					    try {      
 | 
				
			||||||
        await nonQuery('INSERT INTO "User" (username, message_id) VALUES ($1, $2);', [uid, mid]);
 | 
					        await nonQuery('INSERT INTO "User" (username, message_id, name, picture, email) VALUES ($1, $2, $3, $4, $5);', [uid, mid, name, picture, email]);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -28,6 +28,22 @@ async function getmessageids(sl_id) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function searchUsers(searchstring) {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        let users = await query('SELECT row_to_json("User") AS obj FROM "User";');
 | 
				
			||||||
 | 
					        return users.filter(function(obj) {return obj.name.toUpperCase().includes(searchstring.toUpperCase())});
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    catch(error) {
 | 
				
			||||||
 | 
					        console.error(error);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//SELECT own shopping lists
 | 
					//SELECT own shopping lists
 | 
				
			||||||
async function getShoppinglistsAdmin(username) {
 | 
					async function getShoppinglistsAdmin(username) {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
@@ -534,5 +550,5 @@ function probability(cur_item, data) {
 | 
				
			|||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist, addGroup,
 | 
					    getShoppinglistsAdmin, getShoppinglistsShared, newShoppinglist, displayShoppinglist, deleteShoppinglist, addGroup,
 | 
				
			||||||
    addItem, verifyInvite, createInvite, editShoppinglist, editGroup, editItem, deleteGroup, deleteItem, manInvite, updateUser,
 | 
					    addItem, verifyInvite, createInvite, editShoppinglist, editGroup, editItem, deleteGroup, deleteItem, manInvite, updateUser,
 | 
				
			||||||
    moveDoneItems, getDonePurchases, getShoppinglistsByLink
 | 
					    moveDoneItems, getDonePurchases, getShoppinglistsByLink, searchUsers
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -46,7 +46,7 @@ var config = {
 | 
				
			|||||||
  getAllShoppinglists();
 | 
					  getAllShoppinglists();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  updateUser();
 | 
					  updateUser();
 | 
				
			||||||
  displayShoppinglist();
 | 
					  testSearch();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -110,6 +110,40 @@ function updateProgress(percent) {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function updateUser() {
 | 
				
			||||||
 | 
					    firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
 | 
				
			||||||
 | 
					        $.ajax({
 | 
				
			||||||
 | 
					            type: "POST",
 | 
				
			||||||
 | 
					            url: "/user",
 | 
				
			||||||
 | 
					            data:{
 | 
				
			||||||
 | 
					                idtoken: idtoken,
 | 
				
			||||||
 | 
					                message_id: "msgtest"
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            success(){
 | 
				
			||||||
 | 
					                console.log("USer updated");
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            error(err){
 | 
				
			||||||
 | 
					                console.error("Error: " + err);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    }).catch((error) => console.error("Get id token client error: ", error));}else{console.log("Check Auth error", user)}});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function testSearch() {
 | 
				
			||||||
 | 
					    console.log("dpne");
 | 
				
			||||||
 | 
					    $.ajax({
 | 
				
			||||||
 | 
					        type: "GET",
 | 
				
			||||||
 | 
					        url: "/users?search=Lukas",
 | 
				
			||||||
 | 
					        success(data) {
 | 
				
			||||||
 | 
					            console.log(data);
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        error(err) {
 | 
				
			||||||
 | 
					            console.error(err);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getAllShoppinglists() {
 | 
					function getAllShoppinglists() {
 | 
				
			||||||
    firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
 | 
					    firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
 | 
				
			||||||
        $.ajax({
 | 
					        $.ajax({
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -398,8 +398,11 @@ router.post("/user", async function(req, res, next) {
 | 
				
			|||||||
  firebaseAdmin.auth().verifyIdToken(token)
 | 
					  firebaseAdmin.auth().verifyIdToken(token)
 | 
				
			||||||
    .then(async function(decodedToken) {
 | 
					    .then(async function(decodedToken) {
 | 
				
			||||||
      uid = decodedToken.uid;
 | 
					      uid = decodedToken.uid;
 | 
				
			||||||
 | 
					      console.log("UID: ", uid);
 | 
				
			||||||
 | 
					      console.log("So andere sachen: ", req.body.name, req.body.description);
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        res.status(200).send(await postgres.updateUser(uid, req.body.message_id));
 | 
					        
 | 
				
			||||||
 | 
					        res.status(200).send(await postgres.updateUser(uid, req.body.message_id, decodedToken.name, decodedToken.picture, decodedToken.email));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
      catch(err) {
 | 
					      catch(err) {
 | 
				
			||||||
@@ -410,6 +413,18 @@ router.post("/user", async function(req, res, next) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					router.get("/users", async (req, res) => {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					     res.status(200).send(await postgres.searchUsers(req.query.search));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  catch(err) {
 | 
				
			||||||
 | 
					    res.status(400).send(err);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//postgres.updateUser("testuser2", "einemessageid");
 | 
					//postgres.updateUser("testuser2", "einemessageid");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user