Exemplo n.º 1
0
        public ActionResult <UserResource> UpdateUser(int id, [FromBody] SaveUserResource saveUserResource)
        {
            var validator        = new SaveUserResourceValidator();
            var validationResult = validator.Validate(saveUserResource);

            bool requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var userToBeUpdate = _userService.GetById(id);

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

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

            user.UserId = id;
            _userService.Update(user);

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

            return(Ok(updatedUserResource));
        }
Exemplo n.º 2
0
        public ActionResult <UserResource> CreateUser([FromBody] SaveUserResource saveUserResource)
        {
            var validator        = new SaveUserResourceValidator();
            var validationResult = validator.Validate(saveUserResource);

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

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

            _userService.Create(userToCreate);

            var user         = _userService.GetById(userToCreate.UserId);
            var userResource = _mapper.Map <User, UserResource>(user);

            return(Ok(userResource));
        }
Exemplo n.º 3
0
        public async Task <ActionResult <UserResource> > CreateUser([FromBody] SaveUserResource model)
        {
            var validator = new SaveUserResourceValidator();

            var validationResult = validator.Validate(model);

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

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

            var newUser = await _userService.CreateUser(userToCreate);

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

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

            return(Ok());
        }