示例#1
0
        public async Task <IActionResult> CreateUser([FromBody] UserToCreateDto dto)
        {
            if (dto is null)
            {
                return(BadRequest());
            }

            var outerFacingModelUser = _userCommand.ExecuteAddResource(dto);

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

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

            return(CreatedAtRoute("GetUser",
                                  new { id = outerFacingModelUser.Id }, outerFacingModelUser));
        }
        public async Task <IActionResult> CreateUsers([FromBody] IEnumerable <UserToCreateDto> userToCreateDtos)
        {
            if (userToCreateDtos is null)
            {
                return(BadRequest("Need user(s) resource in request body."));
            }

            List <UserDto> outerFacingModelUsers = new List <UserDto>();

            foreach (UserToCreateDto user in userToCreateDtos)
            {
                outerFacingModelUsers.Add(_userCommand.ExecuteAddResource(user));
            }

            if (!(await _userCommand.ExecuteSaveAsync()))
            {
                throw new Exception("Server had problems saving user resources");
            }

            var idsAsString = string.Join(",", outerFacingModelUsers.Select(u => u.Id));


            return(CreatedAtRoute("GetUsersById", new { ids = idsAsString }, outerFacingModelUsers));
        }