// Add new rule // (Requirement 1.0.4) public static void SaveRule(RuleModel rule) { using (IDbConnection conn = new SQLiteConnection(LoadConnectionString())) { conn.Execute(saveRuleQuery, rule); } }
private void Rules_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { // Need to get the new user-edited value with a switch statement var c = e.Column as DataGridBoundColumn; if (c != null) { RuleModel editedRule = (RuleModel)e.Row.DataContext; int rowIndex = e.Row.GetIndex(); var el = e.EditingElement as TextBox; var changedColumn = (c.Binding as Binding).Path.Path; switch (changedColumn) { case "Rule_name": if (el.Text == "") { Rules_List.CancelEdit(); return; } editedRule.Rule_name = el.Text; break; case "Category": if (el.Text == "") { Rules_List.CancelEdit(); return; } editedRule.Category = el.Text; break; case "Payee_regex": if (el.Text == "") { Rules_List.CancelEdit(); return; } editedRule.Payee_regex = el.Text; break; case "Direction": if (el.Text != "Inflow" && el.Text != "Outflow") { Rules_List.CancelEdit(); return; } editedRule.Direction = el.Text; break; default: return; } // save edited transaction to DB and refresh UI viewModel.UpdateRule(editedRule); } } }
private static bool TryCategorizeTransaction(TransactionModel transaction, RuleModel rule) { // Tests if this rule matches the transaction. Sets appropriate category if it does. // check in/out direction constraint if ((rule.Direction == "Inflow" && transaction.Amount < 0) || (rule.Direction == "Outflow" && transaction.Amount > 0)) { return(false); } // check if rule matches var rx = new Regex(rule.Payee_regex); if (rx.IsMatch(transaction.Payee)) { transaction.Category = rule.Category; return(true); } return(false); }