This commit is contained in:
Nickiel12 2023-12-31 17:21:09 -08:00
parent c16ffe3a2e
commit 0ad04869b0
7 changed files with 162 additions and 18 deletions

24
api.go
View file

@ -8,8 +8,6 @@ import (
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
"github.com/go-chi/chi/v5"
"github.com/ggicci/httpin"
)
@ -32,11 +30,6 @@ func apiRouter() http.Handler {
}
func getTransactions(w http.ResponseWriter, req *http.Request) {
db, err := sqlx.Connect("postgres", DB_CONNECTION_STRING)
if err != nil {
log.Fatal(err)
}
defer db.Close()
input := req.Context().Value(httpin.Input).(*GetTransactionPaginationInput)
@ -46,12 +39,12 @@ func getTransactions(w http.ResponseWriter, req *http.Request) {
transactions := []Transaction{}
err = db.Select(&transactions,
"SELECT trns_id, trns_amount, trns_description, " +
"trns_account, trns_bucket, trns_date " +
"FROM rcnt.transactions ORDER BY trns_id DESC " +
fmt.Sprintf("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
input.PageNum, input.ResultCount))
err := db_get_transactions(&transactions, input)
if err != nil {
log.Print("Fatal error in getTransactions from db_get_transactions")
log.Fatal(err)
}
for _, trns := range transactions {
//bytes, err := json.Marshal(trns)
@ -70,6 +63,7 @@ func newTransaction(w http.ResponseWriter, req *http.Request) {
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, "New transaction created for Account: %d, with an Amount of: %s",
t.Account, t.Amount)
//fmt.Fprintf(w, "New transaction created for Account: %d, with an Amount of: %s",
// t.Account, t.Amount)
db_new_transaction(t)
}

51
db.go Normal file
View file

@ -0,0 +1,51 @@
package main
import (
"fmt"
"log"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/jmoiron/sqlx"
)
func db_get_transactions(transactions *[]Transaction,r *GetTransactionPaginationInput) (error) {
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
log.Print("Fatal error in db_get_transactions\nCannot connect to server")
log.Fatal(err)
}
defer db.Close()
err = db.Select(transactions,
"SELECT trns_id, trns_amount, trns_description, " +
"trns_account, trns_bucket, trns_date " +
fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA) +
fmt.Sprintf("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY",
r.PageNum, r.ResultCount))
if err != nil {
return err
}
return nil
}
func db_new_transaction(transaction Transaction) (error) {
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
log.Print("Fatal error in db_get_transactions\nCannot connect to server")
log.Fatal(err)
}
defer db.Close()
_, err = db.NamedQuery(
fmt.Sprintf("INSERT INTO %stransactions", DB_SCHEMA) +
"(trns_amount, trns_description, trns_account, trns_bucket, trns_date)" +
"VALUES (:trns_amount, :trns_description, :trns_account, :trns_bucket, :trns_date)",
transaction)
if err != nil {
log.Fatal(err)
}
return nil
}

2
go.mod
View file

@ -7,7 +7,7 @@ require (
github.com/go-chi/chi/v5 v5.0.11
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/shopspring/decimal v1.3.1
github.com/mattn/go-sqlite3 v1.14.6
)
require github.com/ggicci/owl v0.4.0 // indirect

2
go.sum
View file

@ -21,8 +21,6 @@ github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRU
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

10
main.go
View file

@ -1,6 +1,8 @@
package main
import (
"nickiel.net/recount_server/tests"
"database/sql"
"net/http"
@ -14,6 +16,8 @@ import (
"github.com/go-chi/chi/v5/middleware"
)
var DB_TYPE = "postgres"
var DB_SCHEMA = "rcnt."
var DB_CONNECTION_STRING string = "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable"
// "json:"json_code_name,omitempty"" (omit empty)
@ -48,8 +52,14 @@ func main() {
if *nFlag {
log.Println("Is debugging")
DB_TYPE = "sqlite3"
DB_SCHEMA = ""
DB_CONNECTION_STRING = "test.db"
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
}
log.Print("starting server")
r := chi.NewRouter()
// A good base middleware stack

91
tests/testdb.go Normal file
View file

@ -0,0 +1,91 @@
package debug_mode
import (
"log"
_ "github.com/mattn/go-sqlite3"
"github.com/jmoiron/sqlx"
)
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
log.Print("couldn't open test db")
log.Fatal(err)
}
defer db.Close()
init_sql := `
CREATE TABLE accounts (
acnt_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
acnt_dsply_name varchar(50) NOT NULL,
acnt_description varchar(250) NULL,
CONSTRAINT accounts_pkey PRIMARY KEY (acnt_id)
);
CREATE TABLE buckets (
bkt_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
bkt_dsply_code varchar(5) NOT NULL,
bkt_dsply_name varchar(50) NULL,
bkt_description varchar(250) NULL,
CONSTRAINT buckets_pkey PRIMARY KEY (bkt_id)
);
CREATE TABLE transaction_breakdown (
trns_brkdwn_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
trns_brkdwn_amount money NOT NULL,
trns_brkdwn_parent_transaction int4 NOT NULL,
trns_brkdwn_catagory int4 NULL,
trns_brkdwn_bucket int4 NULL,
CONSTRAINT transaction_breakdown_pkey PRIMARY KEY (trns_brkdwn_id)
);
alter table transaction_breakdown add constraint transaction_breakdown_parent_transaction_fkey foreign key (trns_brkdwn_parent_transaction) references transactions(trns_id);
alter table transaction_breakdown add constraint transaction_breakdown_catagory_fkey foreign key (trns_brkdwn_catagory) references transaction_categories(trns_ctgry_id);
alter table transaction_breakdown add constraint transaction_breakdown_bucket_fkey foreign key (trns_brkdwn_bucket) references buckets(bkt_id);
CREATE TABLE transactions (
trns_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
trns_amount money NOT NULL,
trns_description varchar(250) NULL,
trns_account int4 NOT NULL,
trns_bucket int4 NULL,
trns_date Date not null,
CONSTRAINT transactions_pkey PRIMARY KEY (trns_id)
);
ALTER TABLE transactions ADD CONSTRAINT transactions_trns_account_fkey FOREIGN KEY (trns_account) REFERENCES accounts(acnt_id);
ALTER TABLE transactions ADD CONSTRAINT transactions_trns_bucket_fkey FOREIGN KEY (trns_bucket) REFERENCES buckets(bkt_id) ON DELETE SET NULL;
CREATE TABLE transaction_categories (
trns_ctgry_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
trns_ctgry_dsply_code varchar(5) NOT NULL,
trns_ctgry_dsply_name varchar(50) NOT NULL,
trns_ctgry_description varchar(250) NULL,
CONSTRAINT transaction_categories_pkey PRIMARY KEY (trns_ctgry_id)
);
-- transaction_breakdown foreign keys
ALTER TABLE transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_catagory_fkey FOREIGN KEY (trns_brkdwn_catagory) REFERENCES transaction_categories(trns_ctgry_id) ON DELETE SET NULL;
ALTER TABLE transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_parent_transaction_fkey FOREIGN KEY (trns_brkdwn_parent_transaction) REFERENCES transactions(trns_id) ON DELETE CASCADE;
ALTER TABLE transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_bucket_fkey FOREIGN KEY (trns_brkdwn_bucket) REFERENCES buckets(bkt_id) ON DELETE SET NULL;
`
tx := db.MustBegin()
tx.MustExec(init_sql)
tx.Commit()
if err != nil {
log.Fatal(err)
}
log.Print("Test database initialized")
}