Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PatternService.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 CreateAsync(PatternCreateDto patternCreateDto)
  48. {
  49. _logger.LogInformation($"Start creating Pattern");
  50. _logger.LogInformation($"Check is Pattern in database");
  51. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync();
  52. if (patternExists is not null)
  53. {
  54. _logger.LogError($"Pattern already exists in database");
  55. throw new EntityNotFoundException("Pattern already exists in database");
  56. }
  57. _logger.LogError($"Pattern are not in database");
  58. _logger.LogInformation($"Mapping PatternCreateDto to model");
  59. var pattern = _mapper.Map<Pattern>(patternCreateDto);
  60. _logger.LogInformation($"Pattern mapped successfully");
  61. _logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}");
  62. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);
  63. if(selectionLevel == null)
  64. {
  65. _logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found");
  66. throw new EntityNotFoundException("Selection level not found");
  67. }
  68. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  69. pattern.SelectionLevel = selectionLevel;
  70. await _context.AddAsync(pattern);
  71. _logger.LogInformation($"Saving Ad to db...");
  72. var result = _context.SaveChangesAsync();
  73. _logger.LogInformation($"Saved Ad to db...");
  74. await result;
  75. }
  76. public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
  77. {
  78. _logger.LogInformation($"Start updating Pattern");
  79. _logger.LogInformation($"Check is Pattern in database");
  80. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync();
  81. if (patternExists is not null)
  82. {
  83. _logger.LogError($"Pattern already exists in database");
  84. throw new EntityNotFoundException("Pattern already exists in database");
  85. }
  86. _logger.LogInformation($"Start searching Pattern with id = {id}");
  87. var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();
  88. if (pattern is null)
  89. {
  90. _logger.LogError($"Pattern with id = {id} not found");
  91. throw new EntityNotFoundException("Pattern not found");
  92. }
  93. _logger.LogInformation($"Mapping Pattern with id = {id}");
  94. _mapper.Map(patternUpdateDto, pattern);
  95. _logger.LogInformation($"Pattern with id = {id} mapped successfully");
  96. _logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}");
  97. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);
  98. if (selectionLevel == null)
  99. {
  100. _logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found");
  101. throw new EntityNotFoundException("Selection level not found");
  102. }
  103. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  104. pattern.SelectionLevel = selectionLevel;
  105. _context.Entry(pattern).State = EntityState.Modified;
  106. var result = _context.SaveChangesAsync();
  107. _logger.LogInformation($"Pattern saved to DB");
  108. await result;
  109. }
  110. public async Task DeleteAsync(int id)
  111. {
  112. _logger.LogInformation($"Start searching Pattern with id = {id}");
  113. var pattern = await _context.Patterns.FindAsync(id);
  114. if (pattern is null)
  115. {
  116. _logger.LogError($"Pattern with id = {id} not found");
  117. throw new EntityNotFoundException("Pattern not found");
  118. }
  119. _context.Patterns.Remove(pattern);
  120. var result = _context.SaveChangesAsync();
  121. _logger.LogInformation($"Ad saved to DB");
  122. await result;
  123. }
  124. }
  125. }