-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.go
66 lines (51 loc) · 1.52 KB
/
repo.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type PayoutRepo struct {
db *sql.DB
}
// We are dropping the database to make sure we can rerun the project
const initPayoutTableSQL = `
DROP TABLE IF EXISTS payouts;
CREATE TABLE payouts (
id INTEGER PRIMARY KEY,
amount INTEGER,
currency VARCHAR(3),
booking_id INTEGER,
advertiser_id INTEGER,
tenant_id INTEGER,
payin_at DATETIME,
payout_date DATE
);`
const insertPayoutSQL = `INSERT INTO payouts (
amount, currency, booking_id, advertiser_id, tenant_id, payin_at, payout_date
) VALUES (?, ?, ?, ?, ?, ?, ?)`
const selectAllPayouts = `SELECT * FROM payouts`
func (pr *PayoutRepo) InsertPayout(payout Payout) error {
_, err := pr.db.Exec(insertPayoutSQL, payout.Amount, payout.Currency, payout.BookingId, payout.AdvertiserId, payout.TenantId, payout.PayinAt, payout.PayoutDate)
return err
}
func (pr *PayoutRepo) InitPayoutTable() error {
_, err := pr.db.Exec(initPayoutTableSQL)
return err
}
func (pr *PayoutRepo) GetAllPayouts() ([]Payout, error) {
rows, err := pr.db.Query(selectAllPayouts)
if err != nil {
return nil, err
}
defer rows.Close()
payouts := []Payout{}
for rows.Next() {
payout := Payout{}
err = rows.Scan(&payout.Id, &payout.Amount, &payout.Currency, &payout.BookingId, &payout.AdvertiserId, &payout.TenantId, &payout.PayinAt, &payout.PayoutDate)
if err != nil {
return nil, err
}
// Add payout to the result array
payouts = append(payouts, payout)
}
return payouts, nil
}