Ejemplo n.º 1
0
        public async Task <User> CreateAsync(User user, string password, string repeat)
        {
            // validation
            if (string.IsNullOrWhiteSpace(user.Email))
            {
                throw new PokemonAPIException("Email is required", ExceptionConstants.BAD_REGISTER);
            }

            if (string.IsNullOrWhiteSpace(user.UserName))
            {
                throw new PokemonAPIException("Username is required", ExceptionConstants.BAD_REGISTER);
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new PokemonAPIException("Password is required", ExceptionConstants.BAD_REGISTER);
            }

            if (password != repeat)
            {
                throw new PokemonAPIException("Password and repeat should be equal", ExceptionConstants.BAD_REGISTER);
            }


            if (await GetByEmailAsync(user.Email) != null)
            {
                throw new PokemonAPIException("Email \"" + user.Email + "\" is already taken", ExceptionConstants.BAD_REGISTER);
            }

            if (_context.Users.Any(x => x.UserName == user.UserName))
            {
                throw new PokemonAPIException("Username \"" + user.UserName + "\" is already taken", ExceptionConstants.BAD_REGISTER);
            }

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

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

            await _context.Users.AddAsync(user);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }

            return(user);
        }
Ejemplo n.º 2
0
        public async Task <CustomPokemon> AddCustom(int id, CustomPokemonModel model)
        {
            var entity = model.ToEntity();

            entity.UserId = id;

            await _context.CustomPokemons.AddAsync(entity);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }

            return(entity);
        }