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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. namespace Diligent.WebAPI.Business.Services
  2. {
  3. public class AdService : IAdService
  4. {
  5. private readonly ILogger<AdService> _logger;
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly ITechnologyService _technologyService;
  9. public AdService(DatabaseContext context, IMapper mapper, ITechnologyService technologyService, ILogger<AdService> logger)
  10. {
  11. _logger = logger;
  12. _context = context;
  13. _mapper = mapper;
  14. _technologyService = technologyService;
  15. }
  16. public async Task<List<AdResponseDto>> GetAllAsync()
  17. {
  18. _logger.LogInformation("Start getting all Ads");
  19. var today = DateTime.Now;
  20. _logger.LogInformation("Getting data from DB");
  21. var fromDb = await _context.Ads.Include(x => x.Technologies).Where(x => x.ExpiredAt > today).ToListAsync();
  22. _logger.LogInformation($"Received {fromDb.Count} ads from db.");
  23. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  24. var result = _mapper.Map<List<AdResponseDto>>(fromDb);
  25. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  26. return result;
  27. }
  28. public async Task<List<AdResponseWithCountDto>> GetAllWithCountAsync()
  29. {
  30. _logger.LogInformation("Start getting all Ads with applicants count");
  31. var today = DateTime.Now;
  32. _logger.LogInformation("Getting data from database");
  33. var res = _mapper.Map<List<AdResponseWithCountDto>>(await _context.Ads.Include(x => x.Applicants).Where(x => x.ExpiredAt > today).ToListAsync());
  34. _logger.LogInformation($"Received {res.Count} ads with their counts");
  35. return res;
  36. }
  37. public async Task<AdResponseDto> GetByIdAsync(int id)
  38. {
  39. _logger.LogInformation($"Start searching Ad with id = {id}");
  40. var ad = await _context.Ads.FindAsync(id);
  41. if (ad is null)
  42. {
  43. _logger.LogError($"Ad with id = {id} not found");
  44. throw new EntityNotFoundException("Ad not found");
  45. }
  46. _logger.LogInformation($"Mapping Ad with id = {id}");
  47. AdResponseDto result = _mapper.Map<AdResponseDto>(ad);
  48. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  49. return result;
  50. }
  51. public async Task<AdDetailsResponseDto> GetAdDetailsByIdAsync(int id)
  52. {
  53. _logger.LogInformation($"Start finding Ad with id = {id} with applicants");
  54. var ad = await _context.Ads.Include(x => x.Applicants).Where(x => x.Id == id).FirstOrDefaultAsync();
  55. if (ad is null)
  56. {
  57. _logger.LogError($"Ad with id = {id} not found");
  58. throw new EntityNotFoundException("Ad not found");
  59. }
  60. _logger.LogInformation($"Mapping Ad with id = {id}");
  61. AdDetailsResponseDto result = _mapper.Map<AdDetailsResponseDto>(ad);
  62. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  63. return result;
  64. }
  65. public async Task<List<AdResponseDto>> GetArchiveAds()
  66. {
  67. _logger.LogInformation("Start getting all Archived Ads");
  68. var today = DateTime.Now;
  69. _logger.LogInformation("Getting data from DB");
  70. var archiveAds = await _context.Ads.Where(x => x.ExpiredAt < today).ToListAsync();
  71. _logger.LogInformation($"Received {archiveAds.Count} ads from db.");
  72. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  73. List<AdResponseDto> result = _mapper.Map<List<AdResponseDto>>(archiveAds);
  74. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  75. return result;
  76. }
  77. public async Task<List<AdResponseDto>> GetFilteredAdsAsync(AdFilterDto filters)
  78. {
  79. _logger.LogInformation($"Start getting all filtered Ads");
  80. var today = DateTime.Now;
  81. _logger.LogInformation("Getting data from DB");
  82. var filteredAds = await _context.Ads.Include(x => x.Technologies).Where(x => x.ExpiredAt > today).ToListAsync();
  83. _logger.LogInformation($"Received {filteredAds.Count} ads from db.");
  84. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  85. List<AdResponseDto> result = _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
  86. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  87. return result;
  88. }
  89. public async Task CreateAsync(AdCreateDto adCreateDto)
  90. {
  91. _logger.LogInformation($"Start creating Ad");
  92. var ad = _mapper.Map<Ad>(adCreateDto);
  93. _logger.LogInformation($"Ad created successfully");
  94. _logger.LogInformation($"Start adding technologies to Ad");
  95. for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
  96. {
  97. var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
  98. ad.Technologies.Add(technology);
  99. _logger.LogInformation($"Technology with id {technology.TechnologyId} added to Ad");
  100. }
  101. _logger.LogInformation($"Finished adding techonologies");
  102. await _context.Ads.AddAsync(ad);
  103. _logger.LogInformation($"Saving Ad to db...");
  104. var result = _context.SaveChangesAsync();
  105. _logger.LogInformation($"Ad saved to DB");
  106. await result;
  107. }
  108. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  109. {
  110. _logger.LogInformation($"Start searching Ad with id = {id}");
  111. var ad = await _context.Ads.FindAsync(id);
  112. if (ad is null)
  113. {
  114. _logger.LogError($"Ad with id = {id} not found");
  115. throw new EntityNotFoundException("Ad not found");
  116. }
  117. _logger.LogInformation($"Mapping Ad with id = {id}");
  118. _mapper.Map(adUpdateDto, ad);
  119. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  120. _context.Entry(ad).State = EntityState.Modified;
  121. var result = _context.SaveChangesAsync();
  122. _logger.LogInformation($"Ad saved to DB");
  123. await result;
  124. }
  125. public async Task ArchiveAdAsync(int id)
  126. {
  127. _logger.LogInformation($"Start searching Ad with id = {id}");
  128. var ad = await _context.Ads.FindAsync(id);
  129. if (ad is null)
  130. {
  131. _logger.LogError($"Ad with id = {id} not found");
  132. throw new EntityNotFoundException("Ad not found");
  133. }
  134. _logger.LogInformation($"Change ad expired time");
  135. ad.ExpiredAt = DateTime.Now;
  136. _logger.LogInformation($"Ad expired time changed successfully");
  137. _context.Entry(ad).State = EntityState.Modified;
  138. var result = _context.SaveChangesAsync();
  139. _logger.LogInformation($"Ad saved to DB");
  140. await result;
  141. }
  142. public async Task DeleteAsync(int id)
  143. {
  144. _logger.LogInformation($"Start searching Ad with id = {id}");
  145. var ad = await _context.Ads.FindAsync(id);
  146. if (ad is null)
  147. {
  148. _logger.LogError($"Ad with id = {id} not found");
  149. throw new EntityNotFoundException("Ad not found");
  150. }
  151. _context.Ads.Remove(ad);
  152. var result = _context.SaveChangesAsync();
  153. _logger.LogInformation($"Ad saved to DB");
  154. await result;
  155. }
  156. public async Task<Ad> ImportAsync(AdCreateDto adCreateDto)
  157. {
  158. _logger.LogInformation($"Start importing Ad");
  159. var ad = _mapper.Map<Ad>(adCreateDto);
  160. _logger.LogInformation($"Ad imported successfully");
  161. await _context.Ads.AddAsync(ad);
  162. _logger.LogInformation($"Saving Ad to db...");
  163. await _context.SaveChangesAsync();
  164. _logger.LogInformation($"Ad saved to DB");
  165. return ad;
  166. }
  167. public async Task<Ad> GetByIdEntityAsync(int id)
  168. {
  169. _logger.LogInformation($"Start searching Ad with id = {id}");
  170. var ad = await _context.Ads.FindAsync(id);
  171. if (ad is null)
  172. {
  173. _logger.LogError($"Ad with id = {id} not found");
  174. throw new EntityNotFoundException("Ad not found");
  175. }
  176. return ad;
  177. }
  178. }
  179. }