Exemple #1
0
        public ActionResult <UserResponse> Post(UserRequest userCreateRequest)
        {
            User user = new User();

            user    = _mapper.Map <User>(userCreateRequest);
            user.Id = Guid.NewGuid().ToString();
            var userResponse = new UserResponse();

            var isUserExists = _userRepository.GetUserByEmailAddressAsync(userCreateRequest.EmailAddress).Result;

            if (isUserExists != null)
            {
                return(Conflict($"Email: {userCreateRequest.EmailAddress} already in use"));
            }

            try
            {
                var userCreated = _userRepository.CreateUser(user).Result;
                userResponse = _mapper.Map <UserResponse>(userCreated);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(Problem($"There was a problem processing your request:{ex}"));
            }

            return(CreatedAtAction(nameof(Get), new { id = user.Id }, userResponse));
        }
Exemple #2
0
        public ActionResult <UserResponse> Get(string emailAddress)
        {
            if (emailAddress == null)
            {
                var message = string.Format("Email is required");
                return(BadRequest(message));
            }

            var foundUserFromDb = _userRepository.GetUserByEmailAddressAsync(emailAddress).Result;

            if (foundUserFromDb != null)
            {
                var foundUser = new UserResponse
                {
                    Id           = foundUserFromDb.Id,
                    Name         = $"{foundUserFromDb.FirstName} {foundUserFromDb.MiddleName} {foundUserFromDb.LastName}",
                    PhoneNumber  = foundUserFromDb.PhoneNumber,
                    EmailAddress = foundUserFromDb.EmailAddress
                };
                return(foundUser);
            }

            var notFoundMessage = $"User with email: {emailAddress} not found";

            return(NotFound(notFoundMessage));
        }