-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore.go
132 lines (111 loc) · 3.65 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"database/sql"
_ "embed"
"fmt"
"time"
"github.com/mr-karan/gullak/internal/db"
"github.com/mr-karan/gullak/pkg/models"
_ "modernc.org/sqlite"
)
const (
DEFAULT_CURRENCY = "INR"
)
//go:embed pragmas.sql
var pragmas string
func createTableSQL(currency string) string {
return fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at DATETIME NOT NULL DEFAULT (datetime('now')),
transaction_date DATE NOT NULL,
currency TEXT NOT NULL DEFAULT '%s',
amount FLOAT NOT NULL,
category TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
confirm BOOLEAN NOT NULL DEFAULT false
);
`, currency)
}
func initDB(path string, currency string) (*db.Queries, error) {
conn, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("error opening database: %w", err)
}
if currency == "" {
currency = DEFAULT_CURRENCY
}
// Create the table if it doesn't exist.
if _, err = conn.Exec(createTableSQL(currency)); err != nil {
return nil, fmt.Errorf("error creating tables: %w", err)
}
// PRAGMA statements aren't recognised by sqlc:https://github.com/sqlc-dev/sqlc/issues/3237.
if _, err = conn.Exec(pragmas); err != nil {
return nil, fmt.Errorf("error running PRAGMA statements: %w", err)
}
queries := db.New(conn)
return queries, nil
}
// SaveTransactions saves the transactions to the database using the generated CreateTransaction method.
func (a *App) Save(transactions models.Transactions) ([]db.Transaction, error) {
var savedTransactions []db.Transaction
for _, item := range transactions.Transactions {
var transactDate time.Time
var err error
if item.TransactionDate == "" {
transactDate = time.Now()
} else {
transactDate, err = time.Parse("2006-01-02", item.TransactionDate)
if err != nil {
a.log.Warn("Invalid date format", "date", item.TransactionDate, "error", err, "description", item.Description)
transactDate = time.Now()
}
}
arg := db.CreateTransactionParams{
CreatedAt: time.Now(),
TransactionDate: transactDate,
Amount: item.Amount,
Currency: item.Currency,
Category: item.Category,
Description: item.Description,
}
savedTx, err := a.queries.CreateTransaction(context.TODO(), arg)
if err != nil {
return nil, fmt.Errorf("error saving in db: %w", err)
}
savedTransactions = append(savedTransactions, savedTx...)
}
return savedTransactions, nil
}
// Get retrieves a single transaction by ID.
func (a *App) Get(id int64) (models.Item, error) {
transaction, err := a.queries.GetTransaction(context.TODO(), id)
if err != nil {
return models.Item{}, fmt.Errorf("error getting transaction: %w", err)
}
return models.Item{
CreatedAt: transaction.CreatedAt.Format(time.RFC3339),
TransactionDate: transaction.TransactionDate.Format("2006-01-02"),
Currency: transaction.Currency,
Amount: transaction.Amount,
Category: transaction.Category,
Description: transaction.Description,
Confirm: transaction.Confirm,
}, nil
}
// Update updates a transaction in the database.
func (a *App) Update(id int64, transaction models.Item) error {
arg := db.UpdateTransactionParams{
Amount: transaction.Amount,
Currency: transaction.Currency,
Category: transaction.Category,
Description: transaction.Description,
Confirm: transaction.Confirm,
ID: id,
}
if err := a.queries.UpdateTransaction(context.TODO(), arg); err != nil {
return fmt.Errorf("error updating transaction: %w", err)
}
return nil
}