-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user_interactor.go
69 lines (51 loc) · 1.89 KB
/
create_user_interactor.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
package usecase
import (
"context"
"user-api/entity/user"
"github.com/pkg/errors"
)
const (
msg_repository_nil string = "The UserRepository is a requested param for create a new CreateUser instance"
msg_user_already_exists string = "The user already existis, user's name: "
msg_error_create_user string = "There was a error when try to create user in database, user's name: "
msg_error_create_response string = "Error when try to create response for user named: "
)
type createUserInteractor struct {
repository UserRepository
}
// NewcreateUser is factory of CreateUser
// *UserRepository is a request param if it don't was passed then a error will returned
func NewCreateUser(repository *UserRepository) (CreateUser, error) {
if repository == nil {
return nil, errors.New(msg_repository_nil)
}
return &createUserInteractor{*repository}, nil
}
// CreateUser validate if user of same name exists in the repository
// case don't exists then user received is created and persisted in repository
func (interactor *createUserInteractor) Create(ctx context.Context, request CreateUserRequest) (CreateUserResponse, error) {
var response CreateUserResponse
var err error
existingUser, _ := interactor.repository.FindByName(ctx, request.Name())
if existingUser != nil {
return nil, errors.New(msg_user_already_exists + request.Name())
}
user, _ := createUserFromRequest(request)
userPersisted, err := interactor.repository.Save(ctx, user)
if err != nil {
return nil, errors.Wrap(err, msg_error_create_user+request.Name())
}
response, err = newCreateUserReponse(userPersisted.Id())
if err != nil {
return nil, errors.Wrap(err, msg_error_create_response+request.Name())
}
return response, nil
}
func createUserFromRequest(request CreateUserRequest) (user.User, error) {
return user.
NewBuilder().
Name(request.Name()).
Email(request.Email()).
Age(request.Age()).
Build()
}