public static TResult GetValueForSource <TResult>(this IResolveFieldContext resolveContext) { if (resolveContext == null) { throw new ArgumentNullException(nameof(resolveContext)); } TResult result = default; if (resolveContext.Source is IEntity entity) { result = resolveContext.GetValue <TResult>(entity.Id); } else if (resolveContext.Source is IValueObject valueObject) { result = resolveContext.GetValue <TResult>(((ValueObject)valueObject).GetCacheKey()); } return(result); }
public static Currency GetCurrencyByCode <T>(this IResolveFieldContext <T> userContext, string currencyCode) { var allCurrencies = userContext.GetValue <IEnumerable <Currency> >("allCurrencies"); var result = allCurrencies?.FirstOrDefault(x => x.Code.EqualsInvariant(currencyCode)); if (result == null) { throw new OperationCanceledException($"the currency with code '{ currencyCode }' is not registered"); } return(result); }
public static async Task <IDictionary <string, ExpProduct> > LoadProductsAsync(IMediator mediator, IResolveFieldContext context, IEnumerable <string> ids, string[] includeFields) { var request = new LoadProductsQuery { ObjectIds = ids.ToArray(), StoreId = context.GetValue <string>("storeId"), IncludeFields = includeFields.ToArray() }; var response = await mediator.Send(request); return(response.Products.ToDictionary(x => x.Id)); }
public static Currency GetOrderCurrencyByCode <T>(this IResolveFieldContext <T> userContext, string currencyCode) { //Try to get a currency from order if currency code is not set explicitly or undefined var result = userContext.GetOrderCurrency(); //If the passed currency differs from the order currency, we try to find it from the all registered currencies. if (result == null || (!string.IsNullOrEmpty(currencyCode) && !result.Code.EqualsInvariant(currencyCode))) { var allCurrencies = userContext.GetValue <IEnumerable <Currency> >("allCurrencies"); result = allCurrencies?.FirstOrDefault(x => x.Code.EqualsInvariant(currencyCode)); } if (result == null) { throw new OperationCanceledException($"the currency with code '{ currencyCode }' is not registered"); } return(result); }
public static T GetArgumentOrValue <T>(this IResolveFieldContext resolveContext, string key) { return(resolveContext.GetArgument <T>(key) ?? resolveContext.GetValue <T>(key)); }
public static T GetValue <T>(this IResolveFieldContext resolveContext, string key) { return(resolveContext.GetValue(key, default(T))); }