-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.33 KB
/
main.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "file:data.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Initialize the database
repo := PayoutRepo{db}
err = repo.InitPayoutTable()
if err != nil {
log.Fatal(err)
}
fmt.Println("initialized")
// Load all payouts from the file and inserts them
payouts, err := LoadPayoutsFromFile("payouts.json")
if err != nil {
log.Fatal(err)
}
for _, payout := range payouts {
err = repo.InsertPayout(payout)
}
if err != nil {
log.Fatal(err)
}
// Get all payouts from the db and display them in console
payouts, err = repo.GetAllPayouts()
if err != nil {
log.Fatal(err)
}
for _, payout := range payouts {
fmt.Printf("%v\n", payout)
}
}
func LoadPayoutsFromFile(filename string) ([]Payout, error) {
// Open our jsonFile
jsonFile, err := os.Open(filename)
// if we os.Open returns an error then handle it
if err != nil {
return nil, err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
var result []Payout
err = json.Unmarshal(byteValue, &result)
if err != nil {
return nil, err
}
return result, nil
}