Exemplo n.º 1
0
        public ActionResult Index(int?page, int?categoryId, string searchName,
                                  bool newApplications = false, bool myOrders = false, OrderSorts sort = OrderSorts.New)
        {
            var ordersDto = _orderService.GetAll();

            if (ordersDto == null)
            {
                return(HttpNotFound());
            }
            if (newApplications && !ordersDto.Any(o => !o.AdminStatus))
            {
                ViewBag.AdminMessage = "No new orders";
            }

            ViewBag.NewOrdersAmountString = string.Empty;
            var newOrdersAmount = ordersDto.Count(model => !model.AdminStatus);

            if (newOrdersAmount > 0)
            {
                ViewBag.NewOrdersAmountString = " + " + newOrdersAmount;
            }

            ViewBag.IsNewPage = false;
            if (newApplications)
            {
                ordersDto         = ordersDto.Where(model => !model.AdminStatus);
                ViewBag.IsNewPage = true;
            }
            else
            {
                ordersDto = ordersDto.Where(model => model.AdminStatus);
            }

            ViewBag.IsMyOrdersPage = false;
            if (myOrders)
            {
                ordersDto = _orderService.GetAll();
                ordersDto = ordersDto.Where(model => model.UserId == User.Identity.GetUserId <int>());

                if (!ordersDto.Any())
                {
                    ViewBag.Message = "You have no orders";
                }
                ViewBag.IsMyOrdersPage = true;
            }

            ViewBag.ItemsAmount = ordersDto.Count();
            var categoriesDto = _categoryService.GetAll().ToList();
            var categories    = new List <CategoryViewModel>();

            foreach (var category in categoriesDto)
            {
                categories.Add(new CategoryViewModel()
                {
                    Id          = category.Id,
                    Name        = category.Name,
                    ItemsAmount = ordersDto.Count(o =>
                                                  newApplications ? o.AdminStatus != newApplications && o.CategoryId == category.Id :
                                                  ((myOrders ? o.UserId == User.Identity.GetUserId <int>() : o.AdminStatus) && o.CategoryId == category.Id)
                                                  )
                });
            }
            ViewBag.CategoriesList = categories;

            if (categoryId != null)
            {
                ordersDto = ordersDto.Where(o => o.CategoryId == categoryId);
                ViewBag.CurrentCategoryId = categoryId;
            }

            if (searchName != null)
            {
                ordersDto = ordersDto.Where(o => o.Name.ToLower().Contains(searchName.ToLower()));
            }
            ViewBag.SearchName = searchName;
            if (!ordersDto.Any())
            {
                ViewBag.SearchMessage = "No orders found";
            }

            switch (sort)
            {
            case OrderSorts.New:
                ordersDto = ordersDto.OrderByDescending(o => o.UploadDate); break;

            case OrderSorts.Urgent:
                ordersDto = ordersDto.OrderBy(o => o.CompletionDate); break;

            case OrderSorts.Expensive:
                ordersDto = ordersDto.OrderByDescending(o => o.Price); break;

            case OrderSorts.Active:
                ordersDto = ordersDto.OrderBy(o => o.StatusId); break;
            }
            ViewBag.Sort = sort;

            Mapper.Initialize(cfg => cfg.CreateMap <OrderViewModelBLL, IndexOrderViewModel>()
                              .ForMember("Category", opt => opt.MapFrom(c => _categoryService.FindById(c.CategoryId).Name))
                              .ForMember("Status", opt => opt.MapFrom(c => _statusService.FindById(c.StatusId).Value))
                              .ForMember("Image", opt => opt.MapFrom(c => c.PictureId == null
                    ? System.IO.File.ReadAllBytes(Server.MapPath(DefaultImageName))
                    : _pictureService.FindById(c.PictureId.Value).Image))
                              );
            var orders = Mapper.Map <IEnumerable <OrderViewModelBLL>, List <IndexOrderViewModel> >(ordersDto);

            var orderNames    = _orderService.GetAll().Select(o => o.Name).ToArray();
            var filteredNames = orderNames.Where(o => o.IndexOf(searchName, StringComparison.InvariantCultureIgnoreCase) >= 0);

            ViewBag.Names = Json(filteredNames, JsonRequestBehavior.AllowGet);

            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            return(View(orders.ToPagedList(pageNumber, pageSize)));
        }
