public async Task <IActionResult> UpdatePositionAsync([FromRoute] int id, [FromBody] PositionDto position) { _logger.LogInformation("UpdatePositionAsync() method start."); if (id != position.Id) { var message = $"The submitted id value '{id}' does not match the corresponding field contained in the submitted object Position id value '{position.Id}'."; _logger.LogError("Error in UpdatePositionAsync() method. " + message); return(BadRequest(message)); } // Get the Position var getByIdResponse = await Task.FromResult(_positionService.GetById(id)); if (getByIdResponse.HasErrors) { var message = $"Error in UpdatePositionAsync() method. Cannot find Position with id '{id}'. Error message returned from PositionService: {getByIdResponse.ErrorMessage}"; _logger.LogError(message); return(NotFound(getByIdResponse.ErrorMessage)); } var model = position.Map(); // Update Position var updateResponse = _positionService.Update(model); if (updateResponse.HasErrors) { var message = $"Error in UpdatePositionAsync() method. Cannot update Position with id '{id}'. Error message returned from PositionService: {updateResponse.ErrorMessage}"; _logger.LogError(message); return(NotFound(updateResponse.ErrorMessage)); } var dto = updateResponse.Result.Map(); _logger.LogInformation($"UpdatePositionAsync() method end with Position id '{id}'."); return(Ok(dto)); }
public async Task <IActionResult> CreatePositionAsync([FromBody] PositionDto position) { _logger.LogInformation("CreatePositionAsync() method start."); var model = position.Map(); // Add Position object to the database var addResponse = await Task.FromResult(_positionService.Add(model)); if (addResponse.HasErrors) { if (addResponse.Exception != null) { var exceptionMessage = $"Error in CreatePositionAsync() method. Position object may not have been added to database. Exception was thrown in PositionService. Exception Type: '{addResponse.Exception.GetType()}'. Exception Message: {addResponse.Exception.Message}"; _logger.LogError(exceptionMessage); return(BadRequest(addResponse.ErrorMessage)); } var message = $"Error in CreatePositionAsync() method. Position object may not have been added to database. Error message returned from PositionService: {addResponse.ErrorMessage}"; _logger.LogError(message); return(NotFound(addResponse.ErrorMessage)); } var addedObject = addResponse.Result; var dto = addedObject.Map(); var id = addedObject.Id; _logger.LogInformation($"CreatePositionAsync() method end with Position id '{id}'."); var actionResult = CreatedAtAction( actionName: nameof(GetPositionByIdAsync), // ASP.NET Core 3.0 bug: https://stackoverflow.com/questions/59288259/asp-net-core-3-0-createdataction-returns-no-route-matches-the-supplied-values //controllerName: ControllerContext.ActionDescriptor.ControllerName, routeValues: new { id }, value: dto); return(actionResult); }