public class BaseService
{
protected readonly ILog log;
public delegate ValueTask<T> Delegate<T>();
public BaseService(ILog log)
{
this.log = log;
}
public NullException CreateAndLogInvalideException(Xeption exception)
{
var nullException = new NullException();
log.Error(JsonConvert.SerializeObject(exception.Data), exception);
return nullException;
}
public ServiceException CreateAndLogServiceException(Xeption xeption)
{
var serviceException = new ServiceException(xeption);
log.Error(JsonConvert.SerializeObject(xeption.Data), xeption);
return serviceException;
}
public DependencyException CreateAndLogValidationDependencyException(Xeption exception)
{
var depenDencyException = new DependencyException(exception);
log.Error("Dependecy Error", exception.InnerException);
return depenDencyException;
}
public ValidationException CreateAndLogValidationException(Exception exception)
{
ValidationException validationException = new ValidationException(exception);
log.Error(exception.Message);
return validationException;
}
public ValidationException CreateAndLogValidationXeption(Xeption exception)
{
ValidationException validationException = new ValidationException(exception);
log.Error(JsonConvert.SerializeObject(exception.Data), exception);
return validationException;
}
public dynamic IsInvalidX(string text) => new
{
Condition = String.IsNullOrWhiteSpace(text),
Message = "Text is required"
};
public dynamic IsInvalidX(DateTime date) => new
{
Condition = date == null,
Message = "Date is required"
};
public dynamic IsInvalidX(int value) => new
{
Condition = value <= 0,
Message = "Value is required"
};
public dynamic IsInvalidX(decimal value) => new
{
Condition = value <= 0,
Message = "Value is required"
};
public dynamic IsInvalidX(long value) => new
{
Condition = value <= 0,
Message = "Value is required"
};
public dynamic IsNotNull(object myObject, string objectName) => new
{
Condition = myObject != null,
Message = $"Object : {objectName} already Exist"
};
public dynamic IsNullX(object myObject, string objectName) => new
{
Condition = myObject is null,
Message = $"Object : {objectName} can't be null"
};
public dynamic IsNullX(object myObject, string objectName, string parameterName, object id) => new
{
Condition = myObject is null,
Message = $"Couldn't find {objectName} with {parameterName}: {id}"
};
public async ValueTask<T> TryCatch<T>(Delegate<T> returningDelegate)
{
try
{
return await returningDelegate();
}
catch (InvalidException invalidException)
{
throw CreateAndLogValidationXeption(invalidException);
}
catch (NotFoundException notFoundException)
{
throw CreateAndLogValidationXeption(notFoundException);
}
catch (NullException nullContratCarteException)
{
throw CreateAndLogValidationXeption(nullContratCarteException);
}
catch (OracleException oracleException)
{
var failedStudentStorageException =
new FailedStorageException(oracleException);
throw CreateAndLogValidationDependencyException(failedStudentStorageException);
}
catch (Exception exception)
{
var failServiceException =
new FailedServiceException(exception);
throw CreateAndLogServiceException(failServiceException);
}
}
public void Validate<T>(params (dynamic Rule, string Parameter)[] validations) where T : Xeption, new()
{
var invalidException = new T();
foreach ((dynamic rule, string parameter) in validations)
{
if (rule.Condition)
{
invalidException.UpsertDataList(parameter, rule.Message);
}
}
invalidException.ThrowIfContainsErrors();
}
}
public sealed class SipsParameterService : BaseService, ISipsParameterService
{
private readonly IStorageInMemoryBroker storageInMemoryBroker;
public SipsParameterService(IStorageInMemoryBroker storageInMemoryBroker)
: base(LogManager.GetLogger(typeof(SipsParameterService)))
{
this.storageInMemoryBroker = storageInMemoryBroker;
}
public ValueTask<SipsParameter> GetSipsParametersAsync(string merchantId)
{
return TryCatch(async () =>
{
ValidateMerchantId(merchantId);
var parameter = await this.storageInMemoryBroker.GetSipsParametersAsync();
ValidateParameter(parameter, merchantId);
return parameter;
});
}
private void ValidateMerchantId(string merchantId)
{
Validate<InvalidException>
(
(Rule: IsInvalidX(merchantId), nameof(merchantId))
);
}
private void ValidateParameter(SipsParameter parameter, string merchantId)
{
Validate<NotFoundException>
(
(Rule: IsNullX(parameter, nameof(SipsParameter), nameof(merchantId), merchantId), Parameter: nameof(SipsParameter))
);
Validate<InvalidException>
(
(Rule: IsInvalidX(parameter.MerchantId), Parameter: nameof(SipsParameter.MerchantId)),
(Rule: IsInvalidX(parameter.Url), Parameter: nameof(SipsParameter.Url))
);
}
}