Beispiel #1
0
        public async Task <IActionResult> UpdateActvitityAsync([FromRoute] int id, [FromBody] ActivityDto activity)
        {
            _logger.LogInformation("UpdateActvitityAsync() method start.");

            if (id != activity.Id)
            {
                var message = $"The submitted id value '{id}' does not match the corresponding field contained in the submitted object Activity id value '{activity.Id}'.";
                _logger.LogError("Error in UpdateActvitityAsync() method.  " + message);
                return(BadRequest(message));
            }

            // Get the Activity
            var getByIdResponse = await Task.FromResult(_activityService.GetById(id));

            if (getByIdResponse.HasErrors)
            {
                var message = $"Error in UpdateActvitityAsync() method.  Cannot find Activity with id '{id}'.  Error message returned from ActivityService: {getByIdResponse.ErrorMessage}";
                _logger.LogError(message);
                return(NotFound(getByIdResponse.ErrorMessage));
            }

            var model = activity.Map();

            // Update Activity
            var updateResponse = _activityService.Update(model);

            if (updateResponse.HasErrors)
            {
                var message = $"Error in UpdateActvitityAsync() method.  Cannot update Activity with id '{id}'.  Error message returned from ActivityService: {updateResponse.ErrorMessage}";
                _logger.LogError(message);
                return(NotFound(updateResponse.ErrorMessage));
            }

            var dto = updateResponse.Result.Map();

            _logger.LogInformation($"UpdateActvitityAsync() method end with Activity id '{id}'.");

            return(Ok(dto));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateActivityAsync([FromBody] ActivityDto activity)
        {
            _logger.LogInformation("CreateActivityAsync() method start.");

            var model = activity.Map();

            // Add Activity object to the database
            var addResponse = await Task.FromResult(_activityService.Add(model));

            if (addResponse.HasErrors)
            {
                if (addResponse.Exception != null)
                {
                    var exceptionMessage = $"Error in CreateActivityAsync() method.  Activity object may not have been added to database.  Exception was thrown in ActivityService.  Exception Type: '{addResponse.Exception.GetType()}'.  Exception Message: {addResponse.Exception.Message}";
                    _logger.LogError(exceptionMessage);
                    return(BadRequest(addResponse.ErrorMessage));
                }

                var message = $"Error in CreateActivityAsync() method.  Activity object may not have been added to database.  Error message returned from ActivityService: {addResponse.ErrorMessage}";
                _logger.LogError(message);
                return(NotFound(addResponse.ErrorMessage));
            }

            var addedObject = addResponse.Result;
            var dto         = addedObject.Map();
            var id          = addedObject.Id;

            _logger.LogInformation($"CreateActivityAsync() method end with Activity id '{id}'.");

            var actionResult = CreatedAtAction(
                actionName: nameof(GetActivityByIdAsync),                 // 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);
        }