Пример #1
0
        private static JsonResult CreateErrorResponse(string errorMessage)
        {
            var result = new FillWorkoutDetailsResult
            {
                Success = false,
                Message = errorMessage
            };

            return(new JsonResult(result));
        }
Пример #2
0
        public async Task <IActionResult> Post([FromBody] int workoutId) //string value
        {
            // With .Net Core 3.0 string parameters must be passed as quoted, so the string parameter is changed to integer (since contentType is "application/json")
            // Otherwise JavaScript must use JSON.stringify() to pass the parameters (also when the parameter is an object)
            // In case of mismatched types the middleware returns HTTP result 415 to the caller
            if (workoutId <= 0)
            //if (!int.TryParse(value, out int workoutId))
            {
                return(CreateErrorResponse($"Invalid workoutId ({workoutId})!"));
            }

            FillWorkoutDetailsResult result;

            try
            {
                //static IQueryable<Workout> queryModifier(IQueryable<Workout> q) => q.Include(w =>w.Intervals);
                var workout = await _workoutRepository.GetById(workoutId, new WorkoutWithIntervalsSpecification())
                              .ConfigureAwait(false);

                //var workout = await _workoutRepository.GetById(workoutId, w => w.Intervals)
                if (workout == null)
                {
                    return(CreateErrorResponse($"Workout with Id={workoutId} not found!"));
                }

                if (workout.Intervals?.Count > 0)
                {
                    return(CreateErrorResponse($"The details for workout with Id={workoutId} have already been loaded!"));
                }

                var loadWorkoutResult = await LoadWorkoutDetails(workout)
                                        .ConfigureAwait(false);

                if (!loadWorkoutResult.Success)
                {
                    return(CreateErrorResponse(loadWorkoutResult.Message));
                }

                WorkoutIntervalModifier.AutoFillIntervalTypes(workout);
                await _workoutRepository.Update(workout)
                .ConfigureAwait(false);

                var workoutData = new WorkoutDetailsData
                {
                    WorkoutId = workoutId,
                    Distance  = workout.Distance.ToStringInvariant(),
                    Duration  = DisplayValuesFormatter.FormatDuration(workout.Duration, false),
                    Name      = workout.Name,
                    Pace      = DisplayValuesFormatter.FormatDuration(workout.Pace, false),
                    StartDate = workout.WorkoutDate.ToShortDateString()
                };
                result = new FillWorkoutDetailsResult
                {
                    Success     = true,
                    WorkoutData = workoutData,
                    Message     = loadWorkoutResult.Message
                };

                return(new JsonResult(result));
            }
            catch (Exception ex)
            {
                return(CreateErrorResponse($"An error occurred: {ex.Message}"));
            }
        }