Exemple #1
0
        public async Task <IActionResult> Index(FindAllCostumersQuery query)
        {
            if (!User.IsAdmin())
            {
                query.UserId = User.GetId();
            }

            var customers = await Mediator.Send(query);

            var genders = await FindAllGenders();

            var cities = await FindAllCities();

            var regions = await FindAllRegions();

            var classifications = await FindAllClassifications();

            var users = await FindAllUsers();

            ViewBag.Name = query.Name;
            ViewBag.LastPurchaseStart = query.LastPurchaseStart;
            ViewBag.LastPurchaseEnd   = query.LastPurchaseEnd;

            ViewBag.Genders         = new SelectList(genders, "Id", "Name", query.GenderId);
            ViewBag.Cities          = new SelectList(cities, "Id", "Name", query.CityId);
            ViewBag.Regions         = new SelectList(regions, "Id", "Name", query.RegionId);
            ViewBag.Classifications = new SelectList(classifications, "Id", "Name", query.ClassificationId);
            ViewBag.Users           = new SelectList(users, "Id", "Name", query.UserId);

            return(View(customers));
        }
        public async Task <IList <CustomerDTO> > Handle(FindAllCostumersQuery request, CancellationToken cancellationToken)
        {
            IQueryable <Customer> customers = _context.Customers.AsNoTracking();

            if (!string.IsNullOrEmpty(request.Name))
            {
                customers = customers.Where(a => a.Name.Contains(request.Name));
            }

            if (request.GenderId.HasValue)
            {
                customers = customers.Where(a => a.GenderId == request.GenderId);
            }

            if (request.CityId.HasValue)
            {
                customers = customers.Where(a => a.CityId == request.CityId);
            }

            if (request.RegionId.HasValue)
            {
                customers = customers.Where(a => a.RegionId == request.RegionId);
            }

            if (request.ClassificationId.HasValue)
            {
                customers = customers.Where(a => a.ClassificationId == request.ClassificationId);
            }

            if (request.UserId.HasValue)
            {
                customers = customers.Where(a => a.UserId == request.UserId);
            }

            if (request.LastPurchaseStart.HasValue)
            {
                customers = customers.Where(a => a.LastPurchase >= request.LastPurchaseStart);
            }

            if (request.LastPurchaseEnd.HasValue)
            {
                customers = customers.Where(a => a.LastPurchase <= request.LastPurchaseEnd);
            }

            return(await customers
                   .Select(a => new CustomerDTO()
            {
                Id = a.Id,
                Name = a.Name,
                Phone = a.Phone,
                ClassificationName = a.Classification.Name,
                GenderName = a.Gender.Name,
                CityName = a.City.Name,
                RegionName = a.Region.Name,
                LastPurchase = a.LastPurchase,
                UserName = a.User.Login
            })
                   .ToListAsync());
        }