Skip to content

Commit

Permalink
added the adjust, view and remove budget funcationality
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Feb 25, 2024
1 parent 2144ca9 commit 351cf94
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
10 changes: 6 additions & 4 deletions cmd/budget/adjust.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package budget

import (
"fmt"
"log"

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

Expand All @@ -14,9 +15,10 @@ var adjustCmd = &cobra.Command{
oldCategory, _ := cmd.Flags().GetString("oldcategory")
newCategory, _ := cmd.Flags().GetString("newcategory")
amount, _ := cmd.Flags().GetString("amount")
fmt.Println(oldCategory)
fmt.Println(newCategory)
fmt.Println(amount)
err := app.UpdateBudget(oldCategory, newCategory, amount)
if err != nil {
log.Fatal(err)
}
},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/budget/remove.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package budget

import (
"fmt"
"log"

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

Expand All @@ -12,7 +13,10 @@ var removeCmd = &cobra.Command{
Short: "Remove the budget details",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
fmt.Println(category)
err := app.RemoveBudget(category)
if err != nil {
log.Fatal(err)
}
},
}

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

import (
"fmt"
"log"

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

Expand All @@ -12,7 +14,11 @@ var viewCmd = &cobra.Command{
Short: "View the budget details",
Run: func(cmd *cobra.Command, args []string) {
category, _ := cmd.Flags().GetString("category")
fmt.Println(category)
details, err := app.ViewBudget(category)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Budget Details:\n\tCategory: %s\n\tAmount: %s", details[0], details[1])
},
}

Expand Down
4 changes: 2 additions & 2 deletions db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Variables struct {
DBName string
}

func connection() (*sql.DB, error) {
func Connection() (*sql.DB, error) {
v := Variables{
Host: middleware.LoadEnvVariable("host"),
Port: middleware.LoadEnvVariable("port"),
Expand All @@ -41,7 +41,7 @@ func connection() (*sql.DB, error) {
}

func Table(table_name, filename string, number int) (*sql.DB, error) {
db, err := connection()
db, err := Connection()
if err != nil {
return nil, err
}
Expand Down
69 changes: 69 additions & 0 deletions internal/app/budget.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"

"github.com/ibilalkayy/flow/db"
Expand Down Expand Up @@ -32,3 +33,71 @@ func CreateBudget(bv *BudgetVariables) error {
fmt.Println("Budget data is successfully inserted!")
return nil
}

func ViewBudget(category string) ([2]string, error) {
bv := new(BudgetVariables)
db, err := db.Connection()
if err != nil {
return [2]string{}, err
}

query := "SELECT categories, amounts FROM Budget WHERE categories=$1"
if err := db.QueryRow(query, category).Scan(&bv.Category, &bv.Amount); err != nil {
return [2]string{}, err
}
return [2]string{bv.Category, bv.Amount}, err
}

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

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

defer remove.Close()

if len(category) != 0 {
_, err = remove.Exec(category)
if err != nil {
return err
}
fmt.Printf("%s category is successfully removed!", category)
} else {
fmt.Println("First enter the category and then remove it")
}
return nil
}

func UpdateBudget(old, new, amount string) error {
db, err := db.Connection()
if err != nil {
return err
}

// Check if the old category exists
var count int
err = db.QueryRow("SELECT COUNT(*) FROM Budget WHERE categories = $1", old).Scan(&count)
if err != nil {
return err
}

// If the old category does not exist, return an error
if count == 0 {
return errors.New("'" + old + "'" + " category does not exist")
}

// If the old category exists, execute the update query
query := "UPDATE Budget SET categories=$1, amounts=$2 WHERE categories=$3"
_, err = db.Exec(query, new, amount, old)
if err != nil {
return err
}
fmt.Println("Your budget category is successfully updated!")
return nil
}

0 comments on commit 351cf94

Please sign in to comment.