Ejemplo n.º 1
0
        public async Task <IActionResult> PostUser([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (await _check.UsernameExist(user.Username))
            {
                if (await _check.EmailExist(user.Email))
                {
                    return(BadRequest("Username and Email is already taken"));
                }
                return(BadRequest("Username already exists"));
            }

            else if (await _check.EmailExist(user.Email))

            {
                if (await _check.UsernameExist(user.Username))
                {
                    return(BadRequest("Username and Email is already taken"));
                }
                return(BadRequest("Email already exists"));
            }

            var hashedPassword = _auth.HashPassword(user.Password);

            var newUser = new User
            {
                Username  = user.Username.ToLower(),
                FirstName = user.FirstName,
                LastName  = user.LastName,
                Email     = user.Email,
                Password  = hashedPassword
            };

            await _context.User.AddAsync(newUser);

            await _context.SaveChangesAsync();

            var newPostedUser = await _context.User.FirstOrDefaultAsync(x => x.Username == user.Username);

            return(Ok(newPostedUser));
        }