-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_adapter.go
133 lines (114 loc) · 4.06 KB
/
user_adapter.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
package infrastructures
import (
"context"
"github.com/hiroyky/famiphoto/entities"
"github.com/hiroyky/famiphoto/infrastructures/dbmodels"
"github.com/hiroyky/famiphoto/infrastructures/filters"
"github.com/hiroyky/famiphoto/infrastructures/repositories"
"github.com/hiroyky/famiphoto/utils/array"
"github.com/hiroyky/famiphoto/utils/cast"
"time"
)
type UserAdapter interface {
GetUser(ctx context.Context, userID string) (*entities.User, error)
GetUsers(ctx context.Context, filter *filters.UserFilter, limit, offset int) (entities.UserList, error)
CountUsers(ctx context.Context, filter *filters.UserFilter) (int, error)
ExistUser(ctx context.Context, userID string) (bool, error)
CreateUser(ctx context.Context, user *entities.User, password string, isInitializedPassword bool, now time.Time) (*entities.User, error)
UpdateUserProfile(ctx context.Context, user *entities.User) (*entities.User, error)
GetUserPassword(ctx context.Context, userID string) (*entities.UserPassword, error)
}
func NewUserAdapter(
userRepo repositories.UserRepository,
userPasswordRepo repositories.UserPasswordRepository,
photoStorageRepo repositories.PhotoStorageRepository,
) UserAdapter {
return &userAdapter{
userRepo: userRepo,
userPasswordRepo: userPasswordRepo,
photoStorageRepo: photoStorageRepo,
}
}
type userAdapter struct {
userRepo repositories.UserRepository
userPasswordRepo repositories.UserPasswordRepository
photoStorageRepo repositories.PhotoStorageRepository
}
func (a *userAdapter) GetUser(ctx context.Context, userID string) (*entities.User, error) {
dbUser, err := a.userRepo.GetUser(ctx, userID)
if err != nil {
return nil, err
}
return a.toUserEntity(dbUser), nil
}
func (a *userAdapter) GetUsers(ctx context.Context, filter *filters.UserFilter, limit, offset int) (entities.UserList, error) {
dbUsers, err := a.userRepo.GetUsers(ctx, filter, limit, offset)
if err != nil {
return nil, err
}
return array.Map(dbUsers, a.toUserEntity), nil
}
func (a *userAdapter) CountUsers(ctx context.Context, filter *filters.UserFilter) (int, error) {
return a.userRepo.CountUsers(ctx, filter)
}
func (a *userAdapter) ExistUser(ctx context.Context, userID string) (bool, error) {
return a.userRepo.ExistUser(ctx, userID)
}
func (a *userAdapter) CreateUser(ctx context.Context, user *entities.User, password string, isInitializedPassword bool, now time.Time) (*entities.User, error) {
dbUser := &dbmodels.User{
UserID: user.UserID,
Name: user.Name,
Status: a.toDBUserStatus(user.Status),
}
dbPassword := &dbmodels.UserPassword{
UserID: user.UserID,
Password: password,
LastModifiedAt: now,
IsInitialized: cast.BoolToInt8(isInitializedPassword),
}
dstDBUser, err := a.userRepo.CreateUser(ctx, dbUser, dbPassword)
if err != nil {
return nil, err
}
// ユーザディレクトリを作らない(あとできちんと消す)
//if err := a.photoStorageRepo.CreateUserDir(dbUser.UserID); err != nil {
// return nil, err
//}
return a.toUserEntity(dstDBUser), nil
}
func (a *userAdapter) UpdateUserProfile(ctx context.Context, user *entities.User) (*entities.User, error) {
dbUser := &dbmodels.User{
UserID: user.UserID,
Name: user.Name,
}
dst, err := a.userRepo.UpdateUserProfile(ctx, dbUser)
if err != nil {
return nil, err
}
return a.toUserEntity(dst), nil
}
func (a *userAdapter) GetUserPassword(ctx context.Context, userID string) (*entities.UserPassword, error) {
up, err := a.userPasswordRepo.GetUserPassword(ctx, userID)
if err != nil {
return nil, err
}
return &entities.UserPassword{
UserId: up.UserID,
Password: up.Password,
LastModifiedAt: up.LastModifiedAt,
IsInitialized: cast.IntToBool(up.IsInitialized),
}, nil
}
func (a *userAdapter) toDBUserStatus(s entities.UserStatus) int {
return int(s)
}
func (a *userAdapter) toEntityUserStatus(s int) entities.UserStatus {
return entities.UserStatus(s)
}
func (a *userAdapter) toUserEntity(user *dbmodels.User) *entities.User {
return &entities.User{
UserID: user.UserID,
Name: user.Name,
Status: a.toEntityUserStatus(user.Status),
}
}