Exemple #1
0
 public static void Add(Admins Item)
 {
     using (cap01devContext db = new cap01devContext())
     {
         Add(Item, db);
     }
 }
Exemple #2
0
 public static void Add(string userLogin, string password, string fullName, string comment = null)
 {
     try
     {
         Logger.Trace("Admins.Add IN");
         Logger.Debug("Admins.Add userLogin: {0}", userLogin);
         Logger.Debug("Admins.Add fullName: {0}", fullName);
         Logger.Debug("Admins.Add comment: {0}", comment);
         using (cap01devContext db = new cap01devContext())
         {
             Admins item = new Admins()
             {
                 Email = userLogin.ToLower(), Password = password, FullName = fullName
             };
             item.Comment = comment;
             db.Admins.Add(item);
             db.SaveChanges();
         }
     }
     catch (ValidationException ex)
     {
         Logger.Error(ex, "Add new user to DB validation error");
         throw ex;
     }
     catch (Exception ex)
     {
         Logger.Error(ex, "Add new user to DB error");
         throw new Exception("Add new user to DB error", ex);
     }
     finally
     {
         Logger.Trace("Admins.Add OUT");
     }
 }
Exemple #3
0
 public static List <Admins> All()
 {
     using (cap01devContext db = new cap01devContext())
     {
         return(All(db));
     }
 }
Exemple #4
0
 public static void Clear()
 {
     using (cap01devContext db = new cap01devContext())
     {
         Clear(db);
     }
 }
Exemple #5
0
 public static Admins GetUserByLogin(string userLogin)
 {
     using (cap01devContext db = new cap01devContext())
     {
         return(GetUserByLogin(userLogin, db));
     }
 }
Exemple #6
0
        public static Admins GetUserByLogin(string userLogin, cap01devContext db)
        {
            try
            {
                Logger.Trace("Admins.GetUserByLogin IN");
                Logger.Debug("Admins.GetUserByLogin userLogin: {0}", userLogin);

                string lowerUserLogin = userLogin.ToLower();
                //Admins item = db.Admins.Where(row => row.Email.Equals(userLogin.ToLower())).FirstOrDefault();
                Admins item = db.Admins.FirstOrDefault(elem => elem.Email == lowerUserLogin);
                if (item != null)
                {
                    Logger.Debug("User founded");
                    return(item);
                }
                else
                {
                    Logger.Debug("User not founded");
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "GetUserByLogin error");
                throw new Exception("GetUserByLogin error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Delete OUT");
            }
        }
Exemple #7
0
 public static void Delete(string userLogin)
 {
     using (cap01devContext db = new cap01devContext())
     {
         Delete(userLogin, db);
     }
 }
Exemple #8
0
        public static void Add(Admins Item, cap01devContext db)
        {
            try
            {
                if (db == null)
                {
                    throw new ApplicationException("Database connection not initialized");
                }
                Logger.Trace("Admins.Add IN");
                if (Item != null)
                {
                    Logger.Debug("Admins.Add userLogin: {0}", Item.Email);
                    Logger.Debug("Admins.Add fullName: {0}", Item.FullName);
                    Logger.Debug("Admins.Add comment: {0}", Item.Comment);
                }
                else
                {
                    Logger.Debug("Admins.Add Item==null");
                    return;
                }

                if (string.IsNullOrEmpty(Item.Email))
                {
                    throw new ValidationException("Missing required property: Email");
                }
                if (string.IsNullOrEmpty(Item.PasswordHash))
                {
                    throw new ValidationException("Missing required property: PasswordHash");
                }
                if (string.IsNullOrEmpty(Item.FullName))
                {
                    throw new ValidationException("Missing required property: FullName");
                }


                db.Admins.Add(Item);
                db.SaveChanges();
            }
            catch (ValidationException ex)
            {
                Logger.Error(ex, "Add new user to DB error");
                throw ex;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Add new user to DB error");
                throw new Exception("Add new user to DB error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Add OUT");
            }
        }
Exemple #9
0
 public static void Setpassword(string userLogin, string password)
 {
     try
     {
         Logger.Trace("Admins.Setpassword IN");
         Logger.Debug(string.Format("Admins.Setpassword userLogin: {0}", userLogin));
         using (cap01devContext db = new cap01devContext())
         {
             Admins item = GetUserByLogin(userLogin, db);
             if (item != null)
             {
                 Logger.Debug("User founded");
                 Logger.Debug("Admins.Setpassword user_id: {0}", item.Id);
                 item.Password = password;
                 db.SaveChanges();
             }
             else
             {
                 string mes = $"User {userLogin} not founded";
                 Logger.Debug(mes);
                 throw new KeyNotFoundException(mes);
             }
         }
     }
     catch (ValidationException ex)
     {
         Logger.Debug($"Update user error: {ex.Message}");
         throw ex;
     }
     catch (KeyNotFoundException ex)
     {
         Logger.Debug($"Update user error: {ex.Message}");
         throw ex;
     }
     catch (Exception ex)
     {
         Logger.Error(ex, "Update user error");
         throw new Exception("Update user error", ex);
     }
     finally
     {
         Logger.Trace("Admins.Setpassword OUT");
     }
 }
