Skip to content

Commit

Permalink
worked on the functionality to manage the total amount in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Apr 8, 2024
1 parent e6fb38a commit c4c12f8
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/budget/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ and adjust their budgets to effectively track and control their expenses.`,

func init() {
cmd.RootCmd.AddCommand(budgetCmd)
// Added subcommands
// Subcommands
budgetCmd.AddCommand(budget_handler.CreateCmd)
budgetCmd.AddCommand(budget_handler.ViewCmd)
budgetCmd.AddCommand(budget_handler.AdjustCmd)
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.65"
const version = "v0.1.66"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
10 changes: 7 additions & 3 deletions cmd/total_amount/handler/remove.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package total_amount_handler

import (
"fmt"
"log"

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

// RemoveCmd represents the remove command
var RemoveCmd = &cobra.Command{
Use: "remove",
Short: "A brief description of your command",
Short: "Remove the total amount data",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("remove called")
err := total_amount_db.RemoveTotalAmount()
if err != nil {
log.Fatal(err)
}
},
}

Expand Down
26 changes: 19 additions & 7 deletions cmd/total_amount/handler/set.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package total_amount_handler

import (
"fmt"
"log"

"github.com/ibilalkayy/flow/db/total_amount_db"
"github.com/ibilalkayy/flow/internal/common/functions"
"github.com/ibilalkayy/flow/internal/common/structs"
"github.com/spf13/cobra"
)

// SetCmd represents the set command
var SetCmd = &cobra.Command{
Use: "set",
Short: "Set the total amount",
Short: "Set the total amount data",
Run: func(cmd *cobra.Command, args []string) {
amount, _ := cmd.Flags().GetString("amount")
include_category, _ := cmd.Flags().GetString("include")
exclude_category, _ := cmd.Flags().GetString("exclude")
label, _ := cmd.Flags().GetString("label")
fmt.Println(amount)
fmt.Println(include_category)
fmt.Println(exclude_category)
fmt.Println(label)
totalAmount := functions.StringToInt(amount)

tv := structs.TotalAmountVariables{
Amount: totalAmount,
Included: include_category,
Excluded: exclude_category,
Label: label,
}

err := total_amount_db.SetTotalAmount(&tv, "db/migrations/")
if err != nil {
log.Fatal(err)
}
},
}

func init() {
SetCmd.Flags().StringP("amount", "a", "", "Write the total amount that you want to set")
SetCmd.Flags().StringP("include", "i", "", "Specify a category to include in the total amount")
SetCmd.Flags().StringP("exclude", "e", "", "Specify a category to exclude in the total amount")
SetCmd.Flags().StringP("exclude", "e", "", "Specify a category to exclude from the total amount")
SetCmd.Flags().StringP("label", "l", "", "Provide a label for setting up your total amount")
}
19 changes: 15 additions & 4 deletions cmd/total_amount/handler/update.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package total_amount_handler

import (
"fmt"
"log"

"github.com/ibilalkayy/flow/db/total_amount_db"
"github.com/ibilalkayy/flow/internal/common/functions"
"github.com/ibilalkayy/flow/internal/common/structs"
"github.com/spf13/cobra"
)

// UpdateCmd represents the update command
var UpdateCmd = &cobra.Command{
Use: "update",
Short: "A brief description of your command",
Short: "Update the total amount data",
Run: func(cmd *cobra.Command, args []string) {
amount, _ := cmd.Flags().GetString("amount")
label, _ := cmd.Flags().GetString("label")
fmt.Println(amount)
fmt.Println(label)
totalAmount := functions.StringToInt(amount)

tv := structs.TotalAmountVariables{
Amount: totalAmount,
Label: label,
}
err := total_amount_db.UpdateTotalAmount(&tv)
if err != nil {
log.Fatal(err)
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/total_amount/handler/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package total_amount_handler

import (
"fmt"
"log"

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

Expand All @@ -11,7 +13,11 @@ var ViewCmd = &cobra.Command{
Use: "view",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("view called")
table, err := total_amount_db.ViewTotalAmount()
if err != nil {
log.Fatal(err)
}
fmt.Println(table)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/total_amount/total_amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var TotalAmountCmd = &cobra.Command{
Use: "total-amount",
Short: "Manage your total amount",
Long: `total-amount allows you to manage your total amount by setting, view, removing
Long: `This command allows you to manage your total amount by setting, view, removing
doing category selection, excluding categories, etc`,
Run: func(cmd *cobra.Command, args []string) {
active, _ := cmd.Flags().GetString("active")
Expand All @@ -24,7 +24,7 @@ doing category selection, excluding categories, etc`,

func init() {
cmd.RootCmd.AddCommand(TotalAmountCmd)
// Added Subcommands
// Subcommands
TotalAmountCmd.AddCommand(total_amount_handler.SetCmd)
TotalAmountCmd.AddCommand(total_amount_handler.UpdateCmd)
TotalAmountCmd.AddCommand(total_amount_handler.ViewCmd)
Expand Down
7 changes: 7 additions & 0 deletions db/migrations/003_create_total_amount_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS TotalAmount (
id BIGSERIAL PRIMARY KEY,
amount INT NOT NULL,
included_category VARCHAR(255) NOT NULL,
excluded_category VARCHAR(255) NOT NULL,
label VARCHAR(255) NOT NULL
);
119 changes: 119 additions & 0 deletions db/total_amount_db/total_amount_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package total_amount_db

import (
"database/sql"
"errors"
"fmt"

"github.com/ibilalkayy/flow/db"
"github.com/ibilalkayy/flow/internal/common/structs"
"github.com/jedib0t/go-pretty/v6/table"
)

func SetTotalAmount(tv *structs.TotalAmountVariables, basepath string) error {
data, err := db.Table(basepath, "003_create_total_amount_table.sql", 0)
if err != nil {
return err
}

query := "INSERT INTO TotalAmount(amount, included_category, excluded_category, label) VALUES($1, $2, $3, $4)"
insert, err := data.Prepare(query)
if err != nil {
return err
}

defer insert.Close()

if tv.Amount != 0 {
_, err = insert.Exec(tv.Amount, tv.Included, tv.Excluded, tv.Label)
if err != nil {
return err
}
fmt.Println("Total amount data is successfully inserted!")
} else {
return errors.New("total amount can't be empty")
}
return nil
}

func ViewTotalAmount() (string, error) {
tv := new(structs.TotalAmountVariables)

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

tw := table.NewWriter()
tw.AppendHeader(table.Row{"Total Amount", "Included Category", "Excluded Category", "Label"})

var rows *sql.Rows
query := "SELECT amount, included_category, excluded_category, label FROM TotalAmount"
rows, err = db.Query(query)
if err != nil {
return "", err
}

defer rows.Close()

for rows.Next() {
if err := rows.Scan(&tv.Amount, &tv.Included, &tv.Excluded, &tv.Label); err != nil {
return "", nil
}
}
// Append data to the table inside the loop
tw.AppendRow([]interface{}{tv.Amount, tv.Included, tv.Excluded, tv.Label})
tableRender := "Total Amount\n" + tw.Render()
return tableRender, nil
}

func RemoveTotalAmount() error {
db, err := db.Connection()
if err != nil {
return err
}

query := "DELETE FROM TotalAmount"
remove, err := db.Prepare(query)
if err != nil {
return err
}

defer remove.Close()

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

fmt.Println("Tottal amount is successfully removed!")
return nil
}

func UpdateTotalAmount(tv *structs.TotalAmountVariables) error {
var query string
var params []interface{}

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

if tv.Amount != 0 {
query = "UPDATE TotalAmount SET amount=$1"
params = []interface{}{tv.Amount}
} else if len(tv.Label) != 0 {
query = "UPDATE TotalAmount SET label=$1"
params = []interface{}{tv.Label}
} else {
return errors.New("no flag is provided to update")
}

_, err = db.Exec(query, params...)
if err != nil {
return err
}
fmt.Println("Total amount data is successfully updated!")
return nil
}
7 changes: 7 additions & 0 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ type EmailVariables struct {
Category string
CategoryAmount int
}

type TotalAmountVariables struct {
Amount int
Included string
Excluded string
Label string
}

0 comments on commit c4c12f8

Please sign in to comment.