71 lines
1.9 KiB
C#
Raw Normal View History

2024-12-09 03:27:18 +04:00
using AutoMapper;
using Contracts;
using Domain.Entities;
2024-12-08 22:57:55 +04:00
using Domain.Repository;
2024-12-09 03:27:18 +04:00
using Services.Abstractions;
2024-12-08 22:57:55 +04:00
namespace Services
{
2024-12-09 03:27:18 +04:00
public class CityService : ICityService
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
private readonly ICityRepository _cityRepository;
private readonly IMapper _mapper;
public CityService(ICityRepository cityRepository, IMapper mapper)
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
_cityRepository = cityRepository;
_mapper = mapper;
2024-12-08 22:57:55 +04:00
}
2024-12-09 03:27:18 +04:00
public async Task<CityDto> CreateAsync(CityDtoForCreate cityDto)
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
var city = _mapper.Map<City>(cityDto);
await _cityRepository.AddAsync(city);
return _mapper.Map<CityDto>(city);
2024-12-08 22:57:55 +04:00
}
2024-12-09 03:27:18 +04:00
public async Task DeleteAsync(Guid cityId)
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
var city = await _cityRepository.GetByIdAsync(cityId);
if (city == null)
{
throw new KeyNotFoundException("City not found");
}
await _cityRepository.DeleteAsync(city);
2024-12-08 22:57:55 +04:00
}
2024-12-09 03:27:18 +04:00
public async Task<List<CityDto>> GetAllAsync()
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
var cities = await _cityRepository.GetAllAsync();
return _mapper.Map<List<CityDto>>(cities);
2024-12-08 22:57:55 +04:00
}
2024-12-09 03:27:18 +04:00
public async Task<CityDto> GetByIdAsync(Guid id)
2024-12-08 22:57:55 +04:00
{
2024-12-09 03:27:18 +04:00
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);
2024-12-08 22:57:55 +04:00
}
}
}