2023-12-31 17:21:09 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-11 20:07:47 -08:00
|
|
|
"fmt"
|
2023-12-31 17:21:09 -08:00
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2024-01-01 20:47:34 -08:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-12-31 17:21:09 -08:00
|
|
|
)
|
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginationInput) error {
|
|
|
|
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Msg("Fatal error in db_get_transactions\nCannot connect to server")
|
|
|
|
}
|
|
|
|
|
|
|
|
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("LIMIT %d OFFSET %d",
|
|
|
|
r.ResultCount, r.PageNum*r.ResultCount))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-12-31 17:21:09 -08:00
|
|
|
}
|
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
func db_new_transaction(transaction Transaction) error {
|
|
|
|
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Msg("Fatal error in db_get_transactions\nCannot connect to server")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
log.Debug().Msgf("%#v", transaction)
|
|
|
|
|
|
|
|
_, err = db.NamedExec(
|
|
|
|
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(err).
|
|
|
|
Msg("Could not exec insert db query")
|
|
|
|
}
|
|
|
|
return nil
|
2023-12-31 17:21:09 -08:00
|
|
|
}
|