public static ResponseContext OnFailure(this ResponseContext response, ErrorType errorType, string errorMessage, params string[] errorMessageParameters) { //if (response.IsFailure && response.ErrorType == errorType) if (response.ErrorType == errorType) { return(ResponseContext.Fail(string.Format(errorMessage, errorMessageParameters), errorType) .withInnerResponse(response)); } return(response); }
public static async Task <ResponseContext <K> > MapAsync <T, K>(this Task <ResponseContext <T> > response, Func <T, K> func) { var @this = await response; if (@this.IsFailure) { return(@this.HasException ? ResponseContext.Fail <K>(@this.InnerException) : ResponseContext.Fail <K>(@this.Error, @this.ErrorType)); } return(ResponseContext.Ok(func(@this.Result))); }
public static async Task <ResponseContext> OnFailureAsync(this Task <ResponseContext> response, ErrorType errorType, string errorMessage, params string[] errorMessageParameters) { var @this = await response; if (@this.ErrorType == errorType) { return(ResponseContext.Fail(string.Format(errorMessage, errorMessageParameters), errorType) .withInnerResponse(@this)); } return(@this); }
public static async Task <ResponseContext <T> > EnsureAsync <T>(this Task <ResponseContext <T> > response, Func <ResponseContext <T>, bool> predicate, ErrorType errorType, string errorMessage, params string[] errorMessageParameters) { var @this = await response; if (@this.IsSuccess && !predicate(@this)) { return(ResponseContext.Fail <T>(string.Format(errorMessage, errorMessageParameters), errorType) .withInnerResponse(@this)); } return(@this); }
public async Task <ResponseContext <T> > UpdateAsync(T entity, params Expression <Func <T, bool> >[] keySelectors) { try { if (entity == null) { return(ResponseContext.Fail <T>(string.Format(ExceptionMessages.ArgumentException_ProvideValue, ResourceName), ErrorType.Argument)); } return((await this.Async(async r => (await ValidateUpdateEntity(entity)) .Then(() => OnUpdateSecurityClearance())) .ThenAsync(() => GetByIdBasicAsync(keySelectors)) .ThenAsync(result => EntityPatcher <T> .PatchAsync(entity, result.Value)) .ThenAsync(result => OnBeforeUpdate(result.Value)) .ThenAsync(result => OnUpdateAsync(result.Value)) .ThenAsync(result => IncludeInUpdateTransaction(result.Value)) .ThenIfAsync(CommitInmediately, async result => { Result <int> commitResult; if (_wrapUpdateInTrasaction) { commitResult = await AggregateParams.UOW.CommitWithinTransactionAsync(); } else { commitResult = await AggregateParams.UOW.CommitAsync(); } return commitResult.Ensure(r => r.Value > 0, ErrorType.CommitFailure, string.Format(ExceptionMessages.Exception_UpdateEntityError, ResourceName)) .Return(() => result); }) .ThenAsync(result => AfterUpdate(result))) .ToResponseContext() .withRecordCount(1) .ThenIf(AggregateParams.Context.IsHATEOASRequest, response => UpdateHATEOAS(response))); } catch (Exception e) { throw new Exception(string.Format(ExceptionMessages.Exception_UpdateEntityError, ResourceName), e); } }
public async Task <ResponseContext <T> > AddAsync(T entity) { try { if (entity == null) { return(ResponseContext.Fail <T>(string.Format(ExceptionMessages.ArgumentException_ProvideValue, ResourceName), ErrorType.Argument)); } return((await this.Async(async r => (await ValidateAddEntity(entity)) .Then(() => OnAddSecurityClearance())) .ThenAsync(() => OnBeforeAdd(entity)) .ThenAsync(result => OnAddAsync(result.Value)) .ThenAsync(result => IncludeInAddTransaction(result.Value)) .ThenIfAsync(CommitInmediately, async result => { Result <int> commitResult; if (_wrapAddInTrasaction) { commitResult = await AggregateParams.UOW.CommitWithinTransactionAsync(); } else { commitResult = await AggregateParams.UOW.CommitAsync(); } return commitResult.Ensure(r => r.Value > 0, ErrorType.CommitFailure, string.Format(ExceptionMessages.Exception_CreateEntityError, ResourceName)) .Return(() => result); }) .ThenAsync(result => AfterAdd(result))) .ToResponseContext() .withRecordCount(1) .ThenIf(AggregateParams.Context.IsHATEOASRequest, response => AddHATEOAS(response))); } catch (Exception e) { throw new Exception(string.Format(ExceptionMessages.Exception_CreateEntityError, ResourceName), e); } }
public static ResponseContext <K> Return <T, K>(this ResponseContext <T> result, Func <K> func) { if (result.IsFailure) { return(result.HasException ? ResponseContext.Fail <K>(result.InnerException) : ResponseContext.Fail <K>(result.Error, result.ErrorType)); } return(ResponseContext.Ok(func())); }
public static ResponseContext <K> Map <T, K>(this ResponseContext <T> response, Func <ResponseContext <T>, ResponseContext <K> > func) { if (response.IsFailure) { return(response.HasException ? ResponseContext.Fail <K>(response.InnerException) : ResponseContext.Fail <K>(response.Error, response.ErrorType)); } return(ResponseContext.Ok(func(response))); }