| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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<List<AdResponseDto>> GetAllAsync() =>
- _mapper.Map<List<AdResponseDto>>(await _context.Ads.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 CreateAsync(AdCreateDto adCreateDto)
- {
- await _context.Ads.AddAsync(_mapper.Map<Ad>(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();
- }
- }
- }
|