示例#1
0
 /// <summary>
 /// Add a user to contacts list
 /// </summary>
 /// <param name="_contactsUsername">requested username</param>
 /// <param name="_loggedInUser">logged in username</param>
 /// <returns></returns>
 public string AddNewContact(string _contactsUsername, string _loggedInUser)
 {
     try
     {
         // Check if the given username exists
         var check1 = context.Users.FirstOrDefault(user => user.Username.Equals(_contactsUsername));
         if (check1 == null)
         {
             return("User Does Not Exist");
         }
         var check2 = context.Contacts.FirstOrDefault(user => user.Username.Equals(_loggedInUser) && user.Contact.Equals(_contactsUsername));
         if (check2 != null)
         {
             return("Already in contact list");
         }
         var loggedInUser = context.Users.FirstOrDefault(user => user.Username.Equals(_loggedInUser));
         context.Contacts.Add(new Contacts(_loggedInUser, check1.Username));
         context.Contacts.Add(new Contacts(check1.Username, _loggedInUser));
         context.SaveChanges();
         return(String.Format("{0} added to contacts", _contactsUsername));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
示例#2
0
        /// <summary>
        /// Removes all the messages between two users
        /// </summary>
        /// <param name="firstUser"></param>
        /// <param name="secondUser"></param>
        public string RemoveAllMessage(string firstUser, string secondUser)
        {
            try
            {
                var messages = context.Messages.Where(message => (message.Sender.Equals(firstUser) && message.Reciever.Equals(secondUser)) ||
                                                      (message.Sender.Equals(secondUser) && message.Reciever.Equals(firstUser)));

                if (messages == null)
                {
                    return("User and messages removed");
                }
                foreach (Message m in messages)
                {
                    context.Messages.Remove(m);
                }

                context.SaveChanges();
                return("User and messages removed");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }