71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
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<CityDto> CreateAsync(CityDtoForCreate cityDto)
|
|
{
|
|
var city = _mapper.Map<City>(cityDto);
|
|
await _cityRepository.AddAsync(city);
|
|
|
|
return _mapper.Map<CityDto>(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<List<CityDto>> GetAllAsync()
|
|
{
|
|
var cities = await _cityRepository.GetAllAsync();
|
|
|
|
return _mapper.Map<List<CityDto>>(cities);
|
|
}
|
|
|
|
public async Task<CityDto> GetByIdAsync(Guid id)
|
|
{
|
|
var city = await _cityRepository.GetByIdAsync(id);
|
|
if (city == null)
|
|
{
|
|
throw new KeyNotFoundException("City not found.");
|
|
}
|
|
return _mapper.Map<CityDto>(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);
|
|
}
|
|
}
|
|
}
|