Compare commits

..

No commits in common. "d5fc3d8d23b48c75379216b0f087fc8666c97c5a" and "bf31a1c7a81526a0696c3daac4f4c20c37c600a9" have entirely different histories.

4 changed files with 11 additions and 98 deletions

3
.gitignore vendored
View file

@ -1,6 +1,3 @@
# --> custom test files
test.db
# --> nix # --> nix
.direnv/ .direnv/
.envrc .envrc

8
db.go
View file

@ -23,8 +23,8 @@ func db_get_transactions(transactions *[]Transaction,r *GetTransactionPagination
"SELECT trns_id, trns_amount, trns_description, " + "SELECT trns_id, trns_amount, trns_description, " +
"trns_account, trns_bucket, trns_date " + "trns_account, trns_bucket, trns_date " +
fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA) + fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA) +
fmt.Sprintf("LIMIT %d OFFSET %d", fmt.Sprintf("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
r.ResultCount, r.PageNum * r.ResultCount )) r.PageNum, r.ResultCount))
if err != nil { if err != nil {
return err return err
} }
@ -42,9 +42,7 @@ func db_new_transaction(transaction Transaction) (error) {
defer db.Close() defer db.Close()
log.Debug().Msgf("%#v", transaction) _, err = db.NamedQuery(
_, err = db.NamedExec(
fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA) + fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA) +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" + "(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)", "VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",

20
main.go
View file

@ -8,7 +8,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"time" "time"
//"github.com/shopspring/decimal" //"github.com/shopspring/decimal"
@ -50,20 +50,11 @@ func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel) zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
var debugFlag = flag.Bool("d", false, "whether to enable debug mode") var nFlag = flag.Bool("d", false, "whether to enable debug mode")
var traceFlag = flag.Bool("t", false, "whether to trace logging")
flag.Parse() flag.Parse()
if *traceFlag { if *nFlag {
zerolog.SetGlobalLevel(zerolog.TraceLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Enabling trace level debugging")
}
if *debugFlag {
if !*traceFlag {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Debug().Msg("Is debugging") log.Debug().Msg("Is debugging")
DB_TYPE = "sqlite3" DB_TYPE = "sqlite3"
DB_SCHEMA = "" DB_SCHEMA = ""
@ -71,9 +62,6 @@ func main() {
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING) debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
} }
debug_mode.SetLogLevel(zerolog.GlobalLevel())
log.Info().Msg("starting server") log.Info().Msg("starting server")
r := chi.NewRouter() r := chi.NewRouter()

View file

@ -1,52 +1,13 @@
package debug_mode package debug_mode
import ( import (
"os" _ "github.com/mattn/go-sqlite3"
"encoding/json" "github.com/jmoiron/sqlx"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
type Transaction struct {
Id int `db:"trns_id" json:"Id"`
Amount string `db:"trns_amount" json:"Amount"`
Description sql.NullString `db:"trns_description" json:"Description"`
Account int `db:"trns_account" json:"Account"`
Bucket sql.NullInt64 `db:"trns_bucket" json:"Bucket"`
Date time.Time `db:"trns_date" json:"TransactionDate"`
}
func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) { func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("Could not get current working directory")
} else {
log.Trace().Msgf("Currect working directory is: %s", cwd)
}
_, err = os.Stat(cwd + DB_CONNECTION_STRING)
if err != nil {
log.Debug().Msg("Found existing test.db file. Attempting to delete")
err = os.Remove(DB_CONNECTION_STRING)
if err != nil {
log.Fatal().Err(err).Msg("Failed to delete testing db")
} else {
log.Debug().Msg("Deleted test.db file successfully")
}
} else {
log.Debug().Msg("No existing test.db file found")
}
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil { if err != nil {
log.Fatal(). log.Fatal().
@ -57,6 +18,7 @@ func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
defer db.Close() defer db.Close()
init_sql := ` init_sql := `
CREATE TABLE accounts ( CREATE TABLE accounts (
acnt_id Integer PRIMARY KEY, acnt_id Integer PRIMARY KEY,
acnt_dsply_name varchar(50) NOT NULL, acnt_dsply_name varchar(50) NOT NULL,
@ -109,11 +71,6 @@ CREATE TABLE transaction_categories (
trns_ctgry_description varchar(250) NULL trns_ctgry_description varchar(250) NULL
); );
INSERT INTO accounts (acnt_dsply_name, acnt_description) VALUES ("BECU Saving", "Savings Account");
INSERT INTO buckets (bkt_dsply_code, bkt_dsply_name, bkt_description) VALUES
("SVNGS", "Savings", "Savings Bucket");
INSERT INTO transactions (trns_amount, trns_description, trns_account, trns_bucket, trns_date) VALUES
("50.00", "Money", 1, 1, "2023-11-10");
` `
tx := db.MustBegin() tx := db.MustBegin()
@ -127,33 +84,6 @@ INSERT INTO transactions (trns_amount, trns_description, trns_account, trns_buck
Msg("Could not commit transaction") Msg("Could not commit transaction")
} }
jsonExample := `{
"Id": 3,
"Amount": "100",
"Description": {
"String": "Transaction 3",
"Valid": true
},
"Account": 1,
"Bucket": {
"Int64": 1,
"Valid": true
},
"TransactionDate": "2023-11-11T00:00:00Z"
}`
var trns Transaction = Transaction{}
err = json.Unmarshal([]byte(jsonExample), &trns)
if err != nil {
log.Fatal().Err(err).Msg("could not unmarshal")
}
_, err = db.NamedExec("INSERT INTO transactions" +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
trns)
log.Debug().Msg("Test database initialized") log.Debug().Msg("Test database initialized")