| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using Diligent.WebAPI.Business.Extensions;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class AdService : IAdService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ITechnologyService _technologyService;
-
- public AdService(DatabaseContext context, IMapper mapper, ITechnologyService technologyService)
- {
- _context = context;
- _mapper = mapper;
- _technologyService = technologyService;
- }
-
- public async Task<List<AdResponseDto>> GetAllAsync()
- {
- var today = DateTime.Now;
- return _mapper.Map<List<AdResponseDto>>(await _context.Ads.Include(x => x.Technologies).Where(x => x.ExpiredAt > today).ToListAsync());
- }
-
- public async Task<AdResponseDto> GetByIdAsync(int id)
- {
- var ad = await _context.Ads.FindAsync(id);
-
- if(ad is null)
- throw new EntityNotFoundException("Ad not found");
-
- return _mapper.Map<AdResponseDto>(ad);
-
- }
-
- public async Task<AdDetailsResponseDto> GetAdDetailsByIdAsync(int id)
- {
- var ad = await _context.Ads.Include(x => x.Applicants).Where(x => x.Id == id).FirstOrDefaultAsync();
-
- if (ad is null)
- throw new EntityNotFoundException("Ad not found");
-
- return _mapper.Map<AdDetailsResponseDto>(ad);
- }
-
- public async Task<List<AdResponseDto>> GetArchiveAds()
- {
- var today = DateTime.Now;
- var archiveAds = await _context.Ads.Where(x => x.ExpiredAt < today).ToListAsync();
-
- return _mapper.Map<List<AdResponseDto>>(archiveAds);
- }
-
- public async Task<List<AdResponseDto>> GetFilteredAdsAsync(AdFilterDto filters)
- {
- var filteredAds = await _context.Ads.Include(x => x.Technologies).ToListAsync();
-
- return _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
- }
-
- public async Task CreateAsync(AdCreateDto adCreateDto)
- {
- var ad = _mapper.Map<Ad>(adCreateDto);
-
- for (int i = 0; i < adCreateDto.TechnologiesIds.Count; i++)
- {
- var technology = await _technologyService.GetEntityByIdAsync(adCreateDto.TechnologiesIds[i]);
- ad.Technologies.Add(technology);
- }
-
- await _context.Ads.AddAsync(ad);
-
- await _context.SaveChangesAsync();
- }
-
- public async Task UpdateAsync(int id, AdUpdateDto adUpdateDto)
- {
- var ad = await _context.Ads.FindAsync(id);
-
- if (ad is null)
- throw new EntityNotFoundException("Ad not found");
-
- _mapper.Map(adUpdateDto, ad);
-
- _context.Entry(ad).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- }
-
- public async Task DeleteAsync(int id)
- {
- var ad = await _context.Ads.FindAsync(id);
-
- if (ad is null)
- throw new EntityNotFoundException("Ad not found");
-
- _context.Ads.Remove(ad);
- await _context.SaveChangesAsync();
- }
- }
- }
|