Esempio n. 1
0
        public async Task <User> CreateAsync(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }
            else if (ValidateUser.ValidatePassword(user.Password) == false)
            {
                throw new AppException(
                          "Password have length in range 8-15 character and have at least 1 uppercase, 1 lowercase, 1 digit");
            }

            if (_context.Users.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username \"" + user.Username + "\" is already taken");
            }

            if (user.Email == null)
            {
                throw new AppException("Email is required");
            }
            else if (_context.Users.Any(x => x.Email == user.Email))
            {
                throw new AppException("Email \"" + user.Email + "\" is already taken");
            }
            else if (ValidateUser.IsValidEmail(user.Email) == false)
            {
                throw new AppException("Email is not in right format");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);


            bool addCartResut = await _cartService.AddCart(new Cart()
            {
                UserId = user.Id
            });

            if (!addCartResut)
            {
                return(null);
            }


            _context.SaveChanges();

            return(user);
        }