Skip to content

Commit

Permalink
worked on the view history functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Apr 23, 2024
1 parent b6ec451 commit 6a2a978
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 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.86"
const version = "v0.1.87"

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

import (
"fmt"
"log"

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

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

Expand Down
49 changes: 47 additions & 2 deletions db/budget_db/history_db.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package budget_db

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

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

func InsertHistory(hv *structs.HistoryVariables, basePath string) error {
Expand All @@ -14,7 +17,7 @@ func InsertHistory(hv *structs.HistoryVariables, basePath string) error {
return err
}

query := "INSERT INTO History(dates, categories, amounts, transaction_ids, blockchains, addresses) VALUES($1, $2, $3, $4, $5, $6)"
query := "INSERT INTO History(dates, timez, categories, amounts, transaction_ids, blockchains, addresses) VALUES($1, $2, $3, $4, $5, $6, $7)"
insert, err := data.Prepare(query)
if err != nil {
return err
Expand All @@ -33,7 +36,7 @@ func InsertHistory(hv *structs.HistoryVariables, basePath string) error {

if len(hv.Category) != 0 && len(includedCategory) != 0 {
if hv.Amount != 0 && totalAmount != 0 {
_, err = insert.Exec(hv.Date, hv.Category, hv.Amount, hv.TransactionID, hv.Blockchain, hv.Address)
_, err = insert.Exec(hv.Date, hv.Time, hv.Category, hv.Amount, hv.TransactionID, hv.Blockchain, hv.Address)
if err != nil {
return err
}
Expand All @@ -45,3 +48,45 @@ func InsertHistory(hv *structs.HistoryVariables, basePath string) error {
}
return nil
}

func ViewHistory(category string) error {
hv := new(structs.HistoryVariables)

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

defer db.Close()

tw := table.NewWriter()
tw.AppendHeader(table.Row{"Date", "Time", "Category", "Amounts", "Transaction IDs", "Blockchains", "Addresses"})

var rows *sql.Rows
if len(category) != 0 {
query := "SELECT dates, timez, categories, amounts, transaction_ids, blockchains, addresses from History WHERE categories=$1"
rows, err = db.Query(query, category)
} else {
query := "SELECT dates, timez, categories, amounts, transaction_ids, blockchains, addresses from History"
rows, err = db.Query(query)
}
if err != nil {
return 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
}

if len(hv.Category) != 0 && hv.Amount != 0 {
tw.AppendRow([]interface{}{hv.Date, hv.Time, hv.Category, hv.Amount, hv.TransactionID, hv.Blockchain, hv.Address})
}
}

tableRender := "History Data\n" + tw.Render()
fmt.Println(tableRender)
return nil
}
1 change: 1 addition & 0 deletions db/migrations/001_create_budget_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS Budget (
CREATE TABLE IF NOT EXISTS History (
id BIGSERIAL PRIMARY KEY,
dates VARCHAR(255) NOT NULL,
timez VARCHAR(255) NOT NULL,
categories VARCHAR(255) NOT NULL,
amounts INT NOT NULL,
transaction_ids VARCHAR(255) NOT NULL,
Expand Down
2 changes: 2 additions & 0 deletions internal/app/spend/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (

func StoreHistory(category string, spending_amount int) error {
currentDate := time.Now().Format("2006-01-02")
currentTime := time.Now().Format("03:04:05 PM")

hv := structs.HistoryVariables{
Date: currentDate,
Time: currentTime,
Category: category,
Amount: spending_amount,
TransactionID: "transaction id",
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 @@ -24,6 +24,7 @@ type BudgetVariables struct {

type HistoryVariables struct {
Date string
Time string
Category string
Amount int
TransactionID string
Expand Down

0 comments on commit 6a2a978

Please sign in to comment.