public async Task <IActionResult> List(FilterClientDto filterClientDto) { if (!ModelState.IsValid) { return(View()); } var clients = await _clientService.FilterClientsAsync(filterClientDto); ViewBag.Clients = clients; return(View()); }
public async Task <IEnumerable <Client> > FilterAsync(FilterClientDto filterClientDto) { int minId = filterClientDto.MinId ?? 0; int maxId = filterClientDto.MaxId ?? 0; int filters = 0; List <Client> result = new List <Client>(); if (minId > 0 || maxId > 0) { var filteredMinIds = await _db.Clients.Where(c => c.Id >= minId).ToListAsync(); var filteredMaxIds = await _db.Clients.Where(c => c.Id <= maxId).ToListAsync(); if (minId > 0 && maxId == 0) { result.AddRange(filteredMinIds); } else if (maxId > 0 && minId == 0) { result.AddRange(filteredMaxIds); } result.AddRange(filteredMaxIds.Intersect(filteredMinIds)); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.FirstName)) { var filteredFirstNames = await _db.Clients.Where(c => EF.Functions.Like(c.FirstName, $"%{filterClientDto.FirstName}%")).ToListAsync(); result.AddRange(filteredFirstNames); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.LastName)) { var filteredLastNames = await _db.Clients.Where(c => EF.Functions.Like(c.LastName, $"%{filterClientDto.LastName}%")).ToListAsync(); result.AddRange(filteredLastNames); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.PhoneNumber)) { var filteredPhoneNumbers = await _db.Clients.Where(c => EF.Functions.Like(c.PhoneNumber, $"%{filterClientDto.PhoneNumber}%")).ToListAsync(); result.AddRange(filteredPhoneNumbers); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.Email)) { var filteredEmails = await _db.Clients.Where(c => EF.Functions.Like(c.Email, $"%{filterClientDto.Email}%")).ToListAsync(); result.AddRange(filteredEmails); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.PaymentMethod)) { var filteredPaymentMethods = await _db.Clients.Where(c => EF.Functions.Like(c.PaymentMethod, $"%{filterClientDto.PaymentMethod}%")).ToListAsync(); result.AddRange(filteredPaymentMethods); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.CreditCard)) { var filteredCreditCards = await _db.Clients.Where(c => EF.Functions.Like(c.CreditCard, $"%{filterClientDto.CreditCard}%")).ToListAsync(); result.AddRange(filteredCreditCards); filters += 1; } if (!string.IsNullOrEmpty(filterClientDto.IdDocumentNumber)) { var filteredDocumentNumbers = await _db.Clients.Where(c => EF.Functions.Like(c.IdDocumentNumber, $"%{filterClientDto.IdDocumentNumber}%")).ToListAsync(); result.AddRange(filteredDocumentNumbers); filters += 1; } var groupedResults = result.GroupBy(r => r); List <Client> finalResult = new List <Client>(); foreach (var gr in groupedResults) { if (gr.Count() == filters) { finalResult.Add(gr.Key); } } return(finalResult); }
public async Task <List <GetClientDto> > FilterClientsAsync(FilterClientDto filterClientDto) { var filteredClients = await _repository.FilterAsync(filterClientDto); return(filteredClients.Select(c => _converter.ClientToGetClientDto(c)).ToList()); }