-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
192 lines (151 loc) · 3.24 KB
/
main.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
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/google/uuid"
)
const (
NameMaxLength = 20
EmailMaxLength = 40
)
var (
ErrEmailIsTooLong = errors.New("email is too long")
ErrNameIsTooLong = errors.New("name is too long")
ErrUserCreated = errors.New("user already created")
)
var usersCreated = map[string]User{}
type User struct {
ID string
Name string
Email string
CreatedAt time.Time
UpdatedAt time.Time
}
type UserReq struct {
Name string
Email string
}
type UserResp struct {
ID string
Name string
Email string
}
func NewUser(name, email string) User {
return User{
ID: uuid.NewString(),
Name: name,
Email: email,
}
}
func NewName(name string) (string, error) {
if len(name) > NameMaxLength {
return "", ErrNameIsTooLong
}
return strings.TrimSpace(name), nil
}
func NewEmail(email string) (string, error) {
if len(email) > EmailMaxLength {
return "", ErrEmailIsTooLong
}
return strings.TrimSpace(email), nil
}
type UserRepo interface {
Create(ctx context.Context, user User) error
GetUserByID(ctx context.Context, id string) (*User, error)
}
type PostgresUserRepo struct {
db *sql.DB
}
func NewUserRepo(db *sql.DB) *PostgresUserRepo {
return &PostgresUserRepo{
db: db,
}
}
func (p *PostgresUserRepo) Create(ctx context.Context, user User) error {
if user.ID == "user-created" {
return ErrUserCreated
}
return nil
}
func (p *PostgresUserRepo) GetUserByID(ctx context.Context, id string) (*User, error) {
return &User{
ID: id,
Name: "Paulo",
Email: "[email protected]",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}, nil
}
type UserService struct {
repo UserRepo
}
func NewUserService(ur UserRepo) *UserService {
return &UserService{
repo: ur,
}
}
func (ur *UserService) GetUserByID(ctx context.Context, id string) (UserResp, error) {
// Business logic
var err error
if id == "" {
return UserResp{}, errors.New("empty ID")
}
user, err := ur.repo.GetUserByID(ctx, id)
if err != nil {
return UserResp{}, err
}
return UserResp{
ID: user.ID,
Name: user.Name,
Email: user.Email,
}, nil
}
func (ur *UserService) Create(ctx context.Context, user UserReq) error {
// Business logic
var err error
user.Email, err = NewEmail(user.Email)
if err != nil {
return err
}
user.Name, err = NewName(user.Name)
if err != nil {
return err
}
u := NewUser(user.Name, user.Email)
err = ur.repo.Create(ctx, u)
if err != nil {
return err
}
usersCreated[u.ID] = u
fmt.Printf("User %s created!\n", u.ID)
return nil
}
func main() {
ctx := context.Background()
postgresUserRepo := NewUserRepo(&sql.DB{})
userService := NewUserService(postgresUserRepo)
userReq := UserReq{
Name: "Paulo",
Email: "[email protected]",
}
err := userService.Create(ctx, userReq)
if err != nil {
log.Fatalf("Error trying to create a user: %v\n", err)
}
err = userService.Create(ctx, userReq)
if err != nil {
log.Fatalf("Error trying to create a user: %v\n", err)
}
for _, user := range usersCreated {
user, err := userService.GetUserByID(ctx, user.ID)
if err != nil {
log.Fatalf("Error to get user: %v\n", err)
}
fmt.Printf("Welcome, %s! (%s)\n", user.Name, user.ID)
}
}