| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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<PatternService> _logger;
-
- public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger<PatternService> logger)
- {
- _context = context;
- _mapper = mapper;
- _selectionLevelService = selectionLevelService;
- _logger = logger;
- }
-
- public async Task<List<PatternResponseDto>> 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<List<PatternResponseDto>>(fromDb);
- _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
- return result;
- }
-
- public async Task<PatternResponseDto> 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<PatternResponseDto>(pattern);
- _logger.LogInformation($"Pattern with id = {id} mapped successfully");
- 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.LogError($"Pattern are not in database");
- _logger.LogInformation($"Mapping PatternCreateDto to model");
- var pattern = _mapper.Map<Pattern>(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;
- }
- }
- }
|