Example #1
0
        public async Task <IActionResult> UpdateUser([FromRoute] Guid id, [FromBody] UserToUpdateDto dto)
        {
            if (dto is null)
            {
                return(BadRequest());
            }
            UserDto outerFacingModelUser;

            User resourceEntity = await _userQuery.ExecuteGetResourceByIdWithTracking(id);

            if (resourceEntity is null)
            {
                outerFacingModelUser = _userCommand.ExecuteAddResource(dto, id);

                if (!(await _userCommand.ExecuteSaveAsync()))
                {
                    throw new Exception("There was a problem with saving the user resource");
                }

                outerFacingModelUser
                .ShapeData(null)
                .TryAdd("links", CreateLinks(id, null));

                return(CreatedAtRoute("GetUser", new { id = id }, outerFacingModelUser));
            }


            //if it does update the user with tracking
            _userCommand.ExecuteUpdateResource(resourceEntity, dto);

            //then save changes
            if (!(await _userCommand.ExecuteSaveAsync()))
            {
                throw new Exception("There was a problem with saving the user resource");
            }

            Response.Headers.Add("Location", _urlHelper.Link("GetUser", new { id = resourceEntity.Id }));
            return(NoContent());
        }