| 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 | |||||
| { | |||||
| public static class PatternExtension | |||||
| { | |||||
| public static List<Pattern> FilterApplicants(this List<Pattern> query, FilterPatternDto filterPatternDto) | |||||
| { | |||||
| return query.FilterByDate(filterPatternDto.FromDate, filterPatternDto.ToDate) | |||||
| .FilterBySelectionLevels(filterPatternDto.SelectionLevels) | |||||
| .ToList(); | |||||
| } | |||||
| private static List<Pattern> FilterByDate(this List<Pattern> 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<Pattern> FilterBySelectionLevels(this List<Pattern> query, int[]? selectionLevels) | |||||
| { | |||||
| if (selectionLevels is null) | |||||
| { | |||||
| return query; | |||||
| } | |||||
| List<Pattern> 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; | |||||
| } | |||||
| } | |||||
| } |
| Task<PatternResponseDto> GetByIdAsync(int id); | Task<PatternResponseDto> GetByIdAsync(int id); | ||||
| Task<List<PatternResponseDto>> GetFilteredPatternsAsync(FilterPatternDto filterPatternDto); | |||||
| Task CreateAsync(PatternCreateDto patternCreateDto); | Task CreateAsync(PatternCreateDto patternCreateDto); | ||||
| Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id); | Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id); |
| return result; | return result; | ||||
| } | } | ||||
| public async Task<List<PatternResponseDto>> GetFilteredPatternsAsync(FilterPatternDto filterPatternDto) | |||||
| { | |||||
| _logger.LogInformation($"Start getting all filtered Patterns"); | |||||
| _logger.LogInformation("Getting data from DB"); | |||||
| var filteredPatterns = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync(); | |||||
| _logger.LogInformation($"Received {filteredPatterns.Count} patterns from db."); | |||||
| _logger.LogInformation($"Mapping received patterns to PatternResponseDto"); | |||||
| List<PatternResponseDto> result = _mapper.Map<List<PatternResponseDto>>(filteredPatterns.FilterApplicants(filterPatternDto)); | |||||
| _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns"); | |||||
| return result; | |||||
| } | |||||
| public async Task CreateAsync(PatternCreateDto patternCreateDto) | public async Task CreateAsync(PatternCreateDto patternCreateDto) | ||||
| { | { | ||||
| _logger.LogInformation($"Start creating Pattern"); | _logger.LogInformation($"Start creating Pattern"); |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Pattern | |||||
| { | |||||
| public class FilterPatternDto | |||||
| { | |||||
| public DateTime? FromDate { get; set; } | |||||
| public DateTime? ToDate { get; set; } | |||||
| public int[]? SelectionLevels { get; set; } | |||||
| } | |||||
| } |
| public async Task<IActionResult> GetById([FromRoute] int id) => | public async Task<IActionResult> GetById([FromRoute] int id) => | ||||
| Ok(await _patternService.GetByIdAsync(id)); | Ok(await _patternService.GetByIdAsync(id)); | ||||
| [HttpGet("filter")] | |||||
| public async Task<IActionResult> GetFilteredPatterns([FromQuery] FilterPatternDto request) => | |||||
| Ok(await _patternService.GetFilteredPatternsAsync(request)); | |||||
| [HttpPost] | [HttpPost] | ||||
| public async Task<IActionResult> Create([FromBody] PatternCreateDto request) | public async Task<IActionResult> Create([FromBody] PatternCreateDto request) | ||||
| { | { |