Example #1
0
        public int?AddAccount(string email, string password, string role, string idUser)
        {
            #region Validation
            if (Validation.Validation.IsEmptyStrings(email, password, role, idUser))
            {
                throw new ArgumentNullException("Parametres must be not null");
            }

            if (!Validation.Validation.IsRightEmail(email))
            {
                throw new ArgumentException("Incorrect email");
            }

            if (!int.TryParse(idUser, out int realIdUser))
            {
                throw new ArgumentException("Incorrect user's id");
            }

            bool isUserExists = userDao.GetUserById(realIdUser) is null;

            if (isUserExists)
            {
                throw new Exception("User doesn't exists");
            }
            #endregion

            DateTime createdAt        = DateTime.Now;
            DateTime passwordLifetime = createdAt.AddYears(1);

            Account account = new Account()
            {
                Email            = email,
                Password         = EncryptionPassword(password).ToString(),
                Role             = role,
                IdUser           = realIdUser,
                CreatedAt        = createdAt,
                IsBlocked        = false,
                LoggedInto       = DateTime.MaxValue,
                PasswordLifetime = passwordLifetime
            };

            try
            {
                return(accountDao.AddAccount(account));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }