71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
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<RegionDto> CreateAsync(RegionDtoForCreate regionDto)
|
|
{
|
|
var region = _mapper.Map<Region>(regionDto);
|
|
await _regionRepository.AddAsync(region);
|
|
|
|
return _mapper.Map<RegionDto>(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<List<RegionDto>> GetAllAsync()
|
|
{
|
|
var regions = await _regionRepository.GetAllAsync();
|
|
|
|
return _mapper.Map<List<RegionDto>>(regions);
|
|
}
|
|
|
|
public async Task<RegionDto> GetByIdAsync(Guid id)
|
|
{
|
|
var region = await _regionRepository.GetByIdAsync(id);
|
|
if (region == null)
|
|
{
|
|
throw new KeyNotFoundException("Region not found.");
|
|
}
|
|
return _mapper.Map<RegionDto>(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);
|
|
}
|
|
}
|
|
}
|