You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. namespace Diligent.WebAPI.Business.Services
  2. {
  3. public class AdService : IAdService
  4. {
  5. private readonly DatabaseContext _context;
  6. private readonly IMapper _mapper;
  7. public AdService(DatabaseContext context, IMapper mapper)
  8. {
  9. _context = context;
  10. _mapper = mapper;
  11. }
  12. public async Task<List<AdResponseDto>> GetAllAsync() =>
  13. _mapper.Map<List<AdResponseDto>>(await _context.Ads.Include(x => x.Technologies).ToListAsync());
  14. public async Task<AdResponseDto> GetByIdAsync(int id)
  15. {
  16. var ad = await _context.Ads.FindAsync(id);
  17. if(ad is null)
  18. throw new EntityNotFoundException("Ad not found");
  19. return _mapper.Map<AdResponseDto>(ad);
  20. }
  21. public async Task<AdDetailsResponseDto> GetAdDetailsByIdAsync(int id)
  22. {
  23. var ad = await _context.Ads.Include(x => x.Applicants).Where(x => x.Id == id).FirstOrDefaultAsync();
  24. if (ad is null)
  25. throw new EntityNotFoundException("Ad not found");
  26. return _mapper.Map<AdDetailsResponseDto>(ad);
  27. }
  28. public async Task<List<AdResponseDto>> GetArchiveAds()
  29. {
  30. var today = DateTime.Now;
  31. var archiveAds = await _context.Ads.Where(x => x.ExpiredAt < today).ToListAsync();
  32. return _mapper.Map<List<AdResponseDto>>(archiveAds);
  33. }
  34. public async Task CreateAsync(AdCreateDto adCreateDto)
  35. {
  36. await _context.Ads.AddAsync(_mapper.Map<Ad>(adCreateDto));
  37. await _context.SaveChangesAsync();
  38. }
  39. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  40. {
  41. var ad = await _context.Ads.FindAsync(id);
  42. if (ad is null)
  43. throw new EntityNotFoundException("Ad not found");
  44. _mapper.Map(adUpdateDto, ad);
  45. _context.Entry(ad).State = EntityState.Modified;
  46. await _context.SaveChangesAsync();
  47. }
  48. public async Task DeleteAsync(int id)
  49. {
  50. var ad = await _context.Ads.FindAsync(id);
  51. if (ad is null)
  52. throw new EntityNotFoundException("Ad not found");
  53. _context.Ads.Remove(ad);
  54. await _context.SaveChangesAsync();
  55. }
  56. }
  57. }