Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AdService.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. _logger.LogInformation("Getting data from DB");
  81. var filteredAds = await _context.Ads.Include(x => x.Technologies).ToListAsync();
  82. _logger.LogInformation($"Received {filteredAds.Count} ads from db.");
  83. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  84. List<AdResponseDto> result = _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
  85. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  86. return result;
  87. }
  88. public async Task CreateAsync(AdCreateDto adCreateDto)
  89. {
  90. _logger.LogInformation($"Start creating Ad");
  91. var ad = _mapper.Map<Ad>(adCreateDto);
  92. _logger.LogInformation($"Ad created successfully");
  93. _logger.LogInformation($"Start adding technologies to Ad");
  94. for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
  95. {
  96. var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
  97. ad.Technologies.Add(technology);
  98. _logger.LogInformation($"Technology with id {technology.TechnologyId} added to Ad");
  99. }
  100. _logger.LogInformation($"Finished adding techonologies");
  101. await _context.Ads.AddAsync(ad);
  102. _logger.LogInformation($"Saving Ad to db...");
  103. var result = _context.SaveChangesAsync();
  104. _logger.LogInformation($"Ad saved to DB");
  105. await result;
  106. }
  107. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  108. {
  109. _logger.LogInformation($"Start searching Ad with id = {id}");
  110. var ad = await _context.Ads.FindAsync(id);
  111. if (ad is null)
  112. {
  113. _logger.LogError($"Ad with id = {id} not found");
  114. throw new EntityNotFoundException("Ad not found");
  115. }
  116. _logger.LogInformation($"Mapping Ad with id = {id}");
  117. _mapper.Map(adUpdateDto, ad);
  118. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  119. _context.Entry(ad).State = EntityState.Modified;
  120. var result = _context.SaveChangesAsync();
  121. _logger.LogInformation($"Ad saved to DB");
  122. await result;
  123. }
  124. public async Task ArchiveAdAsync(int id)
  125. {
  126. _logger.LogInformation($"Start searching Ad with id = {id}");
  127. var ad = await _context.Ads.FindAsync(id);
  128. if (ad is null)
  129. {
  130. _logger.LogError($"Ad with id = {id} not found");
  131. throw new EntityNotFoundException("Ad not found");
  132. }
  133. _logger.LogInformation($"Change ad expired time");
  134. ad.ExpiredAt = DateTime.Now;
  135. _logger.LogInformation($"Ad expired time changed successfully");
  136. _context.Entry(ad).State = EntityState.Modified;
  137. var result = _context.SaveChangesAsync();
  138. _logger.LogInformation($"Ad saved to DB");
  139. await result;
  140. }
  141. public async Task DeleteAsync(int id)
  142. {
  143. _logger.LogInformation($"Start searching Ad with id = {id}");
  144. var ad = await _context.Ads.FindAsync(id);
  145. if (ad is null)
  146. {
  147. _logger.LogError($"Ad with id = {id} not found");
  148. throw new EntityNotFoundException("Ad not found");
  149. }
  150. _context.Ads.Remove(ad);
  151. var result = _context.SaveChangesAsync();
  152. _logger.LogInformation($"Ad saved to DB");
  153. await result;
  154. }
  155. public async Task<Ad> ImportAsync(AdCreateDto adCreateDto)
  156. {
  157. _logger.LogInformation($"Start importing Ad");
  158. var ad = _mapper.Map<Ad>(adCreateDto);
  159. _logger.LogInformation($"Ad imported successfully");
  160. await _context.Ads.AddAsync(ad);
  161. _logger.LogInformation($"Saving Ad to db...");
  162. await _context.SaveChangesAsync();
  163. _logger.LogInformation($"Ad saved to DB");
  164. return ad;
  165. }
  166. public async Task<Ad> GetByIdEntityAsync(int id)
  167. {
  168. _logger.LogInformation($"Start searching Ad with id = {id}");
  169. var ad = await _context.Ads.FindAsync(id);
  170. if (ad is null)
  171. {
  172. _logger.LogError($"Ad with id = {id} not found");
  173. throw new EntityNotFoundException("Ad not found");
  174. }
  175. return ad;
  176. }
  177. }
  178. }