public void FetchTwoAssetConfig_ValidRequestWithFilterValueDateTime_ValidQuery() { var currentDateTime = DateTime.Now; AssetSettingsDto request = new AssetSettingsDto { TargetValues = new Dictionary <AssetTargetType, Tuple <Guid, double> > { { AssetTargetType.IdlingBurnRateinLiPerHour, new Tuple <Guid, double>(Guid.NewGuid(), 12) }, { AssetTargetType.WorkingBurnRateinLiPerHour, new Tuple <Guid, double>(Guid.NewGuid(), 22) } }, FilterCriteria = new List <KeyValuePair <string, Tuple <string, object> > > { new KeyValuePair <string, Tuple <string, object> >("LIKE", Tuple.Create <string, object>("a.AssetName", currentDateTime)) } }; var result = _assetSettingsRepository.FetchAssetConfig(_assetUIDs, request).Result; var queryExpected = string.Format( AssetConfigRepository.Queries.FetchAssetConfig, string.Join(",", _assetUIDs.Select(x => "UNHEX('" + x + "')")).Replace("-", string.Empty), //Asset UIDs Lists "a.AssetName LIKE '" + currentDateTime + "' AND ", string.Join(",", request.TargetValues.Keys.Select(x => string.Format("'{0}'", x.ToString()))) ); _transactions.Received(1).GetAsync <AssetSettingsDto>(Arg.Is <string>(queryExpected), Arg.Is <dynamic>(request)); }
private string BuildFilterCriteria(AssetSettingsDto request) { try { this._loggingService.Debug("Started Building Filter Criteria", MethodInfo.GetCurrentMethod().Name); StringBuilder filterCriteria = new StringBuilder(string.Empty); if (request.FilterCriteria != null) { foreach (var filter in request.FilterCriteria) { var filterValue = filter.Value.Item2; Type filterValueType; if (filterValue != null) { filterValueType = filterValue.GetType(); if (filterValueType == typeof(string) || filterValueType == typeof(DateTime)) { filterCriteria.AppendFormat("{0} {1} '{2}' AND ", filter.Value.Item1, filter.Key, filterValue); } else if (filterValueType == null) { filterCriteria.AppendFormat("{0} {1} {2} AND ", filter.Value.Item1, filter.Key, filterValue); } } else { if (string.Compare(filter.Key, "is", true) >= 0) { filterCriteria.AppendFormat("{0} is null AND ", filter.Value.Item1); } } } } this._loggingService.Debug("Built Filter Criteria : " + filterCriteria.ToString(), MethodInfo.GetCurrentMethod().Name); this._loggingService.Debug("Ended Building Filter Criteria", MethodInfo.GetCurrentMethod().Name); return(filterCriteria.ToString()); } catch (Exception ex) { throw ex; } }
private IDictionary <AssetTargetType, Tuple <Guid, double> > AssignAssetTargetValues(IDictionary <AssetTargetType, double?> requestTargetValues, AssetSettingsDto asset) { IDictionary <AssetTargetType, Tuple <Guid, double> > targetValues = new Dictionary <AssetTargetType, Tuple <Guid, double> >(); if (asset.TargetValues == null) { asset.TargetValues = new Dictionary <AssetTargetType, Tuple <Guid, double> >(); } foreach (var targetValue in requestTargetValues) { Tuple <Guid, double> tuple = new Tuple <Guid, double>(Guid.Empty, 0); if (asset.TargetValues.ContainsKey(targetValue.Key)) { tuple = new Tuple <Guid, double>(asset.TargetValues[targetValue.Key].Item1, Convert.ToDouble(targetValue.Value)); } targetValues.Add(targetValue.Key, tuple); } return(targetValues); }
private string BuildTargetTypes(AssetSettingsDto request) { return(string.Join(",", request.TargetValues.Keys.Select(x => string.Format("'{0}'", x.ToString())))); }
public async Task <IEnumerable <AssetSettingsDto> > FetchAssetConfig(List <string> assetUids, AssetSettingsDto request) { try { this._loggingService.Debug("Started executing query", MethodInfo.GetCurrentMethod().Name); var result = await this._transactions.GetAsync <AssetSettingsDto>( string.Format( Queries.FetchAssetConfig, string.Join(",", assetUids.Select(x => "UNHEX('" + x + "')")).Replace("-", string.Empty), //Asset UIDs Lists this.BuildFilterCriteria(request), this.BuildTargetTypes(request) ), request); this._loggingService.Debug("Ended executing query", MethodInfo.GetCurrentMethod().Name); return(result); } catch (Exception ex) { this._loggingService.Error("Exception occurred while executing query", MethodInfo.GetCurrentMethod().Name, ex); throw ex; } }