Esempio n. 1
0
        public static bool AddUser(string userName, string password)
        //Adds user to DB. If user exists return False
        {
            try
            {
                using (var ctx = new ChatContext())
                {
                    //userName is not in DB
                    if (!IsUserInDB(userName))
                    {
                        User user = new User()
                        {
                            UserName = userName
                        };
                        user.Password = Encryption.Encrypt(password);
                        ctx.Users.Add(user);
                        ctx.SaveChanges();
                        Report.log(DeviceToReport.DAL, LogLevel.Information, $"User {userName} Created in DB");
                        return(true);
                    }

                    //userName exists in DB
                    Report.log(DeviceToReport.DAL, LogLevel.Information, $"User {userName} already exists in DB");

                    return(false);
                }
            }
            catch (Exception e)
            {
                Report.log(DeviceToReport.DAL, LogLevel.Exception, $"User {userName} was not added to DB");
                Report.log(DeviceToReport.DAL, LogLevel.Exception, e.Message);
                throw e;
            }
        }
Esempio n. 2
0
 public static void AddMessage(Message message)
 //Adds message from user to contact to the DB
 {
     try
     {
         using (var ctx = new ChatContext())
         {
             ctx.Messages.Add(message);
             ctx.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Report.log(DeviceToReport.DAL, LogLevel.Information, $"Message from {message.Sender} to {message.Receiver} was not added to DB");
         Report.log(DeviceToReport.DAL, LogLevel.Information, e.Message);
         throw e;
     }
 }