-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worked on the functionality to manage the total amount in the database
- Loading branch information
1 parent
e6fb38a
commit c4c12f8
Showing
10 changed files
with
185 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters