Esempio n. 1
0
        public ActionResult <UserInfoViewModel> GetUserInfo(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                return(ActionResult <UserInfoViewModel>
                       .CreateFailure(ex : new ArgumentNullException(),
                                      statusCode : System.Net.HttpStatusCode.BadRequest));
            }

            var user = _userRepository.GetByEmail(email);

            if (user == null)
            {
                return(ActionResult <UserInfoViewModel>
                       .CreateFailure(ex : new ArgumentNullException(),
                                      statusCode : System.Net.HttpStatusCode.NotFound));
            }

            var userInfoViewModel = _mapper.Map <User, UserInfoViewModel>(user);

            userInfoViewModel.Phones = user.Phones.Select(ph => new PhoneViewModel
            {
                AreaCode    = ph.AreaCode,
                CountryCode = ph.CountryCode,
                Number      = ph.Number
            }).ToList();

            return(ActionResult <UserInfoViewModel>
                   .CreateSuccessResult(result : userInfoViewModel));
        }
Esempio n. 2
0
        public ActionResult <UserViewModel> Authenticate(AuthViewModel authViewModel)
        {
            _authValidationServices.Validate(authViewModel);

            var user = _userRepository.GetByEmailAndPassword(authViewModel.Email, authViewModel.Password);

            if (user == null)
            {
                return(ActionResult <UserViewModel> .CreateFailure(ex : new UnauthorizedAccessException(), statusCode : System.Net.HttpStatusCode.Unauthorized));
            }

            var userViewModel = _mapper.Map <User, UserViewModel>(user);

            userViewModel.Phones = user.Phones.Select(ph => new PhoneViewModel
            {
                AreaCode    = ph.AreaCode,
                CountryCode = ph.CountryCode,
                Number      = ph.Number
            }).ToList();

            /// Update LastAccess.
            _userService.UpdateInfoAccess(userViewModel);

            /// Create Token.
            userViewModel.Token = CreateToken(userViewModel);

            return(ActionResult <UserViewModel> .CreateSuccessResult(result : userViewModel, message : "User successfully logged in!"));
        }
Esempio n. 3
0
        public ActionResult InsertUser(UserViewModel userViewModel)
        {
            _userValidationService.Validate(userViewModel);

            var _user = _mapper.Map <UserViewModel, User>(userViewModel);

            _user.Phones = userViewModel.Phones.Select(ph => new Phone
            {
                AreaCode    = ph.AreaCode,
                CountryCode = ph.CountryCode,
                Number      = ph.Number
            }).ToList();

            _userRepository.Insert(_user);

            return(ActionResult.CreateSuccessResult());
        }