Esempio n. 1
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] SaveUserResource saveUserResource)
        {
            var validator            = new SaveUserResourceValidator();
            ValidationResult results = await validator.ValidateAsync(saveUserResource);

            if (!results.IsValid)
            {
                return(BadRequest(results.Errors));
            }

            var currentUser = await _userService.GetUserById(id);

            if (currentUser == null)
            {
                return(NotFound());
            }

            User modifiedUser = _mapper.Map <SaveUserResource, User>(saveUserResource);

            await _userService.UpdateUser(currentUser, modifiedUser);

            User updatedUser = await _userService.GetUserById(id);

            UserResource updatedUserResource = _mapper.Map <User, UserResource>(updatedUser);

            return(Ok(updatedUserResource));
        }
        public async Task <ActionResult <UserResource> > UpdateUser(int id, [FromBody] SaveUserResource model)
        {
            var validator        = new SaveUserResourceValidator();
            var validationResult = await validator.ValidateAsync(model);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var userToBeUpdated = await _userService.GetUserById(id);

            if (userToBeUpdated == null)
            {
                return(NotFound());
            }

            var user = _mapper.Map <SaveUserResource, User>(model);

            await _userService.UpdateUser(userToBeUpdated, user);

            var updatedUser = await _userService.GetUserById(id);

            var updatedUserResource = _mapper.Map <User, UserResource>(updatedUser);

            return(Ok());
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateUser([FromBody] SaveUserResource saveUserResource)
        {
            var validator            = new SaveUserResourceValidator();
            ValidationResult results = await validator.ValidateAsync(saveUserResource);

            if (!results.IsValid)
            {
                return(BadRequest(results.Errors));
            }

            User userToCreate = _mapper.Map <SaveUserResource, User>(saveUserResource);

            var newUser = await _userService.CreateUser(userToCreate);

            if (newUser == null)
            {
                return(NotFound());
            }

            var user = await _userService.GetUserById(newUser.Id);

            UserResource userResource = _mapper.Map <User, UserResource>(user);

            return(Ok(userResource));
        }