/
create_user.go
81 lines (70 loc) · 2.14 KB
/
create_user.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
package repository
import (
"context"
"time"
"github.com/GSabadini/golang-clean-architecture/domain/entity"
"github.com/GSabadini/golang-clean-architecture/infrastructure/database"
"github.com/pkg/errors"
)
type (
// Bson data
createUserBSON struct {
ID string `bson:"id"`
FullName string `bson:"full_name"`
Email string `bson:"email"`
Password string `bson:"password"`
Document createUserDocumentBSON `bson:"document"`
Wallet createUserWalletBSON `bson:"wallet"`
Roles createUserRolesBSON `bson:"roles"`
Type string `bson:"type"`
CreatedAt time.Time `bson:"created_at"`
}
// Bson data
createUserDocumentBSON struct {
Type string `bson:"type"`
Value string `bson:"value"`
}
// Bson data
createUserWalletBSON struct {
Currency string `bson:"currency"`
Amount int64 `bson:"amount"`
}
// Bson data
createUserRolesBSON struct {
CanTransfer bool `bson:"can_transfer"`
}
createUserRepository struct {
handler *database.MongoHandler
collection string
}
)
// NewCreateUserRepository creates new createUserRepository with its dependencies
func NewCreateUserRepository(handler *database.MongoHandler) entity.UserRepositoryCreator {
return createUserRepository{
handler: handler,
collection: "users",
}
}
// Create performs insertOne into the database
func (c createUserRepository) Create(ctx context.Context, u entity.User) (entity.User, error) {
var bson = createUserBSON{
ID: u.ID().Value(),
FullName: u.FullName().Value(),
Document: createUserDocumentBSON{
Type: u.Document().Type().String(),
Value: u.Document().Value(),
},
Email: u.Email().Value(),
Password: u.Password().Value(),
Wallet: createUserWalletBSON{
Currency: u.Wallet().Money().Currency().String(),
Amount: u.Wallet().Money().Amount().Value(),
},
Type: u.TypeUser().String(),
CreatedAt: u.CreatedAt(),
}
if _, err := c.handler.Db().Collection(c.collection).InsertOne(ctx, bson); err != nil {
return entity.User{}, errors.Wrap(err, entity.ErrCreateUser.Error())
}
return u, nil
}