Exemplo n.º 2
0
        public ActionResult Index(int?page, int?categoryId, string searchName,
                                  bool newApplications = false, PerformerSorts sort = PerformerSorts.New)
        {
            var perfomersDto = _userService.GetAll().ToList().Where(p => p.IsPerformer);

            if (newApplications && !perfomersDto.Any(o => !o.AdminStatus))
            {
                ViewBag.AdminMessage = "No new performers";
            }

            ViewBag.NewPerformerssAmountString = string.Empty;
            var newPerformersAmount = perfomersDto.Count(model => !model.AdminStatus);

            if (newPerformersAmount > 0)
            {
                ViewBag.NewPerformersAmountString = " + " + newPerformersAmount;
            }

            ViewBag.IsNewPage = false;
            if (newApplications)
            {
                perfomersDto      = perfomersDto.Where(model => !model.AdminStatus);
                ViewBag.IsNewPage = true;
            }
            else
            {
                perfomersDto = perfomersDto.Where(model => model.AdminStatus);
            }

            ViewBag.ItemsAmount = perfomersDto.Count();
            var categoriesDto = _categoryService.GetAll().ToList();

            Mapper.Initialize(cfg => cfg.CreateMap <CategoryViewModelBLL, CategoryViewModel>()
                              .ForMember("ItemsAmount", opt => opt.MapFrom(c => c.Performers.Count(o =>
                                                                                                   o.IsPerformer && o.AdminStatus != newApplications
                                                                                                   )))
                              );
            var categories = Mapper.Map <List <CategoryViewModelBLL>, List <CategoryViewModel> >(categoriesDto);

            ViewBag.CategoriesList = categories;

            if (categoryId != null)
            {
                perfomersDto = perfomersDto.Where(o => o.CategoriesBll.Select(c => c.Id).Contains(categoryId.Value));
                ViewBag.CurrentCategoryId = categoryId;
            }

            if (searchName != null)
            {
                perfomersDto = perfomersDto.Where(o => o.Name.ToLower().Contains(searchName.ToLower()) ||
                                                  o.Surname.ToLower().Contains(searchName.ToLower()) || o.Company.ToLower().Contains(searchName.ToLower()));
            }
            ViewBag.SearchName = searchName;
            if (!perfomersDto.Any())
            {
                ViewBag.SearchMessage = "No performers found";
            }

            switch (sort)
            {
            case PerformerSorts.New:
                perfomersDto = perfomersDto.OrderByDescending(o => o.RegistrationDate);
                break;

            case PerformerSorts.Best:
                perfomersDto = perfomersDto.OrderBy(o => o.Rating);
                break;
            }
            ViewBag.Sort = sort;

            Mapper.Initialize(cfg => cfg.CreateMap <ClientViewModelBLL, IndexPerformerViewModel>()
                              .ForMember("Name", opt => opt.MapFrom(c => c.Surname + " " + c.Name))
                              .ForMember("Category", opt => opt.MapFrom(c => c.CategoriesBll.First().Name +
                                                                        (c.CategoriesBll.Count > 1 ? " + " + (c.CategoriesBll.Count - 1).ToString() + " more" : string.Empty)))
                              .ForMember("Image", opt => opt.MapFrom(c => c.PictureId == null
                    ? System.IO.File.ReadAllBytes(Server.MapPath(DefaultImageName))
                    : _pictureService.FindById(c.PictureId.Value).Image))
                              );
            var performers = Mapper.Map <IEnumerable <ClientViewModelBLL>, List <IndexPerformerViewModel> >(perfomersDto);

            var performerNames = performers.Select(o => o.Name).ToArray();
            var filteredNames  = performerNames.Where(o => o.IndexOf(searchName, StringComparison.InvariantCultureIgnoreCase) >= 0);

            ViewBag.Names = Json(filteredNames, JsonRequestBehavior.AllowGet);

            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            return(View(performers.ToPagedList(pageNumber, pageSize)));
        }