Skip to content

Commit

Permalink
Merge pull request #36 from darcys22/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
darcys22 authored Jun 26, 2020
2 parents 93808a1 + afb1e21 commit badacb4
Show file tree
Hide file tree
Showing 25 changed files with 1,185 additions and 179 deletions.
2 changes: 1 addition & 1 deletion godbledger/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func initConfig(ctx *cli.Context) error {
config := defaultLedgerConfig
if ctx.Bool("mysql") {
config.DatabaseType = "mysql"
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger?charset=utf8"
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger"
}

if len(ctx.Args().Get(0)) > 0 {
Expand Down
14 changes: 14 additions & 0 deletions godbledger/core/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func NewTransaction(usr *User) (*Transaction, error) {
return txn, nil
}

func ReverseTransaction(originalTxn *Transaction, usr *User) (*Transaction, error) {
guid := xid.New()
txn := &Transaction{guid.String(), time.Now(), usr, []byte{}, []*Split{}}

for _, split := range originalTxn.Splits {
newSplt, err := NewSplit(split.Date, split.Description, split.Accounts, split.Currency, big.NewInt(0).Mul(big.NewInt(-1), split.Amount))
if err != nil {
return nil, err
}
txn.AppendSplit(newSplt)
}
return txn, nil
}

func (txn *Transaction) AppendSplit(spl *Split) error {
txn.Splits = append(txn.Splits, spl)
return nil
Expand Down
8 changes: 5 additions & 3 deletions godbledger/core/trialbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package core
import ()

type TBAccount struct {
Account string `json:"Account"`
Amount int `json:"Amount"`
Tags []string `json:"Tags"`
Account string `json:"Account"`
Amount int `json:"Amount"`
Tags []string `json:"Tags"`
Currency string `json:"Currency"`
Decimals int `json:"Decimals"`
}
7 changes: 6 additions & 1 deletion godbledger/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import (
type Database interface {
InitDB() error
Close() error
AddTransaction(txn *core.Transaction) error
AddTransaction(txn *core.Transaction) (string, error)
FindTransaction(txnID string) (*core.Transaction, error)
DeleteTransaction(txnID string) error
FindTag(tag string) (int, error)
AddTag(tag string) error
SafeAddTag(tag string) error
SafeAddTagToAccount(account, tag string) error
AddTagToAccount(accountID string, tag int) error
DeleteTagFromAccount(account, tag string) error
SafeAddTagToTransaction(txnID, tag string) error
AddTagToTransaction(txnID string, tag int) error
DeleteTagFromTransaction(txnID, tag string) error
FindCurrency(cur string) (*core.Currency, error)
AddCurrency(cur *core.Currency) error
SafeAddCurrency(cur *core.Currency) error
DeleteCurrency(currency string) error
FindAccount(code string) (*core.Account, error)
AddAccount(*core.Account) error
SafeAddAccount(*core.Account) error
Expand Down
40 changes: 30 additions & 10 deletions godbledger/db/mysql/mysqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@ func (db *Database) Close() error {
}

func DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME string) string {
return DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8"
return DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8&parseTime=true"
//return DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/"
}

// NewDB initializes a new DB.
//TODO(Sean): this should actually use the connection_string rather than hardcoded
func NewDB(connection_string string) (*Database, error) {
func ValidateConnectionString(connection_string string) string {
//if connection_string == "" {
//DB_HOST := "tcp(127.0.0.1:3306)"
//DB_NAME := "ledger"
//DB_USER := "godbledger"
//DB_PASS := "password"
//connection_string = DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME)
//}
return connection_string + "?charset=utf8&parseTime=true"
}

// NewDB initializes a new DB.
func NewDB(connection_string string) (*Database, error) {
log.Debug(connection_string)
MySQLDB, err := sql.Open("mysql", connection_string)
MySQLDB, err := sql.Open("mysql", ValidateConnectionString(connection_string))
if err != nil {
log.Fatal(err.Error)
return nil, err
Expand All @@ -60,7 +63,7 @@ func (db *Database) InitDB() error {
log.Debug("Query: " + createDB)
_, err := db.DB.Exec(createDB)
if err != nil {
log.Fatal(err)
log.Fatalf("Creating users table failed: %s", err)
}

//ACCOUNTS
Expand All @@ -73,7 +76,7 @@ func (db *Database) InitDB() error {
log.Debug("Query: " + createDB)
_, err = db.DB.Exec(createDB)
if err != nil {
log.Fatal(err)
log.Fatalf("Creating accounts table failed: %s", err)
}

//TAGS
Expand All @@ -85,7 +88,7 @@ func (db *Database) InitDB() error {
log.Debug("Query: " + createDB)
_, err = db.DB.Exec(createDB)
if err != nil {
log.Fatal(err)
log.Fatalf("Creating tags table failed: %s", err)
}

//TAGS FOR ACCOUNTS
Expand All @@ -100,7 +103,7 @@ func (db *Database) InitDB() error {
log.Debug("Query: " + createDB)
_, err = db.DB.Exec(createDB)
if err != nil {
log.Fatal(err)
log.Fatalf("Creating Account_Tag table failed: %s", err)
}

//CURRENCIES
Expand All @@ -122,7 +125,9 @@ func (db *Database) InitDB() error {
transaction_id VARCHAR(255) NOT NULL,
postdate DATETIME NOT NULL,
brief VARCHAR(255),
PRIMARY KEY(transaction_id)
poster_user_id VARCHAR(255),
PRIMARY KEY(transaction_id),
FOREIGN KEY (poster_user_id) REFERENCES users (user_id) ON DELETE RESTRICT
);`
log.Debug("Query: " + createDB)
_, err = db.DB.Exec(createDB)
Expand All @@ -143,6 +148,21 @@ func (db *Database) InitDB() error {
log.Fatal(err)
}

//TAGS FOR Transactions
createDB = `
CREATE TABLE IF NOT EXISTS transaction_tag (
transaction_id VARCHAR(255) NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (transaction_id, tag_id)
);`
log.Debug("Query: " + createDB)
_, err = db.DB.Exec(createDB)
if err != nil {
log.Fatalf("Creating Transaction_Tag table failed: %s", err)
}

//LINE ITEMS FOR TRANSACTIONS (SPLITS)
createDB = `
CREATE TABLE IF NOT EXISTS splits (
Expand Down
Loading

0 comments on commit badacb4

Please sign in to comment.