From 433b35baec4ea3d30e00fa2b29162d28465f683e Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Mon, 18 Mar 2024 20:08:53 +0500 Subject: [PATCH] added the total ammount and the condition for the category to be must entered --- README.md | 5 +++++ cmd/root.go | 2 +- internal/app/budget/budget.go | 35 ++++++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 15eaac4..c69b999 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ With a user-friendly CLI. It manages the finances and achieve greater financial ## Table of Contents +- [Documentation](#documentation) - [What You Need?](#what-you-need) - [Clonning](#clonning) - [Installation](#installation) @@ -15,6 +16,10 @@ With a user-friendly CLI. It manages the finances and achieve greater financial - [License](#license) +## Documentation + +To get to know about the application in detail, you can visit the [docs](https://github.com/ibilalkayy/flow/tree/main/docs) to understand the application in a better way. + ## What You Need? To get started in flow, you need to have two applications installed on your machine. diff --git a/cmd/root.go b/cmd/root.go index 28356e3..0c42d2c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.1.27" +const version = "v0.1.28" // rootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ diff --git a/internal/app/budget/budget.go b/internal/app/budget/budget.go index 05fdf81..fa7ddfc 100644 --- a/internal/app/budget/budget.go +++ b/internal/app/budget/budget.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "strconv" "github.com/ibilalkayy/flow/db/budget_db" "github.com/ibilalkayy/flow/internal/structs" @@ -23,14 +24,17 @@ func CreateBudget(bv *structs.BudgetVariables, basePath string) error { if err != nil { return err } - defer insert.Close() - _, err = insert.Exec(bv.Category, bv.Amount) - if err != nil { - return err + if len(bv.Category) != 0 { + _, err = insert.Exec(bv.Category, bv.Amount) + if err != nil { + return err + } + fmt.Println("Budget data is successfully inserted!") + } else { + return errors.New("category can't be empty") } - fmt.Println("Budget data is successfully inserted!") return nil } @@ -49,6 +53,9 @@ func ViewBudget(category string) (string, error) { tw := table.NewWriter() tw.AppendHeader(table.Row{"Category", "Amount"}) + // Initialize total amount + totalAmount := 0.0 + // Query the database based on the provided category var rows *sql.Rows if len(category) != 0 { @@ -68,11 +75,25 @@ func ViewBudget(category string) (string, error) { if err := rows.Scan(&bv.Category, &bv.Amount); err != nil { return "", err } - tw.AppendRow([]interface{}{bv.Category, bv.Amount}) + // Check if amount is empty + if bv.Amount != "" { + // Convert bv.Amount to float64 + amount, err := strconv.ParseFloat(bv.Amount, 64) + if err != nil { + return "", err + } + tw.AppendRow([]interface{}{bv.Category, amount}) + totalAmount += amount // Accumulate total amount + } } + // Add total amount row to the table + tw.AppendFooter(table.Row{"Total Amount", totalAmount}) + // Render the table - return "Budget Data\n" + tw.Render(), nil + tableRender := "Budget Data\n" + tw.Render() + + return tableRender, nil } func RemoveBudget(category string) error {