namespace Diligent.WebAPI.Business.Services { public class AdService : IAdService { private readonly DatabaseContext _context; private readonly IMapper _mapper; public AdService(DatabaseContext context, IMapper mapper) { _context = context; _mapper = mapper; } public async Task> GetAllAsync() => _mapper.Map>(await _context.Ads.ToListAsync()); public async Task GetByIdAsync(int id) { var ad = await _context.Ads.FindAsync(id); if(ad is null) throw new EntityNotFoundException("Ad not found"); return _mapper.Map(ad); } public async Task CreateAsync(AdCreateDto adCreateDto) { await _context.Ads.AddAsync(_mapper.Map(adCreateDto)); 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(); } } }