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.Services { public class PatternService : IPatternService { private readonly DatabaseContext _context; private readonly IMapper _mapper; private readonly ISelectionLevelService _selectionLevelService; private readonly ILogger _logger; public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger logger) { _context = context; _mapper = mapper; _selectionLevelService = selectionLevelService; _logger = logger; } public async Task> GetAllAsync() { _logger.LogInformation("Start getting all Patterns"); _logger.LogInformation("Getting data from DB"); var fromDb = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync(); _logger.LogInformation($"Received {fromDb.Count} patterns from db."); _logger.LogInformation($"Mapping received patterns to PatternResponseDto"); var result = _mapper.Map>(fromDb); _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns"); return result; } public async Task GetByIdAsync(int id) { _logger.LogInformation($"Start searching Pattern with id = {id}"); var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync(); if (pattern is null) { _logger.LogError($"Pattern with id = {id} not found"); throw new EntityNotFoundException("Pattern not found"); } _logger.LogInformation($"Mapping Pattern with id = {id}"); PatternResponseDto result = _mapper.Map(pattern); _logger.LogInformation($"Pattern with id = {id} mapped successfully"); return result; } public async Task> 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 result = _mapper.Map>(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"); _logger.LogInformation($"Check is Pattern in database"); var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync(); if (patternExists is not null) { _logger.LogError($"Pattern already exists in database"); throw new EntityNotFoundException("Pattern already exists in database"); } _logger.LogInformation($"Pattern is not in database"); _logger.LogInformation($"Mapping PatternCreateDto to model"); var pattern = _mapper.Map(patternCreateDto); _logger.LogInformation($"Pattern mapped successfully"); _logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}"); var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId); if(selectionLevel == null) { _logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found"); throw new EntityNotFoundException("Selection level not found"); } _logger.LogInformation($"Add founded SelectionLevel to pattern"); pattern.SelectionLevel = selectionLevel; await _context.AddAsync(pattern); _logger.LogInformation($"Saving Ad to db..."); var result = _context.SaveChangesAsync(); _logger.LogInformation($"Saved Ad to db..."); await result; } public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id) { _logger.LogInformation($"Start updating Pattern"); _logger.LogInformation($"Check is Pattern in database"); var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync(); if (patternExists is not null) { _logger.LogError($"Pattern already exists in database"); throw new EntityNotFoundException("Pattern already exists in database"); } _logger.LogInformation($"Start searching Pattern with id = {id}"); var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync(); if (pattern is null) { _logger.LogError($"Pattern with id = {id} not found"); throw new EntityNotFoundException("Pattern not found"); } _logger.LogInformation($"Mapping Pattern with id = {id}"); _mapper.Map(patternUpdateDto, pattern); _logger.LogInformation($"Pattern with id = {id} mapped successfully"); _logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}"); var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId); if (selectionLevel == null) { _logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found"); throw new EntityNotFoundException("Selection level not found"); } _logger.LogInformation($"Add founded SelectionLevel to pattern"); pattern.SelectionLevel = selectionLevel; _context.Entry(pattern).State = EntityState.Modified; var result = _context.SaveChangesAsync(); _logger.LogInformation($"Pattern saved to DB"); await result; } public async Task DeleteAsync(int id) { _logger.LogInformation($"Start searching Pattern with id = {id}"); var pattern = await _context.Patterns.FindAsync(id); if (pattern is null) { _logger.LogError($"Pattern with id = {id} not found"); throw new EntityNotFoundException("Pattern not found"); } _context.Patterns.Remove(pattern); var result = _context.SaveChangesAsync(); _logger.LogInformation($"Ad saved to DB"); await result; } } }