You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PatternService.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Diligent.WebAPI.Contracts.DTOs.Pattern;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Diligent.WebAPI.Business.Services
  8. {
  9. public class PatternService : IPatternService
  10. {
  11. private readonly DatabaseContext _context;
  12. private readonly IMapper _mapper;
  13. private readonly ISelectionLevelService _selectionLevelService;
  14. private readonly ILogger<PatternService> _logger;
  15. public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger<PatternService> logger)
  16. {
  17. _context = context;
  18. _mapper = mapper;
  19. _selectionLevelService = selectionLevelService;
  20. _logger = logger;
  21. }
  22. public async Task<List<PatternResponseDto>> GetAllAsync()
  23. {
  24. _logger.LogInformation("Start getting all Patterns");
  25. _logger.LogInformation("Getting data from DB");
  26. var fromDb = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync();
  27. _logger.LogInformation($"Received {fromDb.Count} patterns from db.");
  28. _logger.LogInformation($"Mapping received patterns to PatternResponseDto");
  29. var result = _mapper.Map<List<PatternResponseDto>>(fromDb);
  30. _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
  31. return result;
  32. }
  33. public async Task<PatternResponseDto> GetByIdAsync(int id)
  34. {
  35. _logger.LogInformation($"Start searching Pattern with id = {id}");
  36. var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();
  37. if (pattern is null)
  38. {
  39. _logger.LogError($"Pattern with id = {id} not found");
  40. throw new EntityNotFoundException("Pattern not found");
  41. }
  42. _logger.LogInformation($"Mapping Pattern with id = {id}");
  43. PatternResponseDto result = _mapper.Map<PatternResponseDto>(pattern);
  44. _logger.LogInformation($"Pattern with id = {id} mapped successfully");
  45. return result;
  46. }
  47. public async Task<List<PatternResponseDto>> GetFilteredPatternsAsync(FilterPatternDto filterPatternDto)
  48. {
  49. _logger.LogInformation($"Start getting all filtered Patterns");
  50. _logger.LogInformation("Getting data from DB");
  51. var filteredPatterns = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync();
  52. _logger.LogInformation($"Received {filteredPatterns.Count} patterns from db.");
  53. _logger.LogInformation($"Mapping received patterns to PatternResponseDto");
  54. List<PatternResponseDto> result = _mapper.Map<List<PatternResponseDto>>(filteredPatterns.FilterApplicants(filterPatternDto));
  55. _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
  56. return result;
  57. }
  58. public async Task CreateAsync(PatternCreateDto patternCreateDto)
  59. {
  60. _logger.LogInformation($"Start creating Pattern");
  61. _logger.LogInformation($"Check is Pattern in database");
  62. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync();
  63. if (patternExists is not null)
  64. {
  65. _logger.LogError($"Pattern already exists in database");
  66. throw new EntityNotFoundException("Pattern already exists in database");
  67. }
  68. _logger.LogInformation($"Pattern is not in database");
  69. _logger.LogInformation($"Mapping PatternCreateDto to model");
  70. var pattern = _mapper.Map<Pattern>(patternCreateDto);
  71. _logger.LogInformation($"Pattern mapped successfully");
  72. _logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}");
  73. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);
  74. if(selectionLevel == null)
  75. {
  76. _logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found");
  77. throw new EntityNotFoundException("Selection level not found");
  78. }
  79. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  80. pattern.SelectionLevel = selectionLevel;
  81. await _context.AddAsync(pattern);
  82. _logger.LogInformation($"Saving Ad to db...");
  83. var result = _context.SaveChangesAsync();
  84. _logger.LogInformation($"Saved Ad to db...");
  85. await result;
  86. }
  87. public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
  88. {
  89. _logger.LogInformation($"Start updating Pattern");
  90. _logger.LogInformation($"Check is Pattern in database");
  91. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync();
  92. if (patternExists is not null)
  93. {
  94. _logger.LogError($"Pattern already exists in database");
  95. throw new EntityNotFoundException("Pattern already exists in database");
  96. }
  97. _logger.LogInformation($"Start searching Pattern with id = {id}");
  98. var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();
  99. if (pattern is null)
  100. {
  101. _logger.LogError($"Pattern with id = {id} not found");
  102. throw new EntityNotFoundException("Pattern not found");
  103. }
  104. _logger.LogInformation($"Mapping Pattern with id = {id}");
  105. _mapper.Map(patternUpdateDto, pattern);
  106. _logger.LogInformation($"Pattern with id = {id} mapped successfully");
  107. _logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}");
  108. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);
  109. if (selectionLevel == null)
  110. {
  111. _logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found");
  112. throw new EntityNotFoundException("Selection level not found");
  113. }
  114. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  115. pattern.SelectionLevel = selectionLevel;
  116. _context.Entry(pattern).State = EntityState.Modified;
  117. var result = _context.SaveChangesAsync();
  118. _logger.LogInformation($"Pattern saved to DB");
  119. await result;
  120. }
  121. public async Task DeleteAsync(int id)
  122. {
  123. _logger.LogInformation($"Start searching Pattern with id = {id}");
  124. var pattern = await _context.Patterns.FindAsync(id);
  125. if (pattern is null)
  126. {
  127. _logger.LogError($"Pattern with id = {id} not found");
  128. throw new EntityNotFoundException("Pattern not found");
  129. }
  130. _context.Patterns.Remove(pattern);
  131. var result = _context.SaveChangesAsync();
  132. _logger.LogInformation($"Ad saved to DB");
  133. await result;
  134. }
  135. }
  136. }