This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
139 lines (113 loc) 路 3.53 KB
/
account.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
package repository
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/p12s/furniture-store/account/internal/domain"
)
var _ Accounter = (*Account)(nil)
// Accounter - repository interface
type Accounter interface {
CreateAccount(account domain.Account) error
GetAccount(publicId string) (domain.Account, error)
UpdateAccountInfo(input domain.UpdateAccountInput) error
UpdateAccountRole(input domain.UpdateAccountRoleInput) error
DeleteAccount(accountPublicId string) error
GetByCredentials(email, password string) (domain.Account, error)
}
// Account
type Account struct {
db *sqlx.DB
}
// NewAccount - constructor
func NewAccount(db *sqlx.DB) *Account {
return &Account{db: db}
}
// CreateAccount
func (r *Account) CreateAccount(account domain.Account) error {
query := fmt.Sprintf(`INSERT INTO %s (public_id, name, username, password_hash, email, address, role)
values ($1, $2, $3, $4, $5, $6, $7)`, accountTable)
_, err := r.db.Exec(query, account.PublicId, account.Name,
account.Username, account.Password, account.Email, account.Address, domain.ROLE_CUSTOMER)
return err
}
// GetAccount
func (r *Account) GetAccount(publicId string) (domain.Account, error) {
var account domain.Account
query := fmt.Sprintf(`SELECT * FROM %s WHERE public_id=$1`, accountTable)
err := r.db.Get(&account, query, publicId)
if err != nil {
return account, fmt.Errorf("get account: %w", err)
}
return account, err
}
// UpdateAccountInfo
func (r *Account) UpdateAccountInfo(input domain.UpdateAccountInput) error {
tx, err := r.db.Begin()
if err != nil {
return err
}
setValues := make([]string, 0)
args := make([]interface{}, 0)
argId := 1
if input.Name != nil {
setValues = append(setValues, fmt.Sprintf("name=$%d", argId))
args = append(args, *input.Name)
argId++
}
if input.Username != nil {
setValues = append(setValues, fmt.Sprintf("username=$%d", argId))
args = append(args, *input.Username)
argId++
}
if input.Password != nil {
setValues = append(setValues, fmt.Sprintf("password_hash=$%d", argId))
args = append(args, *input.Password)
argId++
}
if input.Email != nil {
setValues = append(setValues, fmt.Sprintf("email=$%d", argId))
args = append(args, *input.Email)
argId++
}
if input.Address != nil {
setValues = append(setValues, fmt.Sprintf("address=$%d", argId))
args = append(args, *input.Address)
argId++
}
setQuery := strings.Join(setValues, ", ")
query := fmt.Sprintf(`UPDATE %s SET %s WHERE public_id = $%d`,
accountTable, setQuery, argId)
args = append(args, input.PublicId.String())
_, err = r.db.Exec(query, args...)
if err != nil {
rollbackErr := tx.Rollback()
if rollbackErr != nil {
return rollbackErr
}
return nil
}
return tx.Commit()
}
// UpdateAccountRole
func (r *Account) UpdateAccountRole(input domain.UpdateAccountRoleInput) error {
query := fmt.Sprintf(`UPDATE %s SET role=$1 WHERE public_id = $2`, accountTable)
_, err := r.db.Exec(query, input.Role, input.PublicId)
return err
}
// DeleteAccount
func (r *Account) DeleteAccount(accountPublicId string) error {
query := fmt.Sprintf(`DELETE FROM %s WHERE public_id = $1`, accountTable)
_, err := r.db.Exec(query, accountPublicId)
return err
}
// GetByCredentials
func (r *Account) GetByCredentials(email, password string) (domain.Account, error) {
var account domain.Account
query := fmt.Sprintf(`SELECT * FROM %s WHERE email=$1 AND password_hash=$2`, accountTable)
err := r.db.Get(&account, query, email, password)
if err != nil {
return account, fmt.Errorf("get account: %w", err)
}
return account, err
}