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 GetContractById(ContractByIdRequestDto dto) { var result = await _repository.GetContractById(dto); return _mapper.Map< ContractByIdResponseDto>(result); } public async Task> GetContracts() { var result = await _repository.GetContracts(); return _mapper.Map>(result); } public async Task 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 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 }; } } }