Beispiel #1
0
        public async Task <IActionResult> GetById(int id, WorkoutHistoryOfmResourceParameters workoutHistoryOfmResourceParameters)
        {
            var stringGuid = User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;

            if (String.IsNullOrWhiteSpace(stringGuid))
            {
                return(Unauthorized());
            }
            var ownerGuid = new Guid(stringGuid);

            var ofmForGetQueryResult = await _asyncOfmRepository.GetById(id, workoutHistoryOfmResourceParameters, ownerGuid);

            if (!_controllerGuardClause.ValidateGetById(ofmForGetQueryResult, id, out ObjectResult objectResult))
            {
                return(objectResult);
            }
            var expandable       = ofmForGetQueryResult.ReturnedTOfmForGet.ToExpandableOfm();
            var shapedExpandable = expandable.Shape(workoutHistoryOfmResourceParameters.Fields);

            if (_incomingHeaders.IncludeHateoas)
            {
                shapedExpandable.Add("links", _hateoasLinkFactory.CreateLinksForOfmForGet(id, workoutHistoryOfmResourceParameters.Fields).ToList());
            }
            return(Ok(shapedExpandable));
        }
        public async Task <IActionResult> HistoryDetails(int id)
        {
            var resourceParameters = new WorkoutHistoryOfmResourceParameters()
            {
                IncludeExerciseHistories         = "1",
                IncludePreviousExerciseHistories = "1",
                IncludeCardioSets        = "1",
                IncludeWeightLiftingSets = "1"
            };
            var workoutHistoryViewModelQueryResult =
                await _workoutHistoryViewModelRepository.GetById(id, resourceParameters);

            if (workoutHistoryViewModelQueryResult.HttpStatusCode == HttpStatusCode.Unauthorized ||
                workoutHistoryViewModelQueryResult.HttpStatusCode == HttpStatusCode.Forbidden)
            {
                return(RedirectToAction("AccessDenied", "Authorization"));
            }

            WorkoutHistoryViewModel workoutHistoryViewModel = null;

            if ((int)workoutHistoryViewModelQueryResult.HttpStatusCode == 200)
            {
                workoutHistoryViewModel = workoutHistoryViewModelQueryResult.ViewModel;
            }

            return(View("HistoryDetails", workoutHistoryViewModel));
        }
        public async Task NotReturnSingleOfmGetById_WhenQueriedEntityFieldsAreErroneous()
        {
            // Arrange
            var asyncDataCrudMock = new Mock <IWorkoutHistoryRepository>();

            asyncDataCrudMock
            .Setup(s => s.GetById(It.IsAny <int>())).Returns(Task.FromResult(new WorkoutHistory()
            {
                Id        = 1,
                OwnerGuid = _ownerGuid
            }));

            var workoutHistoryOfmRepository = new WorkoutHistoryOfmRepository(asyncDataCrudMock.Object, new PropertyMappingService(), new TypeHelperService());

            // Act
            var workoutHistoryOfmResourceParameters = new WorkoutHistoryOfmResourceParameters()
            {
                Fields = "ThisFieldDoesntExistOnWorkoutHistory"
            };

            var workoutHistoryOfmResult = await workoutHistoryOfmRepository.GetById(1, workoutHistoryOfmResourceParameters, _ownerGuid);

            // Assert
            var actualOfmResult = JsonConvert.SerializeObject(workoutHistoryOfmResult,
                                                              new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented
            });

            var expectedOfmResult = JsonConvert.SerializeObject(
                new OfmForGetQueryResult <WorkoutHistoryOfmForGet>()
            {
                ReturnedTOfmForGet = null,
                ErrorMessages      = new List <string>()
                {
                    "A property named 'ThisFieldDoesntExistOnWorkoutHistory' does not exist"
                }
            },
                new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented
            });

            Assert.AreEqual(expectedOfmResult, actualOfmResult);
        }
Beispiel #4
0
        public async Task <OfmForGetQueryResult <WorkoutHistoryOfmForGet> > GetById(int id, WorkoutHistoryOfmResourceParameters resourceParameters, Guid ownerGuid)
        {
            var ofmForGetResult = new OfmForGetQueryResult <WorkoutHistoryOfmForGet>();

            ofmForGetResult = await AsyncGetOfmGuardClause.ValidateGetById(ofmForGetResult, resourceParameters.Fields); // Todo: Validate additional *Include* query parameters

            if (ofmForGetResult.ErrorMessages.Count > 0)
            {
                return(ofmForGetResult);
            }

            var workoutHistoryLinqToEntity = Repo.LinqToEntityQueryable();

            if (resourceParameters.IncludeExerciseHistories.ToBool())
            {
                workoutHistoryLinqToEntity =
                    workoutHistoryLinqToEntity
                    .Include(i => i.ExerciseHistories)
                    .ThenInclude(i => i.Exercise);

                if (resourceParameters.IncludeWeightLiftingSets.ToBool())
                {
                    workoutHistoryLinqToEntity =
                        workoutHistoryLinqToEntity
                        .Include(i => i.ExerciseHistories)
                        .ThenInclude(i => i.WeightLiftingSets);

                    if (resourceParameters.IncludePreviousExerciseHistories.ToBool())
                    {
                        workoutHistoryLinqToEntity = workoutHistoryLinqToEntity
                                                     .Include(i => i.ExerciseHistories)
                                                     .ThenInclude(i => i.WeightLiftingSets)
                                                     .Include(i => i.ExerciseHistories)
                                                     .ThenInclude(i => i.PreviousExerciseHistory)
                                                     .ThenInclude(i => i.WeightLiftingSets);
                    }
                }

                if (resourceParameters.IncludeCardioSets.ToBool())
                {
                    workoutHistoryLinqToEntity =
                        workoutHistoryLinqToEntity
                        .Include(i => i.ExerciseHistories)
                        .ThenInclude(i => i.CardioSets);

                    if (resourceParameters.IncludePreviousExerciseHistories.ToBool())
                    {
                        workoutHistoryLinqToEntity =
                            workoutHistoryLinqToEntity
                            .Include(i => i.ExerciseHistories)
                            .ThenInclude(i => i.CardioSets)
                            .Include(i => i.ExerciseHistories)
                            .ThenInclude(i => i.PreviousExerciseHistory)
                            .ThenInclude(i => i.CardioSets);
                    }
                }
            }

            var workoutHistory = workoutHistoryLinqToEntity.Include(i => i.Workout).FirstOrDefault(f => f.Id == id && f.OwnerGuid == ownerGuid);

            if (workoutHistory == null)
            {
                return(ofmForGetResult);
            }

            ofmForGetResult.ReturnedTOfmForGet = Mapper.Map <WorkoutHistoryOfmForGet>(workoutHistory);

            return(ofmForGetResult);
        }