-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_repository.go
196 lines (157 loc) · 3.62 KB
/
account_repository.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package sqlite3
import (
"database/sql"
"fmt"
"time"
"github.com/jmoiron/sqlx"
"github.com/jadefish/avatar"
)
type accountRepo struct {
db *sqlx.DB
}
var _ avatar.AccountRepository = &accountRepo{}
// NewAccountRepo creates a new account repository using the provided DB as
// storage.
func NewAccountRepo(db *sqlx.DB) *accountRepo {
return &accountRepo{db}
}
const createQuery string = `
insert into accounts
(name, email, password_hash, creation_ip, blocked, created_at)
values (?, ?, ?, ?, ?, ?);
`
// Create an Account, persisting it to the underlying storage.
func (repo *accountRepo) Create(data avatar.CreateAccountData) error {
err := transaction(repo.db, func(tx *sqlx.Tx) error {
now := time.Now()
query := repo.db.Rebind(createQuery)
_, err := tx.Exec(query,
data.Name,
data.Email,
data.PasswordHash,
data.CreationIP,
false,
now)
return err
})
if err != nil {
return fmt.Errorf("create account: %w", err)
}
return nil
}
const deleteQuery string = `
update accounts
set deleted_at = ?,
updated_at = ?
where id = ?;
`
// Delete an existing account by ID.
func (repo *accountRepo) Delete(id avatar.EntityID) error {
err := transaction(repo.db, func(tx *sqlx.Tx) error {
query := tx.Rebind(deleteQuery)
now := time.Now()
_, err := tx.Exec(query, now, now, id)
return err
})
if err != nil {
return fmt.Errorf("delete account: %w", err)
}
return nil
}
const getQuery string = `
select a.*
from accounts a
where a.id = ?
and a.deleted_at is null;
`
// Get retrieves an Account by its ID.
func (repo *accountRepo) Get(id avatar.EntityID) (*avatar.Account, error) {
account := &avatar.Account{}
err := transaction(repo.db, func(tx *sqlx.Tx) error {
query := repo.db.Rebind(getQuery)
return repo.db.Get(account, query, id)
})
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return account, nil
}
const updateQuery string = `
update accounts
set name = ?,
email = ?,
password_hash = ?,
blocked = ?,
updated_at = ?
where id = ?;
`
// Update an Account.
func (repo *accountRepo) Update(
id avatar.EntityID,
data avatar.UpdateAccountData,
) error {
err := transaction(repo.db, func(tx *sqlx.Tx) error {
query := tx.Rebind(updateQuery)
now := time.Now()
_, err := tx.Exec(query,
data.Name,
data.Email,
data.PasswordHash,
data.Blocked,
now)
return err
})
if err != nil {
return fmt.Errorf("update account: %w", err)
}
return nil
}
const findByNameQuery string = `
select a.*
from accounts a
where a.name = ?
and a.deleted_at is null
limit 1;
`
// FindByName retrieves an account by its name.
// No error is returned if the account could not be found.
func (repo *accountRepo) FindByName(name string) (*avatar.Account, error) {
account := &avatar.Account{}
err := transaction(repo.db, func(tx *sqlx.Tx) error {
query := repo.db.Rebind(findByNameQuery)
return repo.db.Get(account, query, name)
})
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return account, nil
}
const findByEmailQuery string = `
select a.*
from accounts a
where a.email = ?
and a.deleted_at is null
limit 1;
`
// FindByEmail retrieves an account by its email.
// No error is returned if the account could not be found.
func (repo *accountRepo) FindByEmail(email string) (*avatar.Account, error) {
account := &avatar.Account{}
err := transaction(repo.db, func(tx *sqlx.Tx) error {
query := repo.db.Rebind(findByEmailQuery)
return repo.db.Get(account, query, email)
})
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return account, nil
}