-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
167 lines (141 loc) · 3.18 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
type Storage interface {
CreateAccount(*Account) error
UpdateAccount(account *Account, id int) error
GetAccountById(int) (*Account, error)
DeleteAccount(int) error
GetAccounts() ([]*Account, error)
}
type PostgresStorage struct {
db *sql.DB
}
func NewPostgresStore() (*PostgresStorage, error) {
connStr := "user=postgres dbname=postgres password=gobank sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
return nil, err
}
if err := db.Ping(); err != nil {
log.Fatal(err)
return nil, err
}
return &PostgresStorage{
db: db,
}, nil
}
func (s *PostgresStorage) Init() {
err := s.CreateAccountTable()
if err != nil {
log.Fatal("Couldn't create a account table")
return
}
log.Fatal("Table was created successfully")
}
func (s *PostgresStorage) CreateAccountTable() error {
query := `CREATE TABLE IF NOT EXISTS accounts_table (
id SERIAL PRIMARY KEY,
first_name VARCHAR (50),
last_name VARCHAR (50),
Number serial,
balance serial,
created_at timestamp
)`
_, err := s.db.Exec(query)
if err != nil {
return err
}
return nil
}
// Implementation of the interface Methods;
func (s *PostgresStorage) CreateAccount(account *Account) error {
query := `INSERT INTO accounts_table
(
first_name,
last_name,
number,
balance,
created_at
) VALUES ($1, $2, $3, $4, $5)`
response, err := s.db.Exec(query,
account.FirstName,
account.LastName,
account.Number,
account.Balance,
account.CreatedAt,
)
if err != nil {
return err
}
rowsAffected, err := response.RowsAffected()
if err != nil {
return err
}
fmt.Printf("New account created with ID: %d\n", rowsAffected)
return nil
}
func (s *PostgresStorage) UpdateAccount(account *Account, id int) error {
query := `UPDATE accounts_table
SET first_name = $1, last_name = $2
WHERE id = $3`
_, err := s.db.Query(query, account.FirstName, account.LastName, id)
if err != nil {
return err
}
return nil
}
func (s *PostgresStorage) GetAccountById(id int) (*Account, error) {
rows, err := s.db.Query(`SELECT * FROM accounts_table where id = $1`, id)
if err != nil {
return nil, err
}
for rows.Next() {
return rowScanner(rows)
}
return nil, fmt.Errorf("sorry the account with the id of %d was not found", id)
}
func (s *PostgresStorage) DeleteAccount(id int) error {
_, err := s.db.Query(`DELETE FROM accounts_table where id = $1`, id)
if err != nil {
return err
}
return nil
}
func (s *PostgresStorage) GetAccounts() ([]*Account, error) {
query := `SELECT * FROM accounts_table`
rows, err := s.db.Query(query)
if err != nil {
return nil, err
}
var accounts []*Account
for rows.Next() {
account, err := rowScanner(rows)
if err != nil {
return nil, err
}
accounts = append(accounts, account)
}
fmt.Println(accounts)
return accounts, nil
}
func rowScanner(rows *sql.Rows) (*Account, error) {
account := new(Account)
err := rows.Scan(&account.Id,
&account.FirstName,
&account.LastName,
&account.Number,
&account.Balance,
&account.CreatedAt,
)
if err != nil {
log.Fatal(err)
return nil, err
}
return account, nil
}