Repository navigation
Expand file tree
/
Copy pathuser.go
More file actions
97 lines (78 loc) · 1.68 KB
/
Copy pathuser.go
File metadata and controls
97 lines (78 loc) · 1.68 KB
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
package ddd
import (
"crypto/sha256"
"encoding/base64"
"errors"
)
func HashPassword(password string) string {
h := sha256.New()
h.Write([]byte(password))
b := h.Sum(nil)
return base64.StdEncoding.EncodeToString(b)
}
type User struct {
Email string
FirstName string
LastName string
// this password field is not necessary, but we're using it as storage in the mock
// this would be necessary if we verified the pw outside of the repos
Password string
}
type UserRepository interface {
Create(UserCreate) (*User, error)
Login(UserLogin) (*User, error)
List() ([]*User, error)
Update(UserUpdate) (*User, error)
}
type UserCreate struct {
Email string
FirstName string
LastName string
Password string
}
func (uc UserCreate) Validate() error {
if uc.Email == "" {
return errors.New("email was empty")
}
if uc.FirstName == "" {
return errors.New("firstName was empty")
}
if uc.LastName == "" {
return errors.New("lastName was empty")
}
if uc.Password == "" {
return errors.New("password was empty")
}
return nil
}
type UserLogin struct {
Email string
Password string
}
func (ul UserLogin) Validate() error {
if ul.Email == "" {
return errors.New("email was empty")
}
if ul.Password == "" {
return errors.New("password was empty")
}
return nil
}
type UserUpdate struct {
// email is required, or an ID to update the correct account
Email string
FirstName string
LastName string
}
func (uu UserUpdate) Validate() error {
if uu.Email == "" {
return errors.New("email was empty")
}
if uu.FirstName == "" {
return errors.New("firstName was empty")
}
if uu.LastName == "" {
return errors.New("lastName was empty")
}
return nil
}