moved database to other package
This commit is contained in:
parent
b7773981ed
commit
aa88e9207d
11 changed files with 98 additions and 38 deletions
10
api.go
10
api.go
|
@ -10,6 +10,8 @@ import (
|
||||||
"github.com/ggicci/httpin"
|
"github.com/ggicci/httpin"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"nickiel.net/recount_server/db"
|
||||||
|
"nickiel.net/recount_server/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEFAULT_RESULT_COUNT = 50
|
const DEFAULT_RESULT_COUNT = 50
|
||||||
|
@ -37,9 +39,9 @@ func getTransactions(w http.ResponseWriter, req *http.Request) {
|
||||||
input.ResultCount = DEFAULT_RESULT_COUNT
|
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 {
|
if err != nil {
|
||||||
|
|
||||||
|
@ -62,7 +64,7 @@ func getTransactions(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
func newTransaction(w http.ResponseWriter, req *http.Request) {
|
func newTransaction(w http.ResponseWriter, req *http.Request) {
|
||||||
decoder := json.NewDecoder(req.Body)
|
decoder := json.NewDecoder(req.Body)
|
||||||
var t Transaction
|
var t types.Transaction
|
||||||
err := decoder.Decode(&t)
|
err := decoder.Decode(&t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().
|
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",
|
//fmt.Fprintf(w, "New transaction created for Account: %d, with an Amount of: %s",
|
||||||
// t.Account, t.Amount)
|
// t.Account, t.Amount)
|
||||||
db_new_transaction(t)
|
db.NewTransaction(t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -7,9 +7,20 @@ import (
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/rs/zerolog/log"
|
"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)
|
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
|
@ -24,14 +35,14 @@ func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginatio
|
||||||
"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("LIMIT %d OFFSET %d",
|
||||||
r.ResultCount, r.PageNum*r.ResultCount))
|
resultCount, pageNum*resultCount))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func db_new_transaction(transaction Transaction) error {
|
func NewTransaction(transaction types.Transaction) error {
|
||||||
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().
|
||||||
|
@ -55,3 +66,13 @@ func db_new_transaction(transaction Transaction) error {
|
||||||
}
|
}
|
||||||
return nil
|
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;
|
||||||
|
}
|
|
@ -2,15 +2,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"nickiel.net/recount_server/tests"
|
"nickiel.net/recount_server/tests"
|
||||||
|
"nickiel.net/recount_server/db"
|
||||||
"nickiel.net/recount_server/web"
|
"nickiel.net/recount_server/web"
|
||||||
|
|
||||||
"database/sql"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
//"github.com/shopspring/decimal"
|
//"github.com/shopspring/decimal"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
@ -23,17 +22,6 @@ var DB_TYPE = "postgres"
|
||||||
var DB_SCHEMA = "rcnt."
|
var DB_SCHEMA = "rcnt."
|
||||||
var DB_CONNECTION_STRING string = "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable"
|
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) {
|
func hello(w http.ResponseWriter, req *http.Request) {
|
||||||
fmt.Fprintf(w, "hello\n")
|
fmt.Fprintf(w, "hello\n")
|
||||||
}
|
}
|
||||||
|
@ -72,6 +60,8 @@ func main() {
|
||||||
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
debug_mode.Init_testdb(DB_TYPE, DB_CONNECTION_STRING)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.SetConstants(DB_TYPE, DB_SCHEMA, DB_CONNECTION_STRING);
|
||||||
|
|
||||||
debug_mode.SetLogLevel(zerolog.GlobalLevel())
|
debug_mode.SetLogLevel(zerolog.GlobalLevel())
|
||||||
|
|
||||||
log.Info().Msg("starting server")
|
log.Info().Msg("starting server")
|
||||||
|
|
|
@ -26,6 +26,10 @@ type HumanLegibleTransaction struct {
|
||||||
Date time.Time `db:"trns_date" json:"TransactionDate"`
|
Date time.Time `db:"trns_date" json:"TransactionDate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QuickTransactionTypes struct {
|
||||||
|
DisplayName string
|
||||||
|
}
|
||||||
|
|
||||||
type ChartjsData struct {
|
type ChartjsData struct {
|
||||||
Labels []string `json:"labels"`
|
Labels []string `json:"labels"`
|
||||||
Data []int `json:"data"`
|
Data []int `json:"data"`
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"nickiel.net/recount_server/db"
|
||||||
"nickiel.net/recount_server/web/templates"
|
"nickiel.net/recount_server/web/templates"
|
||||||
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -71,7 +72,10 @@ func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) {
|
||||||
|
|
||||||
|
|
||||||
var new_tp bytes.Buffer;
|
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 {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Could not render new transaction pane for index");
|
log.Fatal().Err(err).Msg("Could not render new transaction pane for index");
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,7 +123,7 @@ nav {
|
||||||
border: 2px solid var(--#{$prefix}-surface1);
|
border: 2px solid var(--#{$prefix}-surface1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#new_transaction_pane {
|
#new-transaction-pane {
|
||||||
height: 75vh;
|
height: 75vh;
|
||||||
width: 50vw;
|
width: 50vw;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
|
|
@ -140,6 +140,23 @@ $w_h_sizes: (
|
||||||
color: var(--#{$prefix}-nav-active-color);
|
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 {
|
table.table {
|
||||||
color: var(--#{$prefix}-text);
|
color: var(--#{$prefix}-text);
|
||||||
td {
|
td {
|
||||||
|
|
|
@ -42,22 +42,22 @@ function toggle_drafts() {
|
||||||
drafts_is_open = true;
|
drafts_is_open = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function close_drafts() {
|
||||||
|
close_new_transaction_pane();
|
||||||
|
drafts_is_open = false;
|
||||||
|
}
|
||||||
const open_drafts = debounce(toggle_drafts);
|
const open_drafts = debounce(toggle_drafts);
|
||||||
|
|
||||||
function register_nav_links() {
|
|
||||||
|
function register_handlers() {
|
||||||
var navAnchors = document.querySelectorAll("nav a");
|
var navAnchors = document.querySelectorAll("nav a");
|
||||||
navAnchors.forEach(function (a_el) {
|
navAnchors.forEach(function (a_el) {
|
||||||
a_el.removeEventListener("click", switch_nav);
|
a_el.removeEventListener("click", switch_nav);
|
||||||
a_el.addEventListener("click", switch_nav);
|
a_el.addEventListener("click", switch_nav);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function register_handlers() {
|
|
||||||
register_nav_links();
|
|
||||||
//fill_charts();
|
|
||||||
register_dropdowns();
|
register_dropdowns();
|
||||||
document.querySelector("#open-draft").addEventListener("click", open_drafts);
|
document.querySelector("#open-draft").addEventListener("click", open_drafts);
|
||||||
|
document.querySelector("#close-transaction-pane").addEventListener("click", close_drafts);
|
||||||
close_new_transaction_pane();
|
close_new_transaction_pane();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,4 +80,4 @@ function load_in_table() {
|
||||||
const trigger_table_animation = debounce(load_in_table, 100);
|
const trigger_table_animation = debounce(load_in_table, 100);
|
||||||
const trigger_reset_handlers = debounce(register_handlers, 200);
|
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};
|
||||||
|
|
|
@ -9,7 +9,7 @@ const centerOffset = offset(({rects}) => {
|
||||||
|
|
||||||
function open_new_transaction_pane() {
|
function open_new_transaction_pane() {
|
||||||
let center = document.querySelector("#main-body-content");
|
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";
|
new_tp.style.opacity = "1";
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ function open_new_transaction_pane() {
|
||||||
|
|
||||||
function close_new_transaction_pane() {
|
function close_new_transaction_pane() {
|
||||||
let right_column = document.querySelector("#right-col");
|
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";
|
new_tp.style.opacity = "0";
|
||||||
computePosition(right_column, new_tp, {
|
computePosition(right_column, new_tp, {
|
||||||
placement: "right",
|
placement: "right",
|
||||||
|
|
|
@ -97,10 +97,17 @@
|
||||||
</div>
|
</div>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.min.js" integrity="sha512-CQBWl4fJHWbryGE+Pc7UAxWMUMNMWzWxF4SQo9CgkJIN1kx6djDQZjh3Y8SZ1d+6I+1zze6Z7kHXO7q3UyZAWw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.min.js" integrity="sha512-CQBWl4fJHWbryGE+Pc7UAxWMUMNMWzWxF4SQo9CgkJIN1kx6djDQZjh3Y8SZ1d+6I+1zze6Z7kHXO7q3UyZAWw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {trigger_reset_handlers, trigger_table_animation, fill_chart} from "/static/index.js";
|
import {
|
||||||
|
trigger_reset_handlers,
|
||||||
|
trigger_table_animation,
|
||||||
|
fill_chart,
|
||||||
|
close_drafts
|
||||||
|
} from "/static/index.js";
|
||||||
|
|
||||||
htmx.onLoad(function (element) {
|
htmx.onLoad(function (element) {
|
||||||
feather.replace();
|
feather.replace();
|
||||||
if (element.dataset.mainBody) {
|
if (element.dataset.mainBody) {
|
||||||
|
close_drafts();
|
||||||
trigger_reset_handlers();
|
trigger_reset_handlers();
|
||||||
}
|
}
|
||||||
if (element.localName === "tr") {
|
if (element.localName === "tr") {
|
||||||
|
|
|
@ -1,9 +1,24 @@
|
||||||
package templates
|
package templates
|
||||||
|
|
||||||
|
import "nickiel.net/recount_server/types"
|
||||||
|
|
||||||
templ NewTransactionPane() {
|
|
||||||
<div id="new_transaction_pane" style="opacity: 0;" class="cr-all c-base">
|
|
||||||
|
|
||||||
|
templ NewTransactionPane(entry_types *[]types.QuickTransactionTypes) {
|
||||||
|
<div id="new-transaction-pane" style="opacity: 0;" class="cr-all c-base d-flex-col">
|
||||||
|
<div class="my-3 d-flex">
|
||||||
|
<h2 class="ms-5">New Transaction</h2>
|
||||||
|
<button class="ms-auto me-4 my-auto exit-btn" id="close-transaction-pane">
|
||||||
|
<i class="my-auto" data-feather="x"></i>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="d-flex" style="overflow-x: scroll;">
|
||||||
|
for _, value := range *entry_types {
|
||||||
|
<button class="borderless-btn mx-3">
|
||||||
|
{value.DisplayName}
|
||||||
|
</button>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue