This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
62 lines (48 loc) · 1.61 KB
/
auth.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
package repository
import (
"fmt"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/p12s/uber-popug/auth/pkg/models"
)
// Authorization - signup/signin
type Authorization interface {
CreateAccount(account models.Account) (int, error)
GetAccount(username, password string) (models.Account, error)
GetAccountById(accountId int) (models.Account, error)
}
// Auth - repo
type Auth struct {
db *sqlx.DB
}
func NewAuth(db *sqlx.DB) *Auth {
return &Auth{db: db}
}
func (r *Auth) CreateAccount(account models.Account) (int, error) {
var id int
query := fmt.Sprintf(`INSERT INTO %s (public_id, name, username, password_hash, role)
values ($1, $2, $3, $4, $5) RETURNING id`, accountTable)
row := r.db.QueryRow(query, uuid.New(), account.Name, account.Username, account.Password, models.Employee)
if err := row.Scan(&id); err != nil {
return 0, fmt.Errorf("create account: %w", err)
}
return id, nil
}
func (r *Auth) GetAccount(username, password string) (models.Account, error) {
var account models.Account
query := fmt.Sprintf(`SELECT id FROM %s WHERE username=$1 AND password_hash=$2`, accountTable)
err := r.db.Get(&account, query, username, password)
if err != nil {
return account, fmt.Errorf("get account: %w", err)
}
return account, err
}
func (r *Auth) GetAccountById(accountId int) (models.Account, error) {
var account models.Account
query := fmt.Sprintf(`SELECT id, public_id, name, username, role, created_at FROM %s WHERE id=$1`, accountTable)
err := r.db.Get(&account, query, accountId)
if err != nil {
return account, fmt.Errorf("get account by id: %w", err)
}
return account, err
}