Skip to content

Commit

Permalink
updated the exception method and wrote it in update budget
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Jun 2, 2024
1 parent 675fc9e commit 1b6ea90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 41 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.125"
const version = "v0.1.126"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
10 changes: 8 additions & 2 deletions framework/db/budget_db/budget_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (h MyBudgetDB) UpdateBudget(bv *entities.BudgetVariables, new_category stri
totalBudgetAmount += amount
}

expectional_budget_amount, err := h.Deps.ManageBudget.BudgetAmountWithException(bv)
exceptionalBudgetAmount, _, err := h.Deps.ManageBudget.ListOfExpection(bv)
if err != nil {
return err
}
Expand Down Expand Up @@ -283,10 +283,16 @@ func (h MyBudgetDB) UpdateBudget(bv *entities.BudgetVariables, new_category stri
}
}

if len(includedCategory) != 0 && fullTotalAmount != 0 && bv.Amount <= fullTotalAmount && totalBudgetAmount <= fullTotalAmount && expectional_budget_amount+bv.Amount <= fullTotalAmount {
sum := 0
for _, exception := range exceptionalBudgetAmount {
sum += exception
}

if len(includedCategory) != 0 && fullTotalAmount != 0 && bv.Amount <= fullTotalAmount && totalBudgetAmount <= fullTotalAmount && sum+bv.Amount <= fullTotalAmount {
if len(new_category) != 0 && bv.Amount != 0 {
log.Fatal("Update one at a time. either amount or category")
}

if bv.Amount != 0 {
err = h.Deps.TotalAmount.CalculateRemaining(bv.Category)
if err != nil {
Expand Down
48 changes: 12 additions & 36 deletions framework/db/budget_db/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,61 +37,37 @@ func (h MyBudgetDB) TakeBudgetAmount() ([]string, []int, error) {
return categories, amounts, nil
}

func (h MyBudgetDB) ListOfExpection(bv *entities.BudgetVariables) ([]int, error) {
var spents []int
func (h MyBudgetDB) ListOfExpection(bv *entities.BudgetVariables) ([]int, []int, error) {
var amounts, spents []int

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

query := "SELECT spent FROM Budget WHERE NOT (categories=$1)"
query := "SELECT amounts, spent FROM Budget WHERE NOT (categories=$1)"
rows, err := db.Query(query, &bv.Category)
if err != nil {
return nil, err
return nil, nil, err
}
defer rows.Close()

for rows.Next() {
var spent int
err := rows.Scan(&spent)
var amount, spent int
err := rows.Scan(&amount, &spent)
if err != nil {
return nil, err
return nil, nil, err
}
amounts = append(amounts, amount)
spents = append(spents, spent)
}

if err := rows.Err(); err != nil {
return nil, err
}

return spents, nil
}

func (h MyBudgetDB) BudgetAmountWithException(bv *entities.BudgetVariables) (int, error) {
var amounts int

db, err := h.Deps.Connect.Connection()
if err != nil {
return 0, err
}
defer db.Close()

query := "SELECT amounts FROM Budget WHERE NOT (categories=$1)"
rows, err := db.Query(query, &bv.Category)
if err != nil {
return 0, err
return nil, nil, err
}
defer rows.Close()

for rows.Next() {
err := rows.Scan(&amounts)
if err != nil {
return 0, err
}
}
return amounts, nil
return amounts, spents, nil
}

func (h MyBudgetDB) CalculateRemaining(cr *entities.BudgetCalculateVariables) ([2]int, error) {
Expand All @@ -113,7 +89,7 @@ func (h MyBudgetDB) CalculateRemaining(cr *entities.BudgetCalculateVariables) ([
}

category := entities.BudgetVariables{Category: cr.Category}
spentAmounts, err := h.Deps.ManageBudget.ListOfExpection(&category)
_, spentAmounts, err := h.Deps.ManageBudget.ListOfExpection(&category)
if err != nil {
return [2]int{}, err
}
Expand Down
3 changes: 1 addition & 2 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ type SpendAmount interface {
type ManageBudget interface {
CreateBudget(bv *entities.BudgetVariables) error
TakeBudgetAmount() ([]string, []int, error)
BudgetAmountWithException(bv *entities.BudgetVariables) (int, error)
ListOfExpection(bv *entities.BudgetVariables) ([]int, error)
ListOfExpection(bv *entities.BudgetVariables) ([]int, []int, error)
ViewBudget(category string) ([5]interface{}, error)
UpdateBudget(bv *entities.BudgetVariables, new_category string) error
UpdateBudgetCategory(new, old string) error
Expand Down

0 comments on commit 1b6ea90

Please sign in to comment.