Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AdService.cs 6.4KB

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