using Diligent.WebAPI.Contracts.DTOs.Pattern; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Diligent.WebAPI.Business.Extensions { [ExcludeFromCodeCoverage] public static class PatternExtension { public static List FilterApplicants(this List query, FilterPatternDto filterPatternDto) { return query.FilterByDate(filterPatternDto.FromDate, filterPatternDto.ToDate) .FilterBySelectionLevels(filterPatternDto.SelectionLevels) .ToList(); } private static List FilterByDate(this List query, DateTime? fromDate, DateTime? toDate) { if(fromDate == null && toDate == null) return query; if(fromDate == null && toDate != null) return query.Where(x => x.CreatedAt <= toDate).ToList(); if ((fromDate != null && toDate == null) || (fromDate > toDate)) return query.Where(x => x.CreatedAt >= fromDate).ToList(); return query.Where(x => x.CreatedAt >= fromDate && x.CreatedAt < toDate).ToList(); } private static List FilterBySelectionLevels(this List query, int[]? selectionLevels) { if (selectionLevels is null) { return query; } List filteredPatterns = new(); for (int i = 0; i < query.Count; i++) { for(int j = 0; j < selectionLevels.Length; j++) { if (query[i].SelectionLevelId == selectionLevels[j]) { filteredPatterns.Add(query[i]); break; } } } return filteredPatterns; } } }