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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Diligent.WebAPI.Business.Extensions;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class AdService : IAdService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly ITechnologyService _technologyService;
  9. public AdService(DatabaseContext context, IMapper mapper, ITechnologyService technologyService)
  10. {
  11. _context = context;
  12. _mapper = mapper;
  13. _technologyService = technologyService;
  14. }
  15. public async Task<List<AdResponseDto>> GetAllAsync()
  16. {
  17. var today = DateTime.Now;
  18. return _mapper.Map<List<AdResponseDto>>(await _context.Ads.Include(x => x.Technologies).Where(x => x.ExpiredAt > today).ToListAsync());
  19. }
  20. public async Task<AdResponseDto> GetByIdAsync(int id)
  21. {
  22. var ad = await _context.Ads.FindAsync(id);
  23. if(ad is null)
  24. throw new EntityNotFoundException("Ad not found");
  25. return _mapper.Map<AdResponseDto>(ad);
  26. }
  27. public async Task<AdDetailsResponseDto> GetAdDetailsByIdAsync(int id)
  28. {
  29. var ad = await _context.Ads.Include(x => x.Applicants).Where(x => x.Id == id).FirstOrDefaultAsync();
  30. if (ad is null)
  31. throw new EntityNotFoundException("Ad not found");
  32. return _mapper.Map<AdDetailsResponseDto>(ad);
  33. }
  34. public async Task<List<AdResponseDto>> GetArchiveAds()
  35. {
  36. var today = DateTime.Now;
  37. var archiveAds = await _context.Ads.Where(x => x.ExpiredAt < today).ToListAsync();
  38. return _mapper.Map<List<AdResponseDto>>(archiveAds);
  39. }
  40. public async Task<List<AdResponseDto>> GetFilteredAdsAsync(AdFilterDto filters)
  41. {
  42. var filteredAds = await _context.Ads.Include(x => x.Technologies).ToListAsync();
  43. return _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
  44. }
  45. public async Task CreateAsync(AdCreateDto adCreateDto)
  46. {
  47. var ad = _mapper.Map<Ad>(adCreateDto);
  48. for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
  49. {
  50. var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
  51. ad.Technologies.Add(technology);
  52. }
  53. await _context.Ads.AddAsync(ad);
  54. await _context.SaveChangesAsync();
  55. }
  56. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  57. {
  58. var ad = await _context.Ads.FindAsync(id);
  59. if (ad is null)
  60. throw new EntityNotFoundException("Ad not found");
  61. _mapper.Map(adUpdateDto, ad);
  62. _context.Entry(ad).State = EntityState.Modified;
  63. await _context.SaveChangesAsync();
  64. }
  65. public async Task DeleteAsync(int id)
  66. {
  67. var ad = await _context.Ads.FindAsync(id);
  68. if (ad is null)
  69. throw new EntityNotFoundException("Ad not found");
  70. _context.Ads.Remove(ad);
  71. await _context.SaveChangesAsync();
  72. }
  73. }
  74. }