Skip to content

Commit

Permalink
worked on the show and remove history functionalities and updated the…
Browse files Browse the repository at this point in the history
… total-amount update and remove functionalities
  • Loading branch information
ibilalkayy committed Apr 24, 2024
1 parent 6a2a978 commit 8ebd738
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 42 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.87"
const version = "v0.1.88"

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

import (
"log"
"fmt"

"github.com/ibilalkayy/flow/db/budget_db"
spend_subhandler "github.com/ibilalkayy/flow/cmd/spend/sub_handler"
"github.com/spf13/cobra"
)

Expand All @@ -12,14 +12,11 @@ var HistoryCmd = &cobra.Command{
Use: "history",
Short: "Show the transaction history",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
err := budget_db.ViewHistory(category)
if err != nil {
log.Fatal(err)
}
fmt.Println("no command is written")
},
}

func init() {
HistoryCmd.Flags().StringP("category", "c", "", "Write the category to show it's history")
HistoryCmd.AddCommand(spend_subhandler.ShowCmd)
HistoryCmd.AddCommand(spend_subhandler.RemoveCmd)
}
25 changes: 25 additions & 0 deletions cmd/spend/sub_handler/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spend_subhandler

import (
"log"

"github.com/ibilalkayy/flow/db/budget_db"
"github.com/spf13/cobra"
)

// RemoveCmd represents the remove command
var RemoveCmd = &cobra.Command{
Use: "remove",
Short: "Remove the history data",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
err := budget_db.RemoveHistory(category)
if err != nil {
log.Fatal(err)
}
},
}

func init() {
RemoveCmd.Flags().StringP("category", "c", "", "Write the category to remove it's history")
}
25 changes: 25 additions & 0 deletions cmd/spend/sub_handler/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spend_subhandler

import (
"log"

"github.com/ibilalkayy/flow/db/budget_db"
"github.com/spf13/cobra"
)

// ShowCmd represents the show command
var ShowCmd = &cobra.Command{
Use: "show",
Short: "Show the history data",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
err := budget_db.ViewHistory(category)
if err != nil {
log.Fatal(err)
}
},
}

func init() {
ShowCmd.Flags().StringP("category", "c", "", "Write the category to show it's history")
}
22 changes: 6 additions & 16 deletions cmd/total_amount/handler/remove.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package total_amount_handler

import (
"fmt"
"log"

"github.com/ibilalkayy/flow/db/total_amount_db"
"github.com/spf13/cobra"
)

func removeData() error {
err := total_amount_db.RemoveTotalAmount("TotalAmount")
if err != nil {
return err
}

err = total_amount_db.RemoveTotalAmount("TotalAmountCategory")
if err != nil {
return err
}
return err
}

// RemoveCmd represents the remove command
var RemoveCmd = &cobra.Command{
Use: "remove",
Short: "Remove the total amount data",
Run: func(cmd *cobra.Command, args []string) {
err := removeData()
category, _ := cmd.Flags().GetString("category")
err := total_amount_db.RemoveTotalAmount(category)
if err != nil {
log.Fatal(err)
}
fmt.Println("Total amount is successfully removed!")
},
}

func init() {
RemoveCmd.Flags().StringP("category", "c", "", "Write the category to remove it's date")
}
9 changes: 6 additions & 3 deletions cmd/total_amount/handler/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ var UpdateCmd = &cobra.Command{
Use: "update",
Short: "Update the total amount data",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
old_category, _ := cmd.Flags().GetString("oldcategory")
new_category, _ := cmd.Flags().GetString("newcategory")
amount, _ := cmd.Flags().GetString("amount")
label, _ := cmd.Flags().GetString("label")
totalAmount := functions.StringToInt(amount)

tv := structs.TotalAmountVariables{
Included: category,
Included: old_category,
NewCategory: new_category,
TotalAmount: totalAmount,
Label: label,
}
Expand All @@ -32,7 +34,8 @@ var UpdateCmd = &cobra.Command{
}

func init() {
UpdateCmd.Flags().StringP("category", "c", "", "Write the category that you want to update")
UpdateCmd.Flags().StringP("oldcategory", "o", "", "Write the old category that you want to update")
UpdateCmd.Flags().StringP("newcategory", "n", "", "Write the new category to update with")
UpdateCmd.Flags().StringP("amount", "a", "", "Write the total amount that you want to update")
UpdateCmd.Flags().StringP("label", "l", "", "Write the label that you want to update")
}
35 changes: 35 additions & 0 deletions db/budget_db/history_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,38 @@ func ViewHistory(category string) error {
fmt.Println(tableRender)
return nil
}

func RemoveHistory(category string) error {
db, err := db.Connection()
if err != nil {
return err
}
defer db.Close()

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

if len(category) != 0 {
query += " WHERE categories=$1"
args = append(args, category)
}

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.Printf("History is successfully deleted!")
}

return nil
}
55 changes: 41 additions & 14 deletions db/total_amount_db/total_amount_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,51 @@ func ViewTotalAmount() ([5]interface{}, error) {
return details, nil
}

func RemoveTotalAmount(table string) error {
func RemoveTotalAmount(category string) error {
db, err := db.Connection()
if err != nil {
return err
}
defer db.Close()

var query string
var args []interface{}

if len(category) != 0 {
query = "DELETE FROM TotalAmountCategory WHERE included_categories=$1"
args = append(args, category)
fmt.Printf("'%s' category is ", category)
} else {
query = "DELETE FROM TotalAmountCategory"
fmt.Print("Total amount data is ")
}

query := "DELETE FROM " + table
remove, err := db.Prepare(query)
removeCategory, err := db.Prepare(query)
if err != nil {
return err
}
defer removeCategory.Close()

defer remove.Close()

_, err = remove.Exec()
_, err = removeCategory.Exec(args...)
if err != nil {
return nil
return err
}
fmt.Println("successfully removed!")

if len(category) == 0 {
query = "DELETE FROM TotalAmount"
removeTotal, err := db.Prepare(query)
if err != nil {
return err
}
defer removeTotal.Close()

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

return nil
}

Expand All @@ -102,14 +129,14 @@ func UpdateTotalAmount(tv *structs.TotalAmountVariables) error {
if tv.TotalAmount != 0 {
query = "UPDATE TotalAmount SET total_amount=$1"
params = []interface{}{tv.TotalAmount}
} else if len(tv.Included) != 0 {
query = "UPDATE TotalAmountCategory SET included_categories=$1"
params = []interface{}{tv.Included}
} else if len(tv.Label) != 0 {
query = "UPDATE TotalAmountCategory SET labels=$1"
params = []interface{}{tv.Label}
} else if len(tv.Included) != 0 && len(tv.NewCategory) != 0 {
query = "UPDATE TotalAmountCategory SET included_categories=$1 WHERE included_categories=$2"
params = []interface{}{tv.NewCategory, tv.Included}
} else if len(tv.Label) != 0 && len(tv.Included) != 0 {
query = "UPDATE TotalAmountCategory SET labels=$1 WHERE included_categories=$2"
params = []interface{}{tv.Label, tv.Included}
} else {
return errors.New("no flag is provided to update")
return errors.New("write the present category also to update the values")
}

_, err = db.Exec(query, params...)
Expand Down
1 change: 1 addition & 0 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type TotalAmountVariables struct {
SpentAmount int
RemainingAmount int
Included string
NewCategory string
Label string
Status string
}

0 comments on commit 8ebd738

Please sign in to comment.