Files
prueba_tecnica_proxima/backend/ProximaContracts/ProximaContracts.Application/Contracts/Services/ContractService.cs
2025-06-15 18:29:25 +02:00

57 lines
2.3 KiB
C#

using AutoMapper;
using ProximaContracts.Application.Rates.Services;
using ProximaContracts.Domain.Contracts.DTOs.Request;
using ProximaContracts.Domain.Contracts.DTOs.Response;
using ProximaContracts.Infrastructure.Rpositories.Contracts;
using ProximaContracts.Shared.Exceptions.Repositories.Contract;
namespace ProximaContracts.Application.Contracts.Services
{
public class ContractService(IContractRepository repository, IMapper mapper, IRateService rateService) : IContractService
{
private readonly IContractRepository _repository = repository;
private readonly IMapper _mapper = mapper;
private readonly IRateService _rateService = rateService;
public async Task<ContractByIdResponseDto> GetContractById(ContractByIdRequestDto dto)
{
var result = await _repository.GetContractById(dto);
return _mapper.Map< ContractByIdResponseDto>(result);
}
public async Task<IEnumerable<GetContractsResponseDto>> GetContracts()
{
var result = await _repository.GetContracts();
return _mapper.Map<List<GetContractsResponseDto>>(result);
}
public async Task<CreateContractResponseDto> CreateContract(CreateContractRequestDto dto)
{
var rateExists = await _rateService.CheckIfExists(dto.RateId);
if (!rateExists)
{
throw new CreateContractRateNotFoundException($"No rate found with id: {dto.RateId}, Contract can't be created.");
}
var result = await _repository.CreateContract(dto);
return new CreateContractResponseDto()
{
IsCreated = result.HasValue ? true : false,
Message = result.HasValue ? "Created" : "Error creating contract",
NewContractId = result.HasValue ? result.Value : 0
};
}
public async Task<UpdateContractResponseDto> UpdateContract(UpdateContractRequestDto dto)
{
var result = await _repository.UpdateContract(dto);
return new UpdateContractResponseDto()
{
IsUpdated = result.HasValue ? true : false,
Message = result.HasValue ? "Updated" : "Error updating contract",
ContractId = result.HasValue ? result.Value : 0
};
}
}
}