-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
108 lines (92 loc) · 2.19 KB
/
storage.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
package main
import (
"database/sql"
_ "github.com/lib/pq"
)
type Storage interface {
CreateAccount(*Account) (*Account, error)
DeleteAccount(int) error
UpdateAccount(*Account) error
GetAccounts() ([]*Account, error)
GetAccountById(int) (*Account, error)
}
type PostgresStore struct {
db *sql.DB
}
func NewPostgresStore() (*PostgresStore, error) {
connStr := "user=postgres dbname=postgres password=gobank sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return &PostgresStore{
db: db,
}, nil
}
func (s *PostgresStore) Init() error {
return s.createAccountTable()
}
func (s *PostgresStore) createAccountTable() error {
query := `CREATE TABLE IF NOT EXISTS account (
id SERIAL PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
number SERIAL,
balance SERIAL,
created_at TIMESTAMP
)`
_, err := s.db.Exec(query)
return err
}
func (s *PostgresStore) CreateAccount(acc *Account) (*Account, error) {
account := &Account{}
stmt, err := s.db.Prepare(`INSERT INTO account
(first_name, last_name, number, balance, created_at)
VALUES
($1, $2, $3, $4, $5)
RETURNING *
`)
defer stmt.Close()
if err != nil {
return nil, err
}
err = stmt.QueryRow(acc.FirstName, acc.LastName, acc.Number, acc.Balance, acc.CreatedAt).Scan(&account.ID, &account.FirstName, &account.LastName, &account.Number, &account.Balance, &account.CreatedAt)
if err != nil {
return nil, err
}
return account, nil
}
func (s *PostgresStore) DeleteAccount(id int) error {
return nil
}
func (s *PostgresStore) UpdateAccount(*Account) error {
return nil
}
func (s *PostgresStore) GetAccountById(id int) (*Account, error) {
return nil, nil
}
func (s *PostgresStore) GetAccounts() ([]*Account, error) {
rows, err := s.db.Query("SELECT * FROM account")
if err != nil {
return nil, err
}
accounts := []*Account{}
for rows.Next() {
account := &Account{}
err := rows.Scan(
&account.ID,
&account.FirstName,
&account.LastName,
&account.Number,
&account.Balance,
&account.CreatedAt)
if err != nil {
return nil, err
}
accounts = append(accounts, account)
}
return accounts, nil
}