| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
-
- 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();
- }
- }
- }
|