public byte UpdateBatch(string query, string duration, int timerLimit, decimal amountLimit, string currencyCode, BatchUpdateLimitDefinition newLimits) { query = query.Replace('~', '%'); int rows; using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection"))) { string cmdSelectString = " SELECT COUNT(1) FROM [dbo].[limit-definition]"; cmdSelectString += " WHERE [path] like @pathQuery"; if (duration != "-2") { cmdSelectString += " AND [duration] like @duration"; } if (amountLimit != -2) { cmdSelectString += " AND [amount-limit] = @oldAmountLimit"; } if (timerLimit != -2) { cmdSelectString += " AND [timer-limit] = @oldTimerLimit"; } cmdSelectString += " AND [currency-code] = @CurrencyCode"; connection.Open(); SqlCommand cmd = new SqlCommand(cmdSelectString, connection); cmd.Parameters.AddWithValue("@pathQuery", query); if (duration != "-2") { cmd.Parameters.AddWithValue("@duration", duration); } if (amountLimit != -2) { cmd.Parameters.AddWithValue("@oldAmountLimit", amountLimit); } if (timerLimit != -2) { cmd.Parameters.AddWithValue("@oldTimerLimit", timerLimit); } cmd.Parameters.AddWithValue("@CurrencyCode", currencyCode); rows = (int)cmd.ExecuteScalar(); connection.Close(); } using (var connection = new SqlConnection(_configuration.GetConnectionString("DefaultConnection"))) { int counter = 0; string cmdString; cmdString = "Update [dbo].[limit-definition] SET "; if (newLimits.AmountLimit != -2) { cmdString += "[amount-limit] = @amountLimit"; cmdString += ", [amount-remaining-limit] = case when @amountLimit=-1 THEN 0 ELSE @amountLimit - [amount-utilized-limit] END"; counter++; } if (newLimits.DefaultAmountLimit != -2) { if (counter > 0) { cmdString += ", [default-amount-limit] = @defaultAmountLimit"; } else { cmdString += "[default-amount-limit] = @defaultAmountLimit"; counter++; } } if (newLimits.MaxAmountLimit != -2) { if (counter > 0) { cmdString += ", [max-amount-limit] = @maxAmountLimit"; } else { cmdString += "[max-amount-limit] = @maxAmountLimit"; counter++; } } if (newLimits.TimerLimit != -2) { if (counter > 0) { cmdString += ", [timer-limit] = @timerLimit"; cmdString += ", [timer-remaining-limit] = @timerLimit - [timer-utilized-limit]"; } else { cmdString += "[timer-limit] = @timerLimit"; cmdString += ", [timer-remaining-limit] = @timerLimit - [timer-utilized-limit]"; counter++; } } if (newLimits.DefaultTimerLimit != -2) { if (counter > 0) { cmdString += ", [default-timer-limit] = @defaultTimerLimit"; } else { cmdString += "[default-timer-limit] = @defaultTimerLimit"; counter++; } } if (newLimits.MaxTimerLimit != -2) { if (counter > 0) { cmdString += ", [max-timer-limit] = @maxTimerLimit"; } else { cmdString += "[max-timer-limit] = @maxTimerLimit"; } } cmdString += " Where [path] like @pathQuery"; if (duration != "-2") { cmdString += " AND [duration] like @duration"; } if (amountLimit != -2) { cmdString += " AND [amount-limit] = @oldAmountLimit"; } if (timerLimit != -2) { cmdString += " AND [timer-limit] = @oldTimerLimit"; } cmdString += " AND [currency-code] like @currencyCode"; connection.Open(); SqlCommand command = new SqlCommand(cmdString, connection); command.Parameters.AddWithValue("@pathQuery", query); if (duration != "-2") { command.Parameters.AddWithValue("@duration", duration); } if (currencyCode != "-2") { command.Parameters.AddWithValue("@currencyCode", currencyCode); } if (amountLimit != -2) { command.Parameters.AddWithValue("@oldAmountLimit", amountLimit); } if (timerLimit != -2) { command.Parameters.AddWithValue("@oldTimerLimit", timerLimit); } if (newLimits.AmountLimit != -2) { command.Parameters.AddWithValue("@amountLimit", newLimits.AmountLimit); } if (newLimits.DefaultAmountLimit != -2) { command.Parameters.AddWithValue("@defaultAmountLimit", newLimits.DefaultAmountLimit); } if (newLimits.MaxAmountLimit != -2) { command.Parameters.AddWithValue("@maxAmountLimit", newLimits.MaxAmountLimit); } if (newLimits.TimerLimit != -2) { command.Parameters.AddWithValue("@timerLimit", newLimits.TimerLimit); } if (newLimits.DefaultTimerLimit != -2) { command.Parameters.AddWithValue("@defaultTimerLimit", newLimits.DefaultTimerLimit); } if (newLimits.MaxTimerLimit != -2) { command.Parameters.AddWithValue("@maxTimerLimit", newLimits.MaxTimerLimit); } int result = command.ExecuteNonQuery(); connection.Close(); if (result == rows) { return(0); } else if (result < rows) { return(1); } else if (result == 0) { return(2); } else { return(3); } } }
public byte updateBatch(string query, string duration, int timerLimit, decimal amountLimit, string currencyCode, BatchUpdateLimitDefinition newLimits) { if (newLimits.AmountLimit < 0 && newLimits.AmountLimit != -1 && newLimits.AmountLimit != -2) { throw new InvalidAmountLimitException { } } ; if (newLimits.TimerLimit < 0 && newLimits.TimerLimit != -1 && newLimits.TimerLimit != -2) { throw new InvalidTimerLimitException { } } ; if (newLimits.MaxAmountLimit < 0 && newLimits.MaxAmountLimit != -1 && newLimits.MaxAmountLimit != -2) { throw new InvalidMaxAmountLimitException { } } ; if (newLimits.MaxTimerLimit < 0 && newLimits.MaxTimerLimit != -1 && newLimits.MaxTimerLimit != -2) { throw new InvalidMaxTimerLimitException { } } ; if (newLimits.DefaultAmountLimit < 0 && newLimits.DefaultAmountLimit != -1 && newLimits.DefaultAmountLimit != -2) { throw new InvalidDefaultAmountLimitException { } } ; if (newLimits.DefaultTimerLimit < 0 && newLimits.DefaultTimerLimit != -1 && newLimits.DefaultTimerLimit != -2) { throw new InvalidDefaultTimerLimitException { } } ; if (newLimits.AmountLimit > newLimits.MaxAmountLimit && newLimits.MaxAmountLimit != -1 && newLimits.MaxAmountLimit != -2) { throw new AmountLimitBiggerThanMaxException(null); } if (newLimits.TimerLimit > newLimits.MaxTimerLimit && newLimits.MaxTimerLimit != -1 && newLimits.MaxTimerLimit != -2) { throw new TimerLimitBiggerThanMaxException(null); } if (!_businessHelperService.CheckCurrencyCode(currencyCode)) { throw new InvalidCurrencyCodeException { } } ; byte result = _dataService.UpdateBatch(query, duration, timerLimit, amountLimit, currencyCode, newLimits); if (result == 2) { throw new BatchUpdateFailedException { } } ; else { return(result); } } } }