From aa88e9207d65c9d8606c037f70010e88d9e459bb Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sat, 10 Feb 2024 10:52:09 -0800 Subject: [PATCH] moved database to other package --- api.go | 10 ++++---- db.go => db/db.go | 29 ++++++++++++++++++++---- recount-server.go | 16 +++---------- types/types.go | 4 ++++ web/router.go | 8 +++++-- web/sass/nav.scss | 2 +- web/sass/utility-classes.scss | 17 ++++++++++++++ web/static/index.js | 16 ++++++------- web/static/new_transactions.js | 4 ++-- web/templates/index.html | 9 +++++++- web/templates/new_transaction_pane.templ | 21 ++++++++++++++--- 11 files changed, 98 insertions(+), 38 deletions(-) rename db.go => db/db.go (61%) diff --git a/api.go b/api.go index 2477887..2b97769 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,8 @@ import ( "github.com/ggicci/httpin" "github.com/go-chi/chi/v5" "github.com/rs/zerolog/log" + "nickiel.net/recount_server/db" + "nickiel.net/recount_server/types" ) const DEFAULT_RESULT_COUNT = 50 @@ -37,9 +39,9 @@ func getTransactions(w http.ResponseWriter, req *http.Request) { input.ResultCount = DEFAULT_RESULT_COUNT } - transactions := []Transaction{} + transactions := []types.Transaction{} - err := db_get_transactions(&transactions, input) + err := db.GetTransactions(&transactions, input.ResultCount, input.PageNum); if err != nil { @@ -62,7 +64,7 @@ func getTransactions(w http.ResponseWriter, req *http.Request) { func newTransaction(w http.ResponseWriter, req *http.Request) { decoder := json.NewDecoder(req.Body) - var t Transaction + var t types.Transaction err := decoder.Decode(&t) if err != nil { log.Fatal(). @@ -71,5 +73,5 @@ func newTransaction(w http.ResponseWriter, req *http.Request) { } //fmt.Fprintf(w, "New transaction created for Account: %d, with an Amount of: %s", // t.Account, t.Amount) - db_new_transaction(t) + db.NewTransaction(t) } diff --git a/db.go b/db/db.go similarity index 61% rename from db.go rename to db/db.go index 881786b..df30327 100644 --- a/db.go +++ b/db/db.go @@ -1,4 +1,4 @@ -package main +package db import ( "fmt" @@ -7,9 +7,20 @@ import ( _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" "github.com/rs/zerolog/log" + "nickiel.net/recount_server/types" ) -func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginationInput) error { +var DB_TYPE string; +var DB_SCHEMA string; +var DB_CONNECTION_STRING string; + +func SetConstants(db_type string, db_schema string, db_conn_string string) { + DB_TYPE = db_type; + DB_SCHEMA = db_schema; + DB_CONNECTION_STRING = db_conn_string; +} + +func GetTransactions(transactions *[]types.Transaction, resultCount int, pageNum int) error { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { log.Fatal(). @@ -24,14 +35,14 @@ func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginatio "trns_account, trns_bucket, trns_date "+ fmt.Sprintf("FROM %stransactions ORDER BY trns_id DESC ", DB_SCHEMA)+ fmt.Sprintf("LIMIT %d OFFSET %d", - r.ResultCount, r.PageNum*r.ResultCount)) + resultCount, pageNum*resultCount)) if err != nil { return err } return nil } -func db_new_transaction(transaction Transaction) error { +func NewTransaction(transaction types.Transaction) error { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { log.Fatal(). @@ -55,3 +66,13 @@ func db_new_transaction(transaction Transaction) error { } return nil } + +func GetTransPaneEntries(userID int) ([]types.QuickTransactionTypes, error) { + transaction_types := make([]types.QuickTransactionTypes, 1); + + transaction_types[0] = types.QuickTransactionTypes { + DisplayName: "Manual", + }; + + return transaction_types, nil; +} diff --git a/recount-server.go b/recount-server.go index da3bde8..64e9155 100644 --- a/recount-server.go +++ b/recount-server.go @@ -2,15 +2,14 @@ package main import ( "nickiel.net/recount_server/tests" + "nickiel.net/recount_server/db" "nickiel.net/recount_server/web" - "database/sql" "net/http" "flag" "fmt" "os" - "time" //"github.com/shopspring/decimal" "github.com/go-chi/chi/v5" @@ -23,17 +22,6 @@ 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) -// if you use `json:"-"` it doesn't encode it -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 hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } @@ -72,6 +60,8 @@ func main() { debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING) } + db.SetConstants(DB_TYPE, DB_SCHEMA, DB_CONNECTION_STRING); + debug_mode.SetLogLevel(zerolog.GlobalLevel()) log.Info().Msg("starting server") diff --git a/types/types.go b/types/types.go index bdfe927..641b85b 100644 --- a/types/types.go +++ b/types/types.go @@ -26,6 +26,10 @@ type HumanLegibleTransaction struct { Date time.Time `db:"trns_date" json:"TransactionDate"` } +type QuickTransactionTypes struct { + DisplayName string +} + type ChartjsData struct { Labels []string `json:"labels"` Data []int `json:"data"` diff --git a/web/router.go b/web/router.go index cdaa31b..39a3b64 100644 --- a/web/router.go +++ b/web/router.go @@ -1,7 +1,8 @@ package web import ( - "nickiel.net/recount_server/web/templates" + "nickiel.net/recount_server/db" + "nickiel.net/recount_server/web/templates" "html/template" "net/http" @@ -71,7 +72,10 @@ func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) { var new_tp bytes.Buffer; - err = templates.NewTransactionPane().Render(context.Background(), &new_tp); + + quick_trans_types, err := db.GetTransPaneEntries(0); + + err = templates.NewTransactionPane(&quick_trans_types).Render(context.Background(), &new_tp); if err != nil { log.Fatal().Err(err).Msg("Could not render new transaction pane for index"); } diff --git a/web/sass/nav.scss b/web/sass/nav.scss index b4d4942..fba88fd 100644 --- a/web/sass/nav.scss +++ b/web/sass/nav.scss @@ -123,7 +123,7 @@ nav { border: 2px solid var(--#{$prefix}-surface1); } -#new_transaction_pane { +#new-transaction-pane { height: 75vh; width: 50vw; position: absolute; diff --git a/web/sass/utility-classes.scss b/web/sass/utility-classes.scss index b63f626..54379fc 100644 --- a/web/sass/utility-classes.scss +++ b/web/sass/utility-classes.scss @@ -140,6 +140,23 @@ $w_h_sizes: ( color: var(--#{$prefix}-nav-active-color); } +.exit-btn { + display: flex; + color: var(--#{$prefix}-text); + font-size: 1em; + border: none; + padding: 11px 11px 11px 11px; + border-radius: $border-radius; + background-color: var(--#{$prefix}-crust); + transition: all 0.1s linear; + cursor: pointer; + height: fit-content; + width: fit-content; +} +.exit-btn:hover { + background-color: var(--#{$prefix}-surface0); +} + table.table { color: var(--#{$prefix}-text); td { diff --git a/web/static/index.js b/web/static/index.js index 9d6c281..ea21685 100644 --- a/web/static/index.js +++ b/web/static/index.js @@ -42,22 +42,22 @@ function toggle_drafts() { drafts_is_open = true; } } +function close_drafts() { + close_new_transaction_pane(); + drafts_is_open = false; +} const open_drafts = debounce(toggle_drafts); -function register_nav_links() { + +function register_handlers() { var navAnchors = document.querySelectorAll("nav a"); navAnchors.forEach(function (a_el) { a_el.removeEventListener("click", switch_nav); a_el.addEventListener("click", switch_nav); }); -} - - -function register_handlers() { - register_nav_links(); - //fill_charts(); register_dropdowns(); document.querySelector("#open-draft").addEventListener("click", open_drafts); + document.querySelector("#close-transaction-pane").addEventListener("click", close_drafts); close_new_transaction_pane(); } @@ -80,4 +80,4 @@ function load_in_table() { const trigger_table_animation = debounce(load_in_table, 100); const trigger_reset_handlers = debounce(register_handlers, 200); -export {trigger_reset_handlers, trigger_table_animation, fill_chart}; +export {trigger_reset_handlers, trigger_table_animation, fill_chart, close_drafts}; diff --git a/web/static/new_transactions.js b/web/static/new_transactions.js index 0588ed2..9b81d17 100644 --- a/web/static/new_transactions.js +++ b/web/static/new_transactions.js @@ -9,7 +9,7 @@ const centerOffset = offset(({rects}) => { function open_new_transaction_pane() { let center = document.querySelector("#main-body-content"); - let new_tp = document.querySelector("#new_transaction_pane"); + let new_tp = document.querySelector("#new-transaction-pane"); new_tp.style.opacity = "1"; @@ -32,7 +32,7 @@ function open_new_transaction_pane() { function close_new_transaction_pane() { let right_column = document.querySelector("#right-col"); - let new_tp = document.querySelector("#new_transaction_pane"); + let new_tp = document.querySelector("#new-transaction-pane"); new_tp.style.opacity = "0"; computePosition(right_column, new_tp, { placement: "right", diff --git a/web/templates/index.html b/web/templates/index.html index a79b686..7adeec1 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -97,10 +97,17 @@