public async Task <IActionResult> CreateUserAndPersonAsync([FromBody] Person person) { if (person == null) { return(BadRequest()); } if (await _personRepository.InsertAsync(person) == -1) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to create person (from user creation)")); } var user = new User() { Id = person.Id }; if (await _userRepository.InsertAsync(user) == -1) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to create user - Warning (person created)")); } //comment: // User currently does not contain any field -> No need to update. // If update of User is needed, replace the Person object in '[FromBody] Person' with a DTO with both that implements IPerson and IUser and use Automapper to create Person and User objects _log.LogCreated(HttpContext.Request.Method, HttpContext.Request.Path, multipleObjectsToJson(user, person).ToString()); return(CreatedAtRoute(new { person.Id }, person)); }
public async Task <IActionResult> CreateAsync(int personId, [FromBody] Address address) { if (address == null) { return(BadRequest()); } if (await _personRepository.GetAsync(personId) == null) { return(NotFound()); } address.PersonId = personId; if (await _addressRepository.InsertAsync(address) == -1) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to create person")); } _log.LogCreated(HttpContext.Request.Method, HttpContext.Request.Path, JsonConvert.SerializeObject(address).ToString()); return(CreatedAtRoute(nameof(this.GetAddressAsync), new { personId = address.PersonId, id = address.Id }, address)); }
public async Task <IActionResult> CreatePersonAsync([FromBody] Person person) { if (person == null) { return(BadRequest()); } if (await _personRepository.InsertAsync(person) == -1) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to create person")); } _log.LogCreated(HttpContext.Request.Method, HttpContext.Request.Path, JsonConvert.SerializeObject(person).ToString()); //var response = Request.CreateResponse(HttpStatusCode.Created); //// Generate a link to the new book and set the Location header in the response. //string uri = Url.Link("GetBookById", new { id = book.BookId }); //response.Headers.Location = new Uri(uri); //https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 return(CreatedAtRoute(nameof(GetPersonAsync), new { person.Id }, person)); }