Skip to content

Commit badacb4

Browse files
authored
Merge pull request #36 from darcys22/dev
Dev
2 parents 93808a1 + afb1e21 commit badacb4

25 files changed

+1185
-179
lines changed

godbledger/cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func initConfig(ctx *cli.Context) error {
150150
config := defaultLedgerConfig
151151
if ctx.Bool("mysql") {
152152
config.DatabaseType = "mysql"
153-
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger?charset=utf8"
153+
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger"
154154
}
155155

156156
if len(ctx.Args().Get(0)) > 0 {

godbledger/core/transactions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ func NewTransaction(usr *User) (*Transaction, error) {
5656
return txn, nil
5757
}
5858

59+
func ReverseTransaction(originalTxn *Transaction, usr *User) (*Transaction, error) {
60+
guid := xid.New()
61+
txn := &Transaction{guid.String(), time.Now(), usr, []byte{}, []*Split{}}
62+
63+
for _, split := range originalTxn.Splits {
64+
newSplt, err := NewSplit(split.Date, split.Description, split.Accounts, split.Currency, big.NewInt(0).Mul(big.NewInt(-1), split.Amount))
65+
if err != nil {
66+
return nil, err
67+
}
68+
txn.AppendSplit(newSplt)
69+
}
70+
return txn, nil
71+
}
72+
5973
func (txn *Transaction) AppendSplit(spl *Split) error {
6074
txn.Splits = append(txn.Splits, spl)
6175
return nil

godbledger/core/trialbalance.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package core
33
import ()
44

55
type TBAccount struct {
6-
Account string `json:"Account"`
7-
Amount int `json:"Amount"`
8-
Tags []string `json:"Tags"`
6+
Account string `json:"Account"`
7+
Amount int `json:"Amount"`
8+
Tags []string `json:"Tags"`
9+
Currency string `json:"Currency"`
10+
Decimals int `json:"Decimals"`
911
}

godbledger/db/database.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ import (
1010
type Database interface {
1111
InitDB() error
1212
Close() error
13-
AddTransaction(txn *core.Transaction) error
13+
AddTransaction(txn *core.Transaction) (string, error)
14+
FindTransaction(txnID string) (*core.Transaction, error)
1415
DeleteTransaction(txnID string) error
1516
FindTag(tag string) (int, error)
1617
AddTag(tag string) error
1718
SafeAddTag(tag string) error
1819
SafeAddTagToAccount(account, tag string) error
1920
AddTagToAccount(accountID string, tag int) error
2021
DeleteTagFromAccount(account, tag string) error
22+
SafeAddTagToTransaction(txnID, tag string) error
23+
AddTagToTransaction(txnID string, tag int) error
24+
DeleteTagFromTransaction(txnID, tag string) error
2125
FindCurrency(cur string) (*core.Currency, error)
2226
AddCurrency(cur *core.Currency) error
2327
SafeAddCurrency(cur *core.Currency) error
28+
DeleteCurrency(currency string) error
2429
FindAccount(code string) (*core.Account, error)
2530
AddAccount(*core.Account) error
2631
SafeAddAccount(*core.Account) error

godbledger/db/mysql/mysqldb.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ func (db *Database) Close() error {
2121
}
2222

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

28-
// NewDB initializes a new DB.
29-
//TODO(Sean): this should actually use the connection_string rather than hardcoded
30-
func NewDB(connection_string string) (*Database, error) {
28+
func ValidateConnectionString(connection_string string) string {
3129
//if connection_string == "" {
3230
//DB_HOST := "tcp(127.0.0.1:3306)"
3331
//DB_NAME := "ledger"
3432
//DB_USER := "godbledger"
3533
//DB_PASS := "password"
3634
//connection_string = DSN(DB_USER, DB_PASS, DB_HOST, DB_NAME)
3735
//}
36+
return connection_string + "?charset=utf8&parseTime=true"
37+
}
38+
39+
// NewDB initializes a new DB.
40+
func NewDB(connection_string string) (*Database, error) {
3841
log.Debug(connection_string)
39-
MySQLDB, err := sql.Open("mysql", connection_string)
42+
MySQLDB, err := sql.Open("mysql", ValidateConnectionString(connection_string))
4043
if err != nil {
4144
log.Fatal(err.Error)
4245
return nil, err
@@ -60,7 +63,7 @@ func (db *Database) InitDB() error {
6063
log.Debug("Query: " + createDB)
6164
_, err := db.DB.Exec(createDB)
6265
if err != nil {
63-
log.Fatal(err)
66+
log.Fatalf("Creating users table failed: %s", err)
6467
}
6568

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

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

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

106109
//CURRENCIES
@@ -122,7 +125,9 @@ func (db *Database) InitDB() error {
122125
transaction_id VARCHAR(255) NOT NULL,
123126
postdate DATETIME NOT NULL,
124127
brief VARCHAR(255),
125-
PRIMARY KEY(transaction_id)
128+
poster_user_id VARCHAR(255),
129+
PRIMARY KEY(transaction_id),
130+
FOREIGN KEY (poster_user_id) REFERENCES users (user_id) ON DELETE RESTRICT
126131
);`
127132
log.Debug("Query: " + createDB)
128133
_, err = db.DB.Exec(createDB)
@@ -143,6 +148,21 @@ func (db *Database) InitDB() error {
143148
log.Fatal(err)
144149
}
145150

151+
//TAGS FOR Transactions
152+
createDB = `
153+
CREATE TABLE IF NOT EXISTS transaction_tag (
154+
transaction_id VARCHAR(255) NOT NULL,
155+
tag_id INTEGER NOT NULL,
156+
FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) ON DELETE RESTRICT ON UPDATE CASCADE,
157+
FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE RESTRICT ON UPDATE CASCADE,
158+
PRIMARY KEY (transaction_id, tag_id)
159+
);`
160+
log.Debug("Query: " + createDB)
161+
_, err = db.DB.Exec(createDB)
162+
if err != nil {
163+
log.Fatalf("Creating Transaction_Tag table failed: %s", err)
164+
}
165+
146166
//LINE ITEMS FOR TRANSACTIONS (SPLITS)
147167
createDB = `
148168
CREATE TABLE IF NOT EXISTS splits (

0 commit comments

Comments
 (0)