using AutoMapper; using Contracts; using Domain.Entities; using Domain.Repository; using Services.Abstractions; namespace Services { public class RegionService : IRegionService { private readonly IRegionRepository _regionRepository; private readonly IMapper _mapper; public RegionService(IRegionRepository regionRepository, IMapper mapper) { _regionRepository = regionRepository; _mapper = mapper; } public async Task CreateAsync(RegionDtoForCreate regionDto) { var region = _mapper.Map(regionDto); await _regionRepository.AddAsync(region); return _mapper.Map(region); } public async Task DeleteAsync(Guid regionId) { var region = await _regionRepository.GetByIdAsync(regionId); if (region == null) { throw new KeyNotFoundException("Region not found"); } await _regionRepository.DeleteAsync(region); } public async Task> GetAllAsync() { var regions = await _regionRepository.GetAllAsync(); return _mapper.Map>(regions); } public async Task GetByIdAsync(Guid id) { var region = await _regionRepository.GetByIdAsync(id); if (region == null) { throw new KeyNotFoundException("Region not found."); } return _mapper.Map(region); } public async Task UpdateAsync(Guid regionId, RegionDtoForUpdate regionDto) { var region = await _regionRepository.GetByIdAsync(regionId); if (region == null) { throw new KeyNotFoundException("Region not found."); } region.Name = regionDto.Name; region.Code = regionDto.Code; await _regionRepository.UpdateAsync(region); } } }