Skip to content

Commit

Permalink
worked on the remaining total amount functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Apr 15, 2024
1 parent bcf3198 commit 26a0b2c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 33 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.74"
const version = "v0.1.75"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
12 changes: 5 additions & 7 deletions cmd/total_amount/handler/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ var SetCmd = &cobra.Command{
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")
totalAmount := functions.StringToInt(amount)

tv := structs.TotalAmountVariables{
Amount: totalAmount,
Included: include_category,
Excluded: exclude_category,
Label: label,
Status: "inactive",
TotalAmount: totalAmount,
RemainingAmount: 0,
Included: include_category,
Label: label,
Status: "inactive",
}

err := total_amount_db.SetTotalAmount(&tv, "db/migrations/")
Expand All @@ -38,6 +37,5 @@ var SetCmd = &cobra.Command{
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 from the total amount")
SetCmd.Flags().StringP("label", "l", "", "Provide a label for setting up your total amount")
}
4 changes: 2 additions & 2 deletions cmd/total_amount/handler/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var UpdateCmd = &cobra.Command{
totalAmount := functions.StringToInt(amount)

tv := structs.TotalAmountVariables{
Amount: totalAmount,
Label: label,
TotalAmount: totalAmount,
Label: label,
}
err := total_amount_db.UpdateTotalAmount(&tv)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions db/migrations/003_create_total_amount_table.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CREATE TABLE IF NOT EXISTS TotalAmount (
id BIGSERIAL PRIMARY KEY,
amount INT NOT NULL,
total_amount INT NOT NULL,
remaining_amount INT NOT NULL,
included_category VARCHAR(255) NOT NULL,
excluded_category VARCHAR(255) NOT NULL,
label VARCHAR(255) NOT NULL,
statuss VARCHAR(255) NOT NULL
);
70 changes: 55 additions & 15 deletions db/total_amount_db/total_amount_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ func SetTotalAmount(tv *structs.TotalAmountVariables, basepath string) error {
return err
}

query := "INSERT INTO TotalAmount(amount, included_category, excluded_category, label, statuss) VALUES($1, $2, $3, $4, $5)"
query := "INSERT INTO TotalAmount(total_amount, remaining_amount, included_category, label, statuss) VALUES($1, $2, $3, $4, $5)"
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, tv.Status)
if tv.TotalAmount != 0 && len(tv.Included) != 0 {
_, err = insert.Exec(tv.TotalAmount, tv.RemainingAmount, tv.Included, tv.Label, tv.Status)
if err != nil {
return err
}
fmt.Println("Total amount data is successfully inserted!")
} else {
return errors.New("total amount can't be empty")
return errors.New("write total amount and category. see 'flow total-amount set -h'")
}
return nil
}
Expand All @@ -46,10 +46,10 @@ func ViewTotalAmount() ([4]interface{}, error) {
defer db.Close()

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

var rows *sql.Rows
query := "SELECT amount, included_category, excluded_category, label, statuss FROM TotalAmount"
query := "SELECT total_amount, remaining_amount, included_category, label, statuss FROM TotalAmount"
rows, err = db.Query(query)
if err != nil {
return [4]interface{}{}, err
Expand All @@ -58,14 +58,14 @@ func ViewTotalAmount() ([4]interface{}, error) {
defer rows.Close()

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

Expand Down Expand Up @@ -100,12 +100,12 @@ func UpdateTotalAmount(tv *structs.TotalAmountVariables) error {
if err != nil {
return err
}
if tv.Amount != 0 && len(tv.Label) != 0 {
query = "UPDATE TotalAmount SET amount=$1, label=$2"
params = []interface{}{tv.Amount, tv.Label}
} else if tv.Amount != 0 {
query = "UPDATE TotalAmount SET amount=$1"
params = []interface{}{tv.Amount}
if tv.TotalAmount != 0 && len(tv.Label) != 0 {
query = "UPDATE TotalAmount SET total_amount=$1, label=$2"
params = []interface{}{tv.TotalAmount, tv.Label}
} else if tv.TotalAmount != 0 {
query = "UPDATE TotalAmount SET total_amount=$1"
params = []interface{}{tv.TotalAmount}
} else if len(tv.Label) != 0 {
query = "UPDATE TotalAmount SET label=$1"
params = []interface{}{tv.Label}
Expand Down Expand Up @@ -144,3 +144,43 @@ func UpdateStatus(tv *structs.TotalAmountVariables) error {
}
return nil
}

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

// Find the total amount data
var totalAmount int
if len(category) != 0 {
query := "SELECT total_amount FROM TotalAmount WHERE included_category=$1"
err := db.QueryRow(query, category).Scan(&totalAmount)
if err != nil {
return err
}
} else {
return errors.New("category is not present")
}

// Find the budget amount data
var savedSpent int
if len(category) != 0 {
query := "SELECT spent FROM Budget WHERE categories = $1"
err := db.QueryRow(query, category).Scan(&savedSpent)
if err != nil {
return err
}
} else {
return errors.New("category is not present")
}

remainingBalance := totalAmount - savedSpent
query := "UPDATE TotalAmount SET remaining_amount=$1 WHERE included_category=$2"
_, err = db.Exec(query, remainingBalance, category)
if err != nil {
return err
}
return nil
}
16 changes: 15 additions & 1 deletion internal/app/spend/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ func SpendMoney(category string, spending_amount int) error {
if err != nil {
return err
}
err = total_amount_db.CalculateRemaining(category)
if err != nil {
return err
}
fmt.Println("Enjoy your spending!")
} else if spending_amount <= remainingAmount {
err := budget_db.AddExpenditure(spending_amount, category)
if err != nil {
return err
}
err = total_amount_db.CalculateRemaining(category)
if err != nil {
return err
}
fmt.Println("Enjoy your spending!")
} else if spending_amount > remainingAmount && spending_amount <= totalAllocatedAmount && spentAmount <= totalAllocatedAmount && totalSpent <= totalAllocatedAmount {
fmt.Printf("Your set budget is %d. You have %d remaining but you spent %d.\n", totalAmount, remainingAmount, spentAmount)
fmt.Printf("Do you still want to spend? [yes/no]: ")
Expand All @@ -58,14 +68,18 @@ func SpendMoney(category string, spending_amount int) error {
if err != nil {
return err
}
err = total_amount_db.CalculateRemaining(category)
if err != nil {
return err
}
fmt.Println("Enjoy your spending!")
case "no", "n":
fmt.Println("Alright")
default:
return errors.New("select the right option")
}
} else {
return errors.New("you have exceeded total amount logic")
return errors.New("you have exceeded the total amount")
}
} else {
return errors.New("category is not found. setup the alert 'flow budget alert setup -h' or include the category in your total amount by writing 'flow total-amount set -h'")
Expand Down
10 changes: 5 additions & 5 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type EmailVariables struct {
}

type TotalAmountVariables struct {
Amount int
Included string
Excluded string
Label string
Status string
TotalAmount int
RemainingAmount int
Included string
Label string
Status string
}

0 comments on commit 26a0b2c

Please sign in to comment.