Beispiel #1
0
        public ActionResult SearchAuto(string name)
        {
            IQueryable <Auto> autos = _context.Autos;

            if (!String.IsNullOrEmpty(name))
            {
                autos = autos.Where(a => a.AutoName.Contains(name));
            }
            AutoListViewModel viewModel = new AutoListViewModel
            {
                Autos    = autos.ToList(),
                AutoNaim = name
            };

            return(View(viewModel));
        }
Beispiel #2
0
        public ViewResult List(string type, string searchString, int page = 1)
        {
            if (!String.IsNullOrEmpty(searchString))
            {
                AutoListViewModel model = new AutoListViewModel
                {
                    Autos = repository.Autos
                            .Where(b => type == null || b.Type == type)
                            .Where(b => b.Name.Contains(searchString))
                            .OrderBy(auto => auto.Id)
                            .Skip((page - 1) * pageSize)
                            .Take(pageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage  = page,
                        ItemsPerPage = pageSize,
                        TotalItems   = type == null?
                                       repository.Autos.Count() :
                                           repository.Autos.Where(auto => auto.Type == type).Count()
                    },
                    CurrentType = type
                };

                return(View(model));
            }
            else
            {
                AutoListViewModel model = new AutoListViewModel
                {
                    Autos = repository.Autos
                            .Where(p => type == null || p.Type == type)
                            .OrderBy(auto => auto.Id)
                            .Skip((page - 1) * pageSize)
                            .Take(pageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage  = page,
                        ItemsPerPage = pageSize,
                        TotalItems   = type == null?
                                       repository.Autos.Count() :
                                           repository.Autos.Where(auto => auto.Type == type).Count()
                    },
                    CurrentType = type
                };
                return(View(model));
            }
        }
Beispiel #3
0
        private List <AutoListViewModel> ConstruirListaAutosListViewModel(List <Auto> listaAutos)
        {
            List <AutoListViewModel> listaVm = new List <AutoListViewModel>();

            foreach (var auto in listaAutos)
            {
                AutoListViewModel autoVm = new AutoListViewModel
                {
                    AutoId     = auto.AutoId,
                    MovilId    = auto.MovilId,
                    Patente    = auto.Patente,
                    Tipo       = auto.Tipo.Descripcion,
                    Marca      = auto.Marca.NombreMarca,
                    Modelo     = auto.Modelo.NombreModelo,
                    Chofer     = auto.Usuario.NombreApellido,
                    Disponible = auto.Disponible
                };
                listaVm.Add(autoVm);
            }

            return(listaVm);
        }
Beispiel #4
0
        public void Can_Send_Pagination_View_Model()
        {
            //Arrange
            Mock <IAutoRepository> mock = new Mock <IAutoRepository>();

            mock.Setup(m => m.Autos).Returns(new List <Auto>
            {
                new Auto {
                    Id = 1, Name = "AUDI"
                },
                new Auto {
                    Id = 2, Name = "BENTLEY"
                },
                new Auto {
                    Id = 3, Name = "BMW"
                },
                new Auto {
                    Id = 4, Name = "FERRARI"
                },
                new Auto {
                    Id = 5, Name = "HYUNDAI"
                },
            });

            AutosController controller = new AutosController(mock.Object);

            controller.pageSize = 3;
            //Act
            AutoListViewModel result = (AutoListViewModel)controller.List(null, 2).Model;

            PagingInfo pagingInfo = result.PagingInfo;

            Assert.AreEqual(pagingInfo.CurrentPage, 2);
            Assert.AreEqual(pagingInfo.ItemsPerPage, 3);
            Assert.AreEqual(pagingInfo.TotalItems, 5);
            Assert.AreEqual(pagingInfo.TotalPages, 2);
        }
Beispiel #5
0
        public void Can_Paginate()
        {
            //Arrange
            Mock <IAutoRepository> mock = new Mock <IAutoRepository>();

            mock.Setup(m => m.Autos).Returns(new List <Auto>
            {
                new Auto {
                    Id = 1, Name = "AUDI"
                },
                new Auto {
                    Id = 2, Name = "BENTLEY"
                },
                new Auto {
                    Id = 3, Name = "BMW"
                },
                new Auto {
                    Id = 4, Name = "FERRARI"
                },
                new Auto {
                    Id = 5, Name = "HYUNDAI"
                },
            });

            AutosController controller = new AutosController(mock.Object);

            controller.pageSize = 3;
            //Act
            AutoListViewModel result = (AutoListViewModel)controller.List(null, 2).Model;
            //Assert
            List <Auto> autos = result.Autos.ToList();

            Assert.IsTrue(autos.Count == 2);
            Assert.AreEqual(autos[0].Name, "BENTLEY");
            Assert.AreEqual(autos[1].Name, "AUDI");
        }