| @@ -0,0 +1,51 @@ | |||
| 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; | |||
| } | |||
| } | |||
| } | |||
| @@ -13,6 +13,8 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| Task<PatternResponseDto> GetByIdAsync(int id); | |||
| Task<List<PatternResponseDto>> GetFilteredPatternsAsync(FilterPatternDto filterPatternDto); | |||
| Task CreateAsync(PatternCreateDto patternCreateDto); | |||
| Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id); | |||
| @@ -51,6 +51,19 @@ namespace Diligent.WebAPI.Business.Services | |||
| 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) | |||
| { | |||
| _logger.LogInformation($"Start creating Pattern"); | |||
| @@ -0,0 +1,17 @@ | |||
| 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; } | |||
| } | |||
| } | |||
| @@ -22,6 +22,10 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| public async Task<IActionResult> GetById([FromRoute] int id) => | |||
| Ok(await _patternService.GetByIdAsync(id)); | |||
| [HttpGet("filter")] | |||
| public async Task<IActionResult> GetFilteredPatterns([FromQuery] FilterPatternDto request) => | |||
| Ok(await _patternService.GetFilteredPatternsAsync(request)); | |||
| [HttpPost] | |||
| public async Task<IActionResult> Create([FromBody] PatternCreateDto request) | |||
| { | |||