Skip to content

Commit

Permalink
fixed the alert messaging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Mar 19, 2024
1 parent 92e12a4 commit 244dd3d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/budget/handler/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var AlertCmd = &cobra.Command{
Method: method,
}

err := app.Alert(av)
err := app.AlertSetup(av)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/budget/handler/msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package handler

import (
"fmt"
"log"

app "github.com/ibilalkayy/flow/internal/app/budget"
"github.com/spf13/cobra"
)

Expand All @@ -11,7 +12,10 @@ var MsgCmd = &cobra.Command{
Use: "msg",
Short: "The message of alert notifications",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("msg is called")
err := app.AlertMessage()
if err != nil {
log.Fatal(err)
}
},
}

Expand Down
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.29"
const version = "v0.1.30"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
10 changes: 8 additions & 2 deletions cmd/transaction/transaction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package transaction

import (
"fmt"
Expand All @@ -7,12 +7,18 @@ import (
"github.com/spf13/cobra"
)

func TransactionAmount() int {
amount := 1600
return amount
}

// transactionCmd represents the transaction command
var TransactionCmd = &cobra.Command{
Use: "transaction",
Short: "Transaction service",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("transaction is called")
amount := TransactionAmount()
fmt.Println(amount)
},
}

Expand Down
31 changes: 21 additions & 10 deletions internal/app/budget/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strconv"

"github.com/ibilalkayy/flow/cmd/transaction"
"github.com/ibilalkayy/flow/db/budget_db"
"github.com/ibilalkayy/flow/internal/structs"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -252,8 +253,8 @@ func CategoryAmount(category string) (string, error) {
}
}

func TotalBudgetAmount() (float64, error) {
var totalAmount float64
func TotalBudgetAmount() (int, error) {
var totalAmount int
bv := new(structs.BudgetVariables)

db, err := budget_db.Connection()
Expand All @@ -276,7 +277,7 @@ func TotalBudgetAmount() (float64, error) {
if bv.Amount == "" {
continue // Skip empty amounts
}
amount, err := strconv.ParseFloat(bv.Amount, 64)
amount, err := strconv.Atoi(bv.Amount)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -315,14 +316,8 @@ func Method(value string) (string, error) {
}
}

func Alert(av structs.AlertVariables) error {
func AlertSetup(av structs.AlertVariables) error {
if len(av.Category) != 0 && len(av.Frequency) != 0 && len(av.Method) != 0 {
totalAmount, err := TotalBudgetAmount()
if err != nil {
return err
}
fmt.Println(totalAmount)

amount, err := CategoryAmount(av.Category)
if err != nil {
return err
Expand All @@ -341,8 +336,24 @@ func Alert(av structs.AlertVariables) error {
fmt.Println(amount)
fmt.Println(frequencyMsg)
fmt.Println(method)
fmt.Println("Your alart message is setup")
} else {
return errors.New("enter all the alert values")
}
return nil
}

func AlertMessage() error {
totalAmount, err := TotalBudgetAmount()
if err != nil {
return err
}
transactionAmount := transaction.TransactionAmount()

if transactionAmount >= totalAmount {
fmt.Printf("You can't spend more becuase your budget is set to %d\n", totalAmount)
} else {
return errors.New("enjoy your spending")
}
return nil
}

0 comments on commit 244dd3d

Please sign in to comment.