Skip to content

Commit

Permalink
worked on the remove budget and history functionality to give me mess…
Browse files Browse the repository at this point in the history
…ages if the data is not present
  • Loading branch information
ibilalkayy committed Apr 25, 2024
1 parent 815cfea commit 956458c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 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.89"
const version = "v0.1.90"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
4 changes: 3 additions & 1 deletion cmd/spend/sub_handler/show.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spend_subhandler

import (
"fmt"
"log"

"github.com/ibilalkayy/flow/db/budget_db"
Expand All @@ -13,10 +14,11 @@ var ShowCmd = &cobra.Command{
Short: "Show the history data",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
err := budget_db.ViewHistory(category)
table, err := budget_db.ViewHistory(category)
if err != nil {
log.Fatal(err)
}
fmt.Println(table[0])
},
}

Expand Down
43 changes: 35 additions & 8 deletions db/budget_db/budget_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func CreateBudget(bv *structs.BudgetVariables, basePath string) error {
}
fmt.Println("Budget data is successfully inserted!")
} else {
return errors.New("enter the category")
return errors.New("enter the category or set the total amount. see 'flow total-amount -h'")
}
return nil
}
Expand Down Expand Up @@ -108,24 +108,51 @@ func RemoveBudget(category string) error {
if err != nil {
return err
}
defer db.Close()

query := "DELETE FROM Budget WHERE categories=$1"
remove, err := db.Prepare(query)
data, err := ViewBudget(category)
if err != nil {
return err
}

defer remove.Close()
foundCategory, ok := data[1].(string)
if !ok {
return errors.New("unable to convert data to string")
}

query := "DELETE FROM Budget"
var args []interface{}

if len(category) != 0 {
_, err = remove.Exec(category)
if err != nil {
return err
if len(foundCategory) != 0 {
query += " WHERE categories=$1"
args = append(args, category)
} else {
return errors.New("category is not found")
}
}

remove, err := db.Prepare(query)
if err != nil {
return err
}
defer remove.Close()

_, err = remove.Exec(args...)
if err != nil {
return err
}

if len(category) != 0 {
fmt.Printf("'%s' category is successfully removed!\n", category)
} else {
fmt.Println("First enter the category and then remove it")
if len(foundCategory) != 0 {
fmt.Printf("Budget data is successfully deleted!")
} else {
return errors.New("no data is present")
}
}

return nil
}

Expand Down
36 changes: 27 additions & 9 deletions db/budget_db/history_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func InsertHistory(hv *structs.HistoryVariables, basePath string) error {
return nil
}

func ViewHistory(category string) error {
func ViewHistory(category string) ([2]interface{}, error) {
hv := new(structs.HistoryVariables)

db, err := db.Connection()
if err != nil {
return err
return [2]interface{}{}, err
}

defer db.Close()
Expand All @@ -71,14 +71,14 @@ func ViewHistory(category string) error {
rows, err = db.Query(query)
}
if err != nil {
return err
return [2]interface{}{}, err
}

defer rows.Close()

for rows.Next() {
if err := rows.Scan(&hv.Date, &hv.Time, &hv.Category, &hv.Amount, &hv.TransactionID, &hv.Blockchain, &hv.Address); err != nil {
return err
return [2]interface{}{}, err
}

if len(hv.Category) != 0 && hv.Amount != 0 {
Expand All @@ -87,8 +87,8 @@ func ViewHistory(category string) error {
}

tableRender := "History Data\n" + tw.Render()
fmt.Println(tableRender)
return nil
details := [2]interface{}{tableRender, hv.Category}
return details, nil
}

func RemoveHistory(category string) error {
Expand All @@ -98,12 +98,26 @@ func RemoveHistory(category string) error {
}
defer db.Close()

data, err := ViewHistory(category)
if err != nil {
return err
}

foundCategory, ok := data[1].(string)
if !ok {
return errors.New("unable to convert data to string")
}

query := "DELETE FROM History"
var args []interface{}

if len(category) != 0 {
query += " WHERE categories=$1"
args = append(args, category)
if len(foundCategory) != 0 {
query += " WHERE categories=$1"
args = append(args, category)
} else {
return errors.New("category is not found")
}
}

remove, err := db.Prepare(query)
Expand All @@ -120,7 +134,11 @@ func RemoveHistory(category string) error {
if len(category) != 0 {
fmt.Printf("'%s' category is successfully removed!\n", category)
} else {
fmt.Printf("History is successfully deleted!")
if len(foundCategory) != 0 {
fmt.Printf("History data is successfully deleted!")
} else {
return errors.New("no data is present")
}
}

return nil
Expand Down

0 comments on commit 956458c

Please sign in to comment.