Skip to content

Commit

Permalink
during the budget creation, I created a functionality to compare it w…
Browse files Browse the repository at this point in the history
…ith the total amount also
  • Loading branch information
ibilalkayy committed May 6, 2024
1 parent ad282e2 commit ede3101
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.1.102"
const version = "v0.1.103"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
44 changes: 41 additions & 3 deletions framework_drivers/db/budget_db/budget_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ func (m MyBudgetDatabase) CreateBudget(bv *entities.BudgetVariables) error {
return err
}

totalAmount, ok := value[0].(int)
fullTotalAmount, ok := value[0].(int)
if !ok {
return errors.New("unable to convert to int")
}

budgetAmount, err := m.TakeBudgetAmounts()
if err != nil {
return nil
}

budgetTotalAmount := 0
for _, amount := range budgetAmount {
budgetTotalAmount += amount
}

for i := 0; i < len(includedCategory); i++ {
if len(bv.Category) != 0 && len(includedCategory) != 0 && includedCategory[i][0] == bv.Category && totalAmount != 0 {
if len(bv.Category) != 0 && len(includedCategory) != 0 && includedCategory[i][0] == bv.Category && fullTotalAmount != 0 && budgetTotalAmount+bv.Amount <= fullTotalAmount {
_, err = insert.Exec(bv.Category, bv.Amount, 0, 0)
if err != nil {
return err
Expand All @@ -44,7 +54,35 @@ func (m MyBudgetDatabase) CreateBudget(bv *entities.BudgetVariables) error {
return nil
}
}
return errors.New("enter the category in the total amount. see 'flow total-amount -h'")
return errors.New("enter the category in the total amount or set the budget below or equal to total amount. see 'flow total-amount -h'")
}

func (m MyBudgetDatabase) TakeBudgetAmounts() ([]int, error) {
bv := new(entities.BudgetVariables)
var amounts []int

db, err := m.Connection()
if err != nil {
return nil, err
}
defer db.Close()

query := "SELECT amounts FROM Budget"
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
err := rows.Scan(&bv.Amount)
if err != nil {
return nil, err
}
amounts = append(amounts, bv.Amount)
}

return amounts, nil
}

func (m MyBudgetDatabase) ViewBudget(category string) ([5]interface{}, error) {
Expand Down
1 change: 1 addition & 0 deletions interface_adapters/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type TotalAmountDatabase interface {

type BudgetDatabase interface {
CreateBudget(bv *entities.BudgetVariables) error
TakeBudgetAmounts() ([]int, error)
ViewBudget(category string) ([5]interface{}, error)
RemoveBudget(category string) error
UpdateBudget(old, new string, amount int) error
Expand Down
3 changes: 0 additions & 3 deletions usecases/app/spend/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func (m MySpending) SpendMoney(category string, spending_amount int) error {
BudgetCategoryRemainingAmount: budget_category_remaining_amount,
}

fmt.Println(included_categories_in_total_amount[1][0])
fmt.Println(included_categories_in_total_amount[0][0])

err = m.ValidBudgetValues(&sv)
if err != nil {
return err
Expand Down

0 comments on commit ede3101

Please sign in to comment.