using AutoMapper; using Contracts; using Domain.Entities; using Domain.Repository; using Services.Abstractions; namespace Services { public class CityService : ICityService { private readonly ICityRepository _cityRepository; private readonly IMapper _mapper; public CityService(ICityRepository cityRepository, IMapper mapper) { _cityRepository = cityRepository; _mapper = mapper; } public async Task CreateAsync(CityDtoForCreate cityDto) { var city = _mapper.Map(cityDto); await _cityRepository.AddAsync(city); return _mapper.Map(city); } public async Task DeleteAsync(Guid cityId) { var city = await _cityRepository.GetByIdAsync(cityId); if (city == null) { throw new KeyNotFoundException("City not found"); } await _cityRepository.DeleteAsync(city); } public async Task> GetAllAsync() { var cities = await _cityRepository.GetAllAsync(); return _mapper.Map>(cities); } public async Task GetByIdAsync(Guid id) { var city = await _cityRepository.GetByIdAsync(id); if (city == null) { throw new KeyNotFoundException("City not found."); } return _mapper.Map(city); } public async Task UpdateAsync(Guid cityId, CityDtoForUpdate cityDto) { var city = await _cityRepository.GetByIdAsync(cityId); if (city == null) { throw new KeyNotFoundException("City not found"); } city.Name = cityDto.Name; city.RegionId = cityDto.RegionId; await _cityRepository.UpdateAsync(city); } } }