Skip to content

Commit

Permalink
updated the alert functionality by putting more conditions while inse…
Browse files Browse the repository at this point in the history
…rting the data
  • Loading branch information
ibilalkayy committed May 8, 2024
1 parent 777515e commit 174a3bf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 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.104"
const version = "v0.1.105"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion interface_adapters/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BudgetDatabase interface {
}

type Budget interface {
CategoryAmount(category string) (int, error)
CategoryAmount(category string) (string, int, error)
}

type HistoryDatabase interface {
Expand Down
11 changes: 5 additions & 6 deletions usecases/app/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (m MyAlerts) AlertSetup(av *entities.AlertVariables) error {
if len(av.Category) != 0 && len(av.Frequency) != 0 && len(av.Method) != 0 {
if len(av.Category) != 0 && len(av.Frequency) != 0 && len(av.Method) != 0 && av.Days != 0 && len(av.Weekdays) != 0 && av.Hours != 0 && av.Minutes != 0 && (av.Seconds >= 0 && av.Seconds <= 60) {
validMethods := map[string]bool{"email": true, "cli": true}
validFrequencies := map[string]bool{"hourly": true, "daily": true, "weekly": true, "monthly": true}

Expand All @@ -22,23 +22,22 @@ func (m MyAlerts) AlertSetup(av *entities.AlertVariables) error {
return errors.New("invalid alert frequency")
}

categoryAmount, err := m.CategoryAmount(av.Category)
category, categoryAmount, err := m.CategoryAmount(av.Category)
if err != nil {
return err
}

if categoryAmount != 0 {
if len(category) != 0 && categoryAmount != 0 {
err := m.CreateAlert(av)
if err != nil {
return err
}
fmt.Printf("Alert is set for the '%s' category\n", av.Category)
} else {
return errors.New("category amount is not present")
return errors.New("budget data is not found. first set the budget")
}
} else {
return errors.New("enter all the flags properly")
// fmt.Printf("You can't spend more than your '%s' category budget\n", av.Category)
return errors.New("enter all the required flags properly")
}
return nil
}
Expand Down
20 changes: 10 additions & 10 deletions usecases/app/budget/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ import (
"github.com/ibilalkayy/flow/entities"
)

func (m MyBudget) CategoryAmount(category string) (int, error) {
func (m MyBudget) CategoryAmount(category string) (string, int, error) {
bv := new(entities.BudgetVariables)

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

checkQuery := "SELECT COUNT(*) FROM Budget WHERE categories=$1"
var count int
err = db.QueryRow(checkQuery, category).Scan(&count)
if err != nil {
return 0, nil
return "", 0, nil
}

if count == 0 {
return 0, errors.New("category not found")
return "", 0, errors.New("category not found in the budget")
}

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

for rows.Next() {
if err := rows.Scan(&bv.Amount); err != nil {
return 0, err
if err := rows.Scan(&bv.Category, &bv.Amount); err != nil {
return "", 0, err
}
}
if err := rows.Err(); err != nil {
return 0, err
return "", 0, err
}

return bv.Amount, nil
return bv.Category, bv.Amount, nil
}

0 comments on commit 174a3bf

Please sign in to comment.