Exemple #10
0
 public static List <Admins> All(cap01devContext db)
 {
     try
     {
         Logger.Trace("Admins.All IN");
         List <Admins> result = new List <Admins>();
         result.AddRange(db.Admins.ToList <Admins>());
         return(result);
     }
     catch (Exception ex)
     {
         Logger.Error(ex, "Get user list error");
         throw new Exception("Get user list error", ex);
     }
     finally
     {
         Logger.Trace("Admins.TestPassword OUT");
     }
 }
Exemple #11
0
        public static void AddOrUpdate(Admins Item, cap01devContext db)
        {
            try
            {
                Logger.Trace("Admins.Update IN");

                db.Update <Admins>(Item);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Update user error");
                throw new Exception("Update user error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Delete OUT");
            }
        }
Exemple #12
0
        public static void Clear(cap01devContext db)
        {
            try
            {
                Logger.Trace("Admins.Clear IN");
                List <Admins> result = new List <Admins>();

                db.Admins.RemoveRange(All(db));
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Delete all admins error");
                throw new Exception("Delete all admins error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Clear OUT");
            }
        }
Exemple #13
0
        public static void Delete(string userLogin, cap01devContext db)
        {
            try
            {
                Logger.Trace("Admins.Delete IN");
                Logger.Debug("Admins.Delete userLogin: {0}", userLogin);
                Admins item = GetUserByLogin(userLogin, db);

                if (item != null)
                {
                    Logger.Debug("User founded");
                    Logger.Debug("Admins.Delete user_id: {0}", item.Id);
                    Logger.Debug("Admins.Delete User with user_id {0} and login {1} will be deleted", item.Id, item.Email);
                    db.Admins.Remove(item);
                    db.Remove <Admins>(item);
                    db.SaveChanges();
                    Logger.Debug("Admins.Del User deleted", item.Id, item.Email);
                }
                else
                {
                    string mes = $"User {userLogin} not founded";
                    Logger.Debug(mes);
                    throw new KeyNotFoundException(mes);
                }
            }
            catch (KeyNotFoundException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Delete user error");
                throw new Exception("Delete user error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Delete OUT");
            }
        }
Exemple #14
0
 public static bool TestPassword(string userLogin, string password)
 {
     try
     {
         Logger.Trace("Admins.TestPassword IN");
         Logger.Debug("Admins.TestPassword userLogin: {0}", userLogin);
         using (cap01devContext db = new cap01devContext())
         {
             Admins item = GetUserByLogin(userLogin, db);
             if (item != null)
             {
                 Logger.Debug("User founded");
                 Logger.Debug("Admins.TestPassword user_id: {0}", item.Id);
                 bool result = item.TestPassword(password);
                 Logger.Debug("Admins.TestPassword result: {0}", result);
                 return(result);
             }
             else
             {
                 string mes = $"User {userLogin} not founded";
                 Logger.Debug(mes);
                 throw new KeyNotFoundException(mes);
             }
         }
     }
     catch (KeyNotFoundException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         Logger.Error(ex, "Test user password error");
         throw new Exception("Test user password error", ex);
     }
     finally
     {
         Logger.Trace("Admins.TestPassword OUT");
     }
 }
Exemple #15
0
        public static void Disable(string userLogin, cap01devContext db)
        {
            try
            {
                Logger.Trace("Admins.Disable IN");
                Logger.Debug("Admins.Disable userLogin: {0}", userLogin);

                Admins item = GetUserByLogin(userLogin, db);
                if (item != null)
                {
                    Logger.Debug("User founded");
                    Logger.Debug("Admins.Disable user_id: {0}", item.Id);
                    item.Disable();
                    db.Update <Admins>(item);
                    db.SaveChanges();
                }
                else
                {
                    Logger.Debug("User not founded");
                    throw new KeyNotFoundException("User not founded");
                }
            }
            catch (KeyNotFoundException ex)
            {
                Logger.Error(ex, $"User {userLogin} not exist");
                throw new KeyNotFoundException("Test user password error", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Disable user error");
                throw new Exception("Disable user error", ex);
            }
            finally
            {
                Logger.Trace("Admins.Disable OUT");
            }
        }