This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
executable file
·155 lines (131 loc) · 3.48 KB
/
store.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
package user
import (
"database/sql"
"errors"
"github.com/mattn/go-sqlite3"
)
type User struct {
ID int64 `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Location string `json:"location,omitempty"`
About string `json:"about,omitempty"`
PasswordHash string `json:"-"`
}
var (
ErrDuplicate = errors.New("record already exists")
ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed")
)
type Repository struct {
db *sql.DB
}
func NewUserRepository(db *sql.DB) *Repository {
return &Repository{
db: db,
}
}
// FIXME: Using serial number as an id can leak information about the
// user. Don't do this in production.
func (r *Repository) Migrate() error {
query := `
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL DEFAULT '',
location TEXT DEFAULT '',
about TEXT DEFAULT '',
passwd TEXT NOT NULL
);
`
_, err := r.db.Exec(query)
return err
}
func (r *Repository) Create(user *User, passwd string) (*User, error) {
res, err := r.db.Exec("INSERT INTO users(email, name, location, about, passwd) values(?,?,?,?,?)", user.Email, user.Name, user.Location, user.About, passwd)
if err != nil {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintUnique) {
return nil, ErrDuplicate
}
}
return nil, err
}
id, err := res.LastInsertId()
if err != nil {
return nil, err
}
user.ID = id
return user, nil
}
func (r *Repository) All() ([]User, error) {
rows, err := r.db.Query("SELECT * FROM users")
if err != nil {
return nil, err
}
defer rows.Close()
var all []User
for rows.Next() {
var user User
if err := rows.Scan(&user.ID, &user.Email, &user.Location, &user.About); err != nil {
return nil, err
}
all = append(all, user)
}
return all, nil
}
func (r *Repository) GetByEmail(email string) (*User, error) {
row := r.db.QueryRow("SELECT * FROM users WHERE email = ?", email)
var user User
if err := row.Scan(&user.ID, &user.Email, &user.Name, &user.Location, &user.About, &user.PasswordHash); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotExists
}
return nil, err
}
return &user, nil
}
func (r *Repository) GetByID(id int64) (*User, error) {
row := r.db.QueryRow("SELECT * FROM users WHERE id = ?", id)
var user User
if err := row.Scan(&user.ID, &user.Email, &user.Name, &user.Location, &user.About, &user.PasswordHash); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotExists
}
return nil, err
}
return &user, nil
}
func (r *Repository) Update(id int64, updated User) (*User, error) {
if id == 0 {
return nil, errors.New("invalid updated id")
}
res, err := r.db.Exec("UPDATE users SET name = ?, email = ?, location = ? WHERE id = ?", updated.Name, updated.Email, updated.Location, id)
if err != nil {
return nil, err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return nil, err
}
if rowsAffected == 0 {
return nil, ErrUpdateFailed
}
return &updated, nil
}
func (r *Repository) Delete(id int64) error {
res, err := r.db.Exec("DELETE FROM users WHERE id = ?", id)
if err != nil {
return err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return ErrDeleteFailed
}
return err
}