Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AdService.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var today = DateTime.Now;
  31. return _mapper.Map<List<AdResponseWithCountDto>>(await _context.Ads.Include(x => x.Applicants).Where(x => x.ExpiredAt > today).ToListAsync());
  32. }
  33. public async Task<AdResponseDto> GetByIdAsync(int id)
  34. {
  35. _logger.LogInformation($"Start searching Ad with id = {id}");
  36. var ad = await _context.Ads.FindAsync(id);
  37. if (ad is null)
  38. {
  39. _logger.LogError($"Ad with id = {id} not found");
  40. throw new EntityNotFoundException("Ad not found");
  41. }
  42. _logger.LogInformation($"Mapping Ad with id = {id}");
  43. AdResponseDto result = _mapper.Map<AdResponseDto>(ad);
  44. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  45. return result;
  46. }
  47. public async Task<AdDetailsResponseDto> GetAdDetailsByIdAsync(int id)
  48. {
  49. _logger.LogInformation($"Start finding Ad with id = {id} with applicants");
  50. var ad = await _context.Ads.Include(x => x.Applicants).Where(x => x.Id == id).FirstOrDefaultAsync();
  51. if (ad is null)
  52. {
  53. _logger.LogError($"Ad with id = {id} not found");
  54. throw new EntityNotFoundException("Ad not found");
  55. }
  56. _logger.LogInformation($"Mapping Ad with id = {id}");
  57. AdDetailsResponseDto result = _mapper.Map<AdDetailsResponseDto>(ad);
  58. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  59. return result;
  60. }
  61. public async Task<List<AdResponseDto>> GetArchiveAds()
  62. {
  63. _logger.LogInformation("Start getting all Archived Ads");
  64. var today = DateTime.Now;
  65. _logger.LogInformation("Getting data from DB");
  66. var archiveAds = await _context.Ads.Where(x => x.ExpiredAt < today).ToListAsync();
  67. _logger.LogInformation($"Received {archiveAds.Count} ads from db.");
  68. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  69. List<AdResponseDto> result = _mapper.Map<List<AdResponseDto>>(archiveAds);
  70. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  71. return result;
  72. }
  73. public async Task<List<AdResponseDto>> GetFilteredAdsAsync(AdFilterDto filters)
  74. {
  75. _logger.LogInformation($"Start getting all filtered Ads");
  76. _logger.LogInformation("Getting data from DB");
  77. var filteredAds = await _context.Ads.Include(x => x.Technologies).ToListAsync();
  78. _logger.LogInformation($"Received {filteredAds.Count} ads from db.");
  79. _logger.LogInformation($"Mapping received ads to AdResponseDto");
  80. List<AdResponseDto> result = _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
  81. _logger.LogInformation($"Ads has been mapped and received to client: {result.Count} mapped ads");
  82. return result;
  83. }
  84. public async Task CreateAsync(AdCreateDto adCreateDto)
  85. {
  86. _logger.LogInformation($"Start creating Ad");
  87. var ad = _mapper.Map<Ad>(adCreateDto);
  88. _logger.LogInformation($"Ad created successfully");
  89. _logger.LogInformation($"Start adding technologies to Ad");
  90. for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
  91. {
  92. var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
  93. ad.Technologies.Add(technology);
  94. _logger.LogInformation($"Technology with id {technology.TechnologyId} added to Ad");
  95. }
  96. _logger.LogInformation($"Finished adding techonologies");
  97. await _context.Ads.AddAsync(ad);
  98. _logger.LogInformation($"Saving Ad to db...");
  99. var result = _context.SaveChangesAsync();
  100. _logger.LogInformation($"Ad saved to DB");
  101. await result;
  102. }
  103. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  104. {
  105. _logger.LogInformation($"Start searching Ad with id = {id}");
  106. var ad = await _context.Ads.FindAsync(id);
  107. if (ad is null)
  108. {
  109. _logger.LogError($"Ad with id = {id} not found");
  110. throw new EntityNotFoundException("Ad not found");
  111. }
  112. _logger.LogInformation($"Mapping Ad with id = {id}");
  113. _mapper.Map(adUpdateDto, ad);
  114. _logger.LogInformation($"Ad with id = {id} mapped successfully");
  115. _context.Entry(ad).State = EntityState.Modified;
  116. var result = _context.SaveChangesAsync();
  117. _logger.LogInformation($"Ad saved to DB");
  118. await result;
  119. }
  120. public async Task DeleteAsync(int id)
  121. {
  122. _logger.LogInformation($"Start searching Ad with id = {id}");
  123. var ad = await _context.Ads.FindAsync(id);
  124. if (ad is null)
  125. {
  126. _logger.LogError($"Ad with id = {id} not found");
  127. throw new EntityNotFoundException("Ad not found");
  128. }
  129. _context.Ads.Remove(ad);
  130. var result = _context.SaveChangesAsync();
  131. _logger.LogInformation($"Ad saved to DB");
  132. await result;
  133. }
  134. }
  135. }