-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
145 lines (117 loc) · 3.03 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
type Storage interface {
CreateAccount(*Account) error
UpdateAccount(*Account) error
GetAccountByID(int) (*Account, error)
GetAccounts() ([]*Account, error)
DeleteAccount(int) error
GetAccountByNumber(int64) (*Account, error)
}
type PostgreesStorage struct {
db *sql.DB
}
func NewPostgresStore() (*PostgreesStorage, 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 &PostgreesStorage{
db: db,
}, nil
}
func (s *PostgreesStorage) Init() error {
return s.createAccountTable()
}
func (s *PostgreesStorage) createAccountTable() error {
query := `create table if not exists account(
id serial primary key,
first_name varchar(50),
last_name varchar(50),
number serial,
encrypted_password varchar(255),
balance serial,
created_at timestamp
)`
_, err := s.db.Exec(query)
return err
}
func (s *PostgreesStorage) Seed() error {
account, err := NewAccount("John", "Doe", "Hunter1234")
if err != nil {
return err
}
return s.CreateAccount(account)
}
func (s *PostgreesStorage) CreateAccount(account *Account) error {
statement := `INSERT INTO account (first_name, last_name, number, encrypted_password, balance, created_at)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err := s.db.Exec(statement, account.FisrtName, account.LastName, account.Number, account.EncryptedPassword, account.Balance, account.CreatedAt)
return err
}
func (s *PostgreesStorage) UpdateAccount(account *Account) error {
return nil
}
func (s *PostgreesStorage) GetAccountByID(id int) (*Account, error) {
rows, err := s.db.Query("SELECT * FROM account WHERE id = $1", id)
if err != nil {
return nil, err
}
for rows.Next() {
return scanIntoAccount(rows)
}
return nil, fmt.Errorf("account %d not found", id)
}
func (s *PostgreesStorage) GetAccounts() ([]*Account, error) {
rows, err := s.db.Query("SELECT * FROM account")
if err != nil {
return nil, err
}
accounts := []*Account{}
for rows.Next() {
account, err := scanIntoAccount(rows)
if err != nil {
return nil, err
}
accounts = append(accounts, account)
}
return accounts, nil
}
func (s *PostgreesStorage) GetAccountByNumber(accountNumber int64) (*Account, error) {
rows, err := s.db.Query("SELECT * FROM account WHERE number = $1", accountNumber)
if err != nil {
return nil, err
}
for rows.Next() {
return scanIntoAccount(rows)
}
return nil, fmt.Errorf("account %d not found", accountNumber)
}
func (s *PostgreesStorage) DeleteAccount(id int) error {
_, err := s.db.Query("DELETE FROM account WHERE id = $1", id)
if err != nil {
return err
}
return nil
}
func scanIntoAccount(rows *sql.Rows) (*Account, error) {
account := new(Account)
err := rows.Scan(
&account.ID,
&account.FisrtName,
&account.LastName,
&account.Number,
&account.EncryptedPassword,
&account.Balance,
&account.CreatedAt,
)
return account, err
}