Ejemplo n.º 1
0
        //Add New User to DB
        public void UpdateUser(User user)
        {
            if (user != null)
            {
                using (ChatDbContext context = new ChatDbContext())
                {
                    var oldUser = GetUserByName(user.UserName);
                    try
                    {
                        oldUser.Password  = user.Password;
                        oldUser.FirstName = user.FirstName;
                        oldUser.LastName  = user.LastName;
                        //oldUser.Messages = user.Messages;

                        context.Users.Add(user);

                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                throw new Exception("The user object is null");
            }
        }
Ejemplo n.º 2
0
        //Add New Message to DB
        public void AddMessage(Message msg)
        {
            if (msg != null)
            {
                using (ChatDbContext context = new ChatDbContext())
                {
                    var user = GetUserByName(msg.UserName);

                    try
                    {
                        var count = context.Messages.Count();
                        if (count > 1000)
                        {
                            DeleteAllMessage();
                        }
                        msg.MessageId = context.Messages.Count();
                        context.Messages.Add(msg);
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                throw new Exception("The message object is null");
            }
        }
Ejemplo n.º 3
0
 //Remove All Message
 public void DeleteAllMessage()
 {
     using (ChatDbContext context = new ChatDbContext())
     {
         try
         {
             context.Messages.RemoveRange(context.Messages);
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             throw new Exception("En Error during registration", ex);
         }
     }
 }
Ejemplo n.º 4
0
 //Delete User
 public void DeleteUser(string userName, string password)
 {
     if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
     {
         User user = GetUser(userName, password);
         if (user != null)
         {
             using (ChatDbContext context = new ChatDbContext())
             {
                 try
                 {
                     context.Users.Remove(user);
                     context.SaveChanges();
                 }
                 catch (Exception ex)
                 {
                     throw new Exception("En Error during removing the user", ex.InnerException);
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
 //Add New User to DB
 public void AddUser(User user)
 {
     if (user != null)
     {
         using (ChatDbContext context = new ChatDbContext())
         {
             try
             {
                 user.UserId = context.Users.Count();
                 context.Users.Add(user);
                 context.SaveChanges();
             }
             catch (Exception ex)
             {
                 throw new Exception("En Error during registration", ex);
             }
         }
     }
     else
     {
         throw new Exception("The user object is null");
     }
 }