public ActionResult Index(PagerParameters pagerParameters, CustomersSearchVM search)
        {
            // Create a basic query that selects all customer content items, joined with the UserPartRecord table
            var customerQuery = _customerService.GetCustomers().Join <UserPartRecord>().List();

            // If the user specified a search expression, update the query with a filter
            if (!string.IsNullOrWhiteSpace(search.Expression))
            {
                var expression = search.Expression.Trim();

                customerQuery = from customer in customerQuery
                                where
                                customer.FirstName.Contains(expression, StringComparison.InvariantCultureIgnoreCase) ||
                                customer.LastName.Contains(expression, StringComparison.InvariantCultureIgnoreCase) ||
                                customer.As <UserPart>().Email.Contains(expression)
                                select customer;
            }

            // Project the query into a list of customer shapes
            var customersProjection = from customer in customerQuery
                                      select Shape.Customer
                                      (
                Id : customer.Id,
                FirstName : customer.FirstName,
                LastName : customer.LastName,
                PhoneNumber : customer.PhoneNumber,
                Email : customer.As <UserPart>().Email,
                CreatedAt : customer.CreatedUtc
                                      );

            // The pager is used to apply paging on the query and to create a PagerShape
            var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize);

            // Apply paging
            var customers = customersProjection.Skip(pager.GetStartIndex()).Take(pager.PageSize);

            // Construct a Pager shape
            var pagerShape = Shape.Pager(pager).TotalItemCount(customerQuery.Count());

            // Create the viewmodel
            var model = new CustomersIndexVM(customers, search, pagerShape);

            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult CustomerList()
        {
            // var customerQuery = _customerService.GetCustomers().Join<UserPartRecord>().List();
            var customerQuery = _customerService.GetCustomers().List();

            // Project the query into a list of customer shapes
            var customersProjection = from customer in customerQuery
                                      select Shape.Customer
                                      (
                Id : customer.Id,
                FirstName : customer.FirstName,
                LastName : customer.LastName,
                Email : customer.EmailAddress
                                      );

            var model = new CustomersIndexVM(customersProjection);

            return(View("CustomerList", model));
        }
Exemplo n.º 3
0
        public ActionResult Index(string sortOrder)
        {
            LibraryManagementSystemContext context             = new LibraryManagementSystemContext();
            CustomersRepository            customersRepository = new CustomersRepository(context);
            CustomersIndexVM model = new CustomersIndexVM();

            this.TryUpdateModel(model);

            model.CustomersPager                   = model.CustomersPager ?? new GenericPagerVM();
            model.CustomersPager.CurrentPage       = model.CustomersPager.CurrentPage == 0 ? 1 : model.CustomersPager.CurrentPage;
            model.CustomersPager.Action            = "Index";
            model.CustomersPager.Controller        = "Customers";
            model.CustomersPager.Prefix            = "CustomersPager";
            model.CustomersPager.CurrentParameters = new Dictionary <string, object>()
            {
                { "PersonaNumber", model.PersonalNumber },
                { "CustomerName", model.CustomerName },
                { "Email", model.Email },
                { "Address", model.Address },
                { "BirthdayStartDate", model.BirthdayStartDate },
                { "BirthdayEndDate", model.BirthdayEndDate },
                { "DateInStartDate", model.DateInStartDate },
                { "DateInEndDate", model.DateInEndDate },
                { "DateOutStartDate", model.DateOutStartDate },
                { "DateOutEndDate", model.DateOutEndDate },
                { "CustomersPager.CurrentPage", model.CustomersPager.CurrentPage }
            };

            #region Sorting and Filtering
            Expression <Func <Customer, bool> > filter = c =>
                                                         (model.PersonalNumber == default(int) || c.PersonalNumber == model.PersonalNumber) &&
                                                         (string.IsNullOrEmpty(model.CustomerName) || (c.FirstName.Contains(model.CustomerName) || c.LastName.Contains(model.CustomerName))) &&
                                                         (string.IsNullOrEmpty(model.Email) || c.Email.Contains(model.Email)) &&
                                                         (string.IsNullOrEmpty(model.Address) || c.Address.Contains(model.Address)) &&
                                                         (model.BirthdayStartDate == default(DateTime) || (c.Birthday == model.BirthdayStartDate || (c.Birthday >= model.BirthdayStartDate && c.Birthday <= model.BirthdayEndDate))) &&
                                                         (model.DateInStartDate == default(DateTime) || (c.DateIn == model.DateInStartDate || (c.DateIn >= model.DateInStartDate && c.Birthday <= model.DateInEndDate))) &&
                                                         (model.DateOutStartDate == default(DateTime) || (c.DateOut == model.DateOutStartDate || (c.DateOut >= model.DateOutStartDate && c.Birthday <= model.DateOutEndDate)));
            model.CustomersPager.PagesCount = GetPagesCount(filter);

            ViewBag.NameSortParam           = string.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.PersonalNumberSortParam = sortOrder == "PersonalNumber" ? "personalNumber_desc" : "PersonalNumber";
            ViewBag.EmailSortParam          = sortOrder == "Email" ? "email_desc" : "Email";
            ViewBag.AddressSortParam        = sortOrder == "Address" ? "address_desc" : "Address";
            ViewBag.BirthdaySortParam       = sortOrder == "Birthday" ? "birthday_desc" : "Birthday";
            ViewBag.DateInSortParam         = sortOrder == "DateIn" ? "dateIn_desc" : "DateIn";
            ViewBag.DateOutSortParam        = sortOrder == "DateOut" ? "dateOut_desc" : "DateOut";
            switch (sortOrder)
            {
            case "name_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.FirstName));
                break;

            case "PersonalNumber":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.PersonalNumber));
                break;

            case "personalNumber_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.PersonalNumber));
                break;

            case "Email":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Email));
                break;

            case "email_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Email));
                break;

            case "Address":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Address));
                break;

            case "address_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Address));
                break;

            case "Birthday":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.Birthday));
                break;

            case "birthday_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.Birthday));
                break;

            case "DateIn":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.DateIn));
                break;

            case "dateIn_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.DateIn));
                break;

            case "DateOut":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.DateOut));
                break;

            case "dateOut_desc":
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderByDescending(c => c.DateOut));
                break;

            default:
                model.CustomersList = customersRepository
                                      .GetAll(model.CustomersPager.CurrentPage, ApplicationConfiguration.ItemsPerPage, filter, order: x => x.OrderBy(c => c.FirstName));
                break;
            }
            #endregion

            return(View(model));
        }