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.

AdService.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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)
  43. .FilterByExperience(filters.MinExperience, filters.MaxExperience)
  44. .FilterByWorkType(filters.WorkHour)
  45. .FilterByEmploymentType(filters.EmploymentType)
  46. .ToListAsync();
  47. filteredAds = filteredAds.FilterByTechnologies(filters.Technologies);
  48. return _mapper.Map<List<AdResponseDto>>(filteredAds);
  49. }
  50. public async Task CreateAsync(AdCreateDto adCreateDto)
  51. {
  52. var ad = _mapper.Map<Ad>(adCreateDto);
  53. for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
  54. {
  55. var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
  56. ad.Technologies.Add(technology);
  57. }
  58. await _context.Ads.AddAsync(ad);
  59. await _context.SaveChangesAsync();
  60. }
  61. public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
  62. {
  63. var ad = await _context.Ads.FindAsync(id);
  64. if (ad is null)
  65. throw new EntityNotFoundException("Ad not found");
  66. _mapper.Map(adUpdateDto, ad);
  67. _context.Entry(ad).State = EntityState.Modified;
  68. await _context.SaveChangesAsync();
  69. }
  70. public async Task DeleteAsync(int id)
  71. {
  72. var ad = await _context.Ads.FindAsync(id);
  73. if (ad is null)
  74. throw new EntityNotFoundException("Ad not found");
  75. _context.Ads.Remove(ad);
  76. await _context.SaveChangesAsync();
  77. }
  78. }
  79. }