forked from anthdm/gobank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
58 lines (48 loc) · 1.37 KB
/
types.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
package main
import (
"math/rand"
"time"
"golang.org/x/crypto/bcrypt"
)
type LoginResponse struct {
Number int64 `json:"number"`
Token string `json:"token"`
}
type LoginRequest struct {
Number int64 `json:"number"`
Password string `json:"password"`
}
type TransferRequest struct {
ToAccount int `json:"toAccount"`
Amount int `json:"amount"`
}
type CreateAccountRequest struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Password string `json:"password"`
}
type Account struct {
ID int `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Number int64 `json:"number"`
EncryptedPassword string `json:"-"`
Balance int64 `json:"balance"`
CreatedAt time.Time `json:"createdAt"`
}
func (a *Account) ValidPassword(pw string) bool {
return bcrypt.CompareHashAndPassword([]byte(a.EncryptedPassword), []byte(pw)) == nil
}
func NewAccount(firstName, lastName, password string) (*Account, error) {
encpw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
return &Account{
FirstName: firstName,
LastName: lastName,
EncryptedPassword: string(encpw),
Number: int64(rand.Intn(1000000)),
CreatedAt: time.Now().UTC(),
}, nil
}