Esempio n. 1
0
        public static string AccDel(List <string> param)
        {
            string login    = param[0];
            string password = Utilities.hashBytePassHex(param[1] + login);

            using (var ctx = new SecConvDBEntities())
            {
                var userToDelete = ctx.Users.Where(x => x.Login == login && x.Password == password).FirstOrDefault();
                if (userToDelete != null)
                {
                    try
                    {
                        //1. set, in history, id to null, it can be first or second
                        var userHistory = ctx.Histories.Where(x => x.UserReceiverID == userToDelete.UserID);//first
                        foreach (var item in userHistory)
                        {
                            item.UserReceiverID   = null;
                            ctx.Entry(item).State = System.Data.Entity.EntityState.Modified;
                        }
                        userHistory = ctx.Histories.Where(x => x.UserSenderID == userToDelete.UserID);//second
                        foreach (var item in userHistory)
                        {
                            item.UserSenderID     = null;
                            ctx.Entry(item).State = System.Data.Entity.EntityState.Modified;
                        }

                        //2. delete acquaintances in both directions
                        var userAcquaintances = ctx.Friends.Where(x => x.UserID1 == userToDelete.UserID);//first
                        foreach (var item in userAcquaintances)
                        {
                            ctx.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                        }
                        userAcquaintances = ctx.Friends.Where(x => x.UserID2 == userToDelete.UserID);
                        foreach (var item in userAcquaintances)
                        {
                            ctx.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                        }

                        //3. delete user
                        ctx.Entry(userToDelete).State = System.Data.Entity.EntityState.Deleted;

                        ctx.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(Fail());
                    }
                }
                else
                {
                    return(Fail());
                }
            }
            return(OK());
        }
Esempio n. 2
0
        public static string PassChng(List <string> param)
        {
            string login     = param[0];
            string password1 = Utilities.hashBytePassHex(param[1] + login);

            using (var ctx = new SecConvDBEntities())
            {
                var userToChngPasswd = ctx.Users.Where(x => x.Login == login && x.Password == password1).FirstOrDefault();
                if (userToChngPasswd != null)
                {
                    userToChngPasswd.Password = Utilities.hashBytePassHex(param[2] + login);
                    try
                    {
                        ctx.Entry(userToChngPasswd).State = System.Data.Entity.EntityState.Modified;
                        ctx.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(Fail());
                    }
                }
                else
                {
                    return(Fail());
                }
            }
            return(OK());
        }
Esempio n. 3
0
        public static string LogOut(List <string> param)
        {
            string login = param[0];

            using (var ctx = new SecConvDBEntities())
            {
                var userToLogOut = ctx.Users.Where(x => x.Login == login).FirstOrDefault();
                if (userToLogOut != null)
                {
                    userToLogOut.LastLogoutDate = DateTime.Now;
                    try
                    {
                        Program.onlineUsers.Remove(userToLogOut.UserID); //delete from dictionary
                        ctx.Entry(userToLogOut).State = System.Data.Entity.EntityState.Modified;
                        ctx.SaveChanges();
                        //find people who have friend that is logged in
                        var friends = ctx.Friends.Where(x => x.UserID2 == userToLogOut.UserID).Select(x => x.UserID1);
                        //wyszukaj znajomych w slowniku online i dodaj/zmień w słowniku adres IP
                        foreach (var item in friends)
                        {
                            if (Program.onlineUsers.ContainsKey(item))
                            {
                                Program.onlineUsers[item].friendWithChangedState[login] = "0";
                            }
                        }
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(Fail());
                    }
                }
                else
                {
                    return(Fail());
                }
            }
            return(OK());
        }
Esempio n. 4
0
        public static string DelFriend(List <string> param)
        {
            string login1 = param[0]; //logged in user
            string login2 = param[1];

            using (var ctx = new SecConvDBEntities())
            {
                var userLoggedInID1 = ctx.Users.Where(x => x.Login == login1).Select(x => x.UserID).FirstOrDefault();
                if (userLoggedInID1 != 0)
                {
                    var user1Friend = ctx.Users.Where(x => x.Login == login2).Select(x => new { x.UserID, x.Login }).FirstOrDefault();
                    if (user1Friend.UserID != 0)
                    {
                        var friend = ctx.Friends.Where(x => x.UserID1 == userLoggedInID1 && x.UserID2 == user1Friend.UserID).FirstOrDefault();
                        ctx.Entry(friend).State = System.Data.Entity.EntityState.Deleted;
                        try
                        {
                            ctx.SaveChanges();
                            Program.onlineUsers[userLoggedInID1].friendWithChangedState.Remove(user1Friend.Login);
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                            return(Fail());
                        }
                    }
                    else
                    {
                        return(Fail());
                    }
                }
                else
                {
                    return(Fail());
                }
            }
            return(OK());
        }