Exemplo n.º 1
0
        public void Can_Paginate()
        {
            Mock <IExerciseRepository> mock = new Mock <IExerciseRepository>();

            mock.Setup(m => m.Exercises).Returns(new Exercise[]
            {
                new Exercise {
                    ExerciseID = 1, Name = "E1"
                },
                new Exercise {
                    ExerciseID = 2, Name = "E2"
                },
                new Exercise {
                    ExerciseID = 3, Name = "E3"
                },
                new Exercise {
                    ExerciseID = 4, Name = "E4"
                },
                new Exercise {
                    ExerciseID = 5, Name = "E5"
                }
            });

            ExerciseController controller = new ExerciseController(mock.Object);

            controller.PageSize = 3;

            ExercisesListViewModel result = (ExercisesListViewModel)controller.List(null, 2).Model;

            Exercise[] exArray = result.Exercises.ToArray();
            Assert.IsTrue(exArray.Length == 2);
            Assert.AreEqual(exArray[0].Name, "E4");
            Assert.AreEqual(exArray[1].Name, "E5");
        }
Exemplo n.º 2
0
        public IActionResult Search(SearchExerciseInputModel inputModel, int id = 1)
        {
            if (id <= 0)
            {
                return(this.NotFound());
            }

            const int ItemsPerPage = 15;

            var result = this.exercisesService.GetAllSearched <ExerciseViewModel>(inputModel, id);

            if (result.Count == 0)
            {
                return(this.NotFound());
            }

            var viewModel = new ExercisesListViewModel
            {
                PageNumber   = id,
                ItemsPerPage = ItemsPerPage,
                TotalCount   = result.Count,
                Exercises    = result.Exercises,
                Name         = inputModel.Name,
                BodyPart     = inputModel.BodyPart,
                Difficulty   = inputModel.Difficulty,
                ExerciseType = inputModel.ExerciseType,
                EquipmentId  = inputModel.EquipmentId,
            };

            return(this.View(viewModel));
        }
Exemplo n.º 3
0
        public IActionResult All(int id = 1)
        {
            if (id <= 0)
            {
                return(this.NotFound());
            }

            const int ItemsPerPage = 14;

            var viewModel = new ExercisesListViewModel
            {
                ItemsPerPage = ItemsPerPage,
                PageNumber   = id,
                TotalCount   = this.exercisesService.GetCount(),
                Exercises    = this.exercisesService.GetAll <ExerciseViewModel>(id, ItemsPerPage),
            };

            return(this.View(viewModel));
        }
        public ViewResult List(string category, int page = 1)
        {
            ExercisesListViewModel model = new ExercisesListViewModel
            {
                Exercises = exerciseRepository.Exercises
                            .Where(e => category == null || e.Category.Name == category)
                            .OrderBy(e => e.ExerciseID)
                            .Skip((page - 1) * PageSize)
                            .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage  = page,
                    ItemsPerPage = PageSize,
                    TotalItems   = category == null?
                                   exerciseRepository.Exercises.Count() :
                                       exerciseRepository.Exercises.Where(e => e.Category.Name == category).Count()
                },
                CurrentCategory = category
            };

            return(View(model));
        }
Exemplo n.º 5
0
        public void Can_Send_Pagination_View_Model()
        {
            Mock <IExerciseRepository> mock = new Mock <IExerciseRepository>();

            mock.Setup(m => m.Exercises).Returns(new Exercise[]
            {
                new Exercise {
                    ExerciseID = 1, Name = "E1"
                },
                new Exercise {
                    ExerciseID = 2, Name = "E2"
                },
                new Exercise {
                    ExerciseID = 3, Name = "E3"
                },
                new Exercise {
                    ExerciseID = 4, Name = "E4"
                },
                new Exercise {
                    ExerciseID = 5, Name = "E5"
                }
            });

            ExerciseController controller = new ExerciseController(mock.Object);

            controller.PageSize = 3;

            ExercisesListViewModel result = (ExercisesListViewModel)controller.List(null, 2).Model;

            PagingInfo pageInfo = result.PagingInfo;

            Assert.AreEqual(pageInfo.CurrentPage, 2);
            Assert.AreEqual(pageInfo.ItemsPerPage, 3);
            Assert.AreEqual(pageInfo.TotalItems, 5);
            Assert.AreEqual(pageInfo.TotalPages, 2);
        }