forked from pact-foundation/pact-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_service.go
57 lines (47 loc) · 1.21 KB
/
user_service.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
package provider
import "errors"
// User is a representation of a User. Dah.
type User struct {
Name string `json:"name"`
username string
password string
}
var (
// ErrNotFound represents a resource not found (404)
ErrNotFound = errors.New("not found")
// ErrUnauthorized represents a Unauthorized (401)
ErrUnauthorized = errors.New("unauthorized")
// ErrEmpty is returned when input string is empty
ErrEmpty = errors.New("empty string")
)
// Service provides operations on Users.
type Service interface {
Login(string, string) (*User, error)
}
type userService struct {
userDatabase *UserRepository
}
// NewInmemService gets you a shiny new UserService!
func NewInmemService() Service {
return &userService{
userDatabase: &UserRepository{
users: map[string]*User{
"billy": &User{
Name: "billy",
username: "billy",
password: "issilly",
},
},
},
}
}
// Login to the system.
func (u *userService) Login(username string, password string) (user *User, err error) {
if user, err = u.userDatabase.ByUsername(username); err != nil {
return nil, ErrNotFound
}
if user.username != username || user.password != password {
return nil, ErrUnauthorized
}
return user, nil
}