2024-02-10 10:52:09 -08:00
|
|
|
package db
|
2023-12-31 17:21:09 -08:00
|
|
|
|
|
|
|
import (
|
2024-02-27 18:38:00 -08:00
|
|
|
"database/sql"
|
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"
|
2024-02-10 10:52:09 -08:00
|
|
|
"nickiel.net/recount_server/types"
|
2023-12-31 17:21:09 -08:00
|
|
|
)
|
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
var DB_TYPE string
|
|
|
|
var DB_SCHEMA string
|
|
|
|
var DB_CONNECTION_STRING string
|
2024-02-10 10:52:09 -08:00
|
|
|
|
|
|
|
func SetConstants(db_type string, db_schema string, db_conn_string string) {
|
2024-02-27 18:38:00 -08:00
|
|
|
DB_TYPE = db_type
|
|
|
|
DB_SCHEMA = db_schema
|
|
|
|
DB_CONNECTION_STRING = db_conn_string
|
2024-02-10 10:52:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetTransactions(transactions *[]types.Transaction, resultCount int, pageNum int) error {
|
2024-01-11 20:07:47 -08:00
|
|
|
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",
|
2024-02-10 10:52:09 -08:00
|
|
|
resultCount, pageNum*resultCount))
|
2024-01-11 20:07:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-12-31 17:21:09 -08:00
|
|
|
}
|
|
|
|
|
2024-02-10 10:52:09 -08:00
|
|
|
func NewTransaction(transaction types.Transaction) error {
|
2024-01-11 20:07:47 -08:00
|
|
|
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
|
|
|
}
|
2024-02-10 10:52:09 -08:00
|
|
|
|
2024-02-17 14:50:58 -08:00
|
|
|
func GetUserAccounts(userID int) ([]types.Account, error) {
|
2024-02-27 18:38:00 -08:00
|
|
|
user_accounts := make([]types.Account, 4)
|
|
|
|
|
|
|
|
user_accounts[0] = types.Account{
|
|
|
|
Id: 1,
|
|
|
|
DisplayName: sql.NullString{
|
|
|
|
String: "Savings",
|
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
Description: sql.NullString{
|
|
|
|
String: "BECU Saving Account",
|
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user_accounts[1] = types.Account{
|
|
|
|
Id: 2,
|
|
|
|
DisplayName: sql.NullString{
|
|
|
|
String: "BECU Credit Card",
|
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
Description: sql.NullString{
|
|
|
|
String: "BECU Saving Account",
|
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_accounts, nil
|
2024-02-17 14:50:58 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 17:35:33 -08:00
|
|
|
func GetUserBuckets(userID int) ([]types.Bucket, error) {
|
2024-02-27 18:38:00 -08:00
|
|
|
bucket1 := types.Bucket{
|
|
|
|
Id: 1,
|
|
|
|
DisplayCode: sql.NullString{String: "SVNG", Valid: true},
|
|
|
|
DisplayName: sql.NullString{String: "Savings", Valid: true},
|
|
|
|
Description: sql.NullString{String: "The Savings Bucket", Valid: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket2 := types.Bucket{
|
|
|
|
Id: 2,
|
|
|
|
DisplayCode: sql.NullString{String: "SPND", Valid: true},
|
|
|
|
DisplayName: sql.NullString{String: "Spending", Valid: true},
|
|
|
|
Description: sql.NullString{String: "The Spending Bucket", Valid: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a slice of length two and populating it with the two structs
|
|
|
|
buckets := []types.Bucket{bucket1, bucket2}
|
|
|
|
|
|
|
|
return buckets, nil
|
2024-02-27 17:35:33 -08:00
|
|
|
}
|
|
|
|
|
2024-02-17 14:50:58 -08:00
|
|
|
func GetTransPaneEntries(userID int) ([]types.QuickTransactionType, error) {
|
2024-02-27 18:38:00 -08:00
|
|
|
transaction_types := make([]types.QuickTransactionType, 1)
|
2024-02-17 14:50:58 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
transaction_types[0] = types.QuickTransactionType{
|
|
|
|
DisplayName: "Manual",
|
|
|
|
}
|
2024-02-10 10:52:09 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
return transaction_types, nil
|
2024-02-10 10:52:09 -08:00
|
|
|
}
|