public IEnumerable <LimitDefinition> GetDefinitions(string path, bool includeVariants) { List <LimitDefinition> returnValue = new List <LimitDefinition>(); using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection"))) { using (SqlCommand command = new SqlCommand("get-limits-by-path")) { command.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter parameter = command.Parameters.AddWithValue("@paths", _dataHelperService.DecodePaths(path, includeVariants)); parameter.SqlDbType = SqlDbType.Structured; parameter.TypeName = "path-array-type"; command.Connection = connection; connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { var limitDefinition = new LimitDefinition { CreatedAt = reader.GetDateTime("created-at"), RenewedAt = reader.GetDateTime("Renewed-at"), Path = reader.GetString("path"), MaxAmountLimit = reader.GetDecimal("max-amount-limit"), MaxAmountLimitCurrencyCode = reader.GetString("max-amount-limit-currency-code"), DefaultAmountLimit = reader.GetDecimal("default-amount-limit"), DefaultTimerLimit = reader.GetInt32("default-timer-limit"), TransactionLimit = new TransactionLimitDefinition { MinimumLimit = reader.GetDecimal("transaction-min-limit"), MaximumLimit = reader.GetDecimal("transaction-max-limit"), CurrencyCode = reader.GetString("currency-code") }, AmountLimit = new AmountLimitDefinition { Limit = reader.GetDecimal("amount-limit"), Utilized = reader.GetDecimal("amount-utilized-limit"), Remaining = reader.GetDecimal("amount-remaining-limit"), CurrencyCode = reader.GetString("currency-code") }, TimerLimit = new TimerLimitDefinition { Limit = reader.GetInt32("timer-limit"), Utilized = reader.GetInt32("timer-utilized-limit"), Remaining = reader.GetInt32("timer-remaining-limit"), MaxTimerLimit = reader.GetInt32("max-timer-limit") }, Duration = new DurationDefinition { Span = reader.GetString("duration"), Renewal = (RenewalType)reader.GetByte("renewal") }, isActive = reader.GetBoolean("is-active"), alsoLook = reader.GetString("also-look") }; if (!reader.IsDBNull("availability")) { limitDefinition.Availability = JsonConvert.DeserializeObject <AvailabilityDefinition>(reader.GetString("availability")); } returnValue.Add(limitDefinition); } } reader.Close(); connection.Close(); } } return(returnValue.ToArray()); }
public LimitDefinition RequestToDefinition(UpdateOrCreateLimitDefinitionRequestDefinition request) { LimitDefinition def = new LimitDefinition { Path = request.Path, DefaultAmountLimit = request.DefaultAmountLimit, DefaultTimerLimit = request.DefaultTimerLimit, MaxAmountLimit = request.MaxAmountLimit, MaxAmountLimitCurrencyCode = request.MaxAmountLimitCurrencyCode, TransactionLimit = new TransactionLimitDefinition { MinimumLimit = request.TransactionMinimumLimit ?? default(int), MaximumLimit = request.TransactionMaximumLimit ?? default(int), CurrencyCode = request.CurrencyCode }, AmountLimit = new AmountLimitDefinition { Limit = request.AmountLimit ?? default(int), Utilized = 0, Remaining = request.AmountLimit ?? default(int), CurrencyCode = request.CurrencyCode }, TimerLimit = new TimerLimitDefinition { Limit = request.TimerLimit ?? default(int), Utilized = 0, Remaining = request.TimerLimit ?? default(int), MaxTimerLimit = request.MaxTimerLimit ?? default(int) }, Duration = new DurationDefinition { Span = request.Span, Renewal = request.Renewal ?? default(RenewalType) }, isActive = request.isActive ?? default(bool), alsoLook = request.alsoLook }; return(def); }
public LimitDefinition PatchDefinition(string path, string duration, PatchRequestDefinition data) { if (path == "" || path == null) { throw new NullPathException(); } if (duration == "" || duration == null) { throw new NullDurationException(); } if (_dataService.GetDefinitions(path, false).Where(x => x.Duration.Span == duration).FirstOrDefault() == null) { throw new PathNotDefinedException(); } if (data.NewAmountUtilizedLimit < 0 && data.NewAmountUtilizedLimit != -1) { throw new InvalidAmountException(); } if (data.NewTimerUtilizedLimit < 0 && data.NewTimerUtilizedLimit != -1) { throw new InvalidTimerLimitException(); } _dataService.PatchLimit(path, duration, data); LimitDefinition result = _dataService.GetDefinitions(path, false).Where(x => x.Duration.Span == duration).FirstOrDefault(); return(result); }
public void AutoLimitUpdateOnTheFly(LimitDefinition definition) { if (definition.Duration.Renewal == RenewalType.elapsed)// Periyodik yenilenebilir olup olmadığı kontrol ediliyor. { var lastExpectedRenewalDate = CrontabSchedule.Parse(GetCron(definition.Duration.Span)).GetNextOccurrences(DateTime.Now.AddYears(-1), DateTime.Now).Last(); if (definition.RenewedAt < lastExpectedRenewalDate)// yenilenme zamanının gelip gelmediğini kontrol ediyoruz. { definition.RenewedAt = lastExpectedRenewalDate; definition.AmountLimit.Remaining = definition.AmountLimit.Limit; definition.AmountLimit.Utilized = 0; definition.TimerLimit.Remaining = definition.TimerLimit.Limit; definition.TimerLimit.Utilized = 0; } } }
public SearchDefinitionsResponseDefinition SearchDefinitions(string query, int pageIndex, int pageSize, bool isActive) { query = query.Replace('~', '%'); List <LimitDefinition> limitDefinitions = new List <LimitDefinition>(); SearchDefinitionsResponseDefinition returnValue = new SearchDefinitionsResponseDefinition(); using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection"))) { using (SqlCommand command = new SqlCommand("search-limits")) { command.CommandType = System.Data.CommandType.StoredProcedure; command.Parameters.AddWithValue("@query", query); command.Parameters.AddWithValue("@isActive", isActive); command.Parameters.AddWithValue("@pageSize", pageSize); command.Parameters.AddWithValue("@pageIndex", pageIndex); command.Connection = connection; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { var limitDefinition = new LimitDefinition { CreatedAt = reader.GetDateTime("created-at"), RenewedAt = reader.GetDateTime("Renewed-at"), Path = reader.GetString("path"), DefaultAmountLimit = reader.GetDecimal("default-amount-limit"), DefaultTimerLimit = reader.GetInt32("default-timer-limit"), MaxAmountLimit = reader.GetDecimal("max-amount-limit"), MaxAmountLimitCurrencyCode = reader.GetString("max-amount-limit-currency-code"), TransactionLimit = new TransactionLimitDefinition { MinimumLimit = reader.GetDecimal("transaction-min-limit"), MaximumLimit = reader.GetDecimal("transaction-max-limit"), CurrencyCode = reader.GetString("currency-code") }, AmountLimit = new AmountLimitDefinition { Limit = reader.GetDecimal("amount-limit"), Utilized = reader.GetDecimal("amount-utilized-limit"), Remaining = reader.GetDecimal("amount-remaining-limit"), CurrencyCode = reader.GetString("currency-code") }, TimerLimit = new TimerLimitDefinition { Limit = reader.GetInt32("timer-limit"), Utilized = reader.GetInt32("timer-utilized-limit"), Remaining = reader.GetInt32("timer-remaining-limit"), MaxTimerLimit = reader.GetInt32("max-timer-limit") }, Duration = new DurationDefinition { Span = reader.GetString("duration"), Renewal = (RenewalType)reader.GetByte("renewal") }, isActive = reader.GetBoolean("is-active"), alsoLook = reader.GetString("also-look") }; if (!reader.IsDBNull("availability")) { limitDefinition.Availability = JsonConvert.DeserializeObject <AvailabilityDefinition>(reader.GetString("availability")); } limitDefinitions.Add(limitDefinition); } reader.Close(); connection.Close(); } if (limitDefinitions.Count > pageSize) { limitDefinitions.RemoveAt(limitDefinitions.Count - 1); returnValue.HasNextPage = true; } else { returnValue.HasNextPage = false; } returnValue.LimitDefinitions = limitDefinitions; return(returnValue); } }