Skip to content

Commit 7bd4841

Browse files
committed
add export package
1 parent d986152 commit 7bd4841

File tree

14 files changed

+693
-0
lines changed

14 files changed

+693
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package infrastructure
2+
3+
import (
4+
"context"
5+
6+
"github.com/reearth/reearth-accounts/server/pkg/id"
7+
"github.com/reearth/reearth-accounts/server/pkg/repo"
8+
"github.com/reearth/reearth-accounts/server/pkg/user"
9+
internalRepo "github.com/reearth/reearth-accounts/server/internal/usecase/repo"
10+
"github.com/reearth/reearthx/usecasex"
11+
)
12+
13+
// userAdapter adapts internal repo.User to pkg repo.User interface
14+
type userAdapter struct {
15+
internal internalRepo.User
16+
}
17+
18+
// NewUserAdapter creates an adapter that bridges internal User implementation to pkg User interface
19+
func NewUserAdapter(internal internalRepo.User) repo.User {
20+
return &userAdapter{internal: internal}
21+
}
22+
23+
func (a *userAdapter) FindAll(ctx context.Context) ([]*user.User, error) {
24+
list, err := a.internal.FindAll(ctx)
25+
return []*user.User(list), err
26+
}
27+
28+
func (a *userAdapter) FindByID(ctx context.Context, uid id.UserID) (*user.User, error) {
29+
return a.internal.FindByID(ctx, user.ID(uid))
30+
}
31+
32+
func (a *userAdapter) FindByIDs(ctx context.Context, ids id.UserIDList) ([]*user.User, error) {
33+
userIDs := make(user.IDList, len(ids))
34+
for i, uid := range ids {
35+
userIDs[i] = user.ID(uid)
36+
}
37+
list, err := a.internal.FindByIDs(ctx, userIDs)
38+
return []*user.User(list), err
39+
}
40+
41+
func (a *userAdapter) FindByIDsWithPagination(ctx context.Context, ids id.UserIDList, pagination *usecasex.Pagination, nameOrAlias ...string) ([]*user.User, *usecasex.PageInfo, error) {
42+
// Internal implementation doesn't have pagination for FindByIDs
43+
// We'll implement a simple version here
44+
users, err := a.FindByIDs(ctx, ids)
45+
if err != nil {
46+
return nil, nil, err
47+
}
48+
49+
// Apply name/alias filter if provided
50+
if len(nameOrAlias) > 0 && nameOrAlias[0] != "" {
51+
filtered := make([]*user.User, 0)
52+
for _, u := range users {
53+
if u.Name() == nameOrAlias[0] || u.Alias() == nameOrAlias[0] {
54+
filtered = append(filtered, u)
55+
}
56+
}
57+
users = filtered
58+
}
59+
60+
// Apply pagination
61+
total := int64(len(users))
62+
start, end := 0, len(users)
63+
64+
if pagination != nil {
65+
if pagination.Offset != nil {
66+
start = int(pagination.Offset.Offset)
67+
if start > len(users) {
68+
start = len(users)
69+
}
70+
if pagination.Offset.Limit > 0 {
71+
end = start + int(pagination.Offset.Limit)
72+
if end > len(users) {
73+
end = len(users)
74+
}
75+
}
76+
} else if pagination.Cursor != nil {
77+
// For cursor-based pagination, use First
78+
if pagination.Cursor.First != nil {
79+
end = int(*pagination.Cursor.First)
80+
if end > len(users) {
81+
end = len(users)
82+
}
83+
}
84+
}
85+
}
86+
87+
return users[start:end], &usecasex.PageInfo{
88+
TotalCount: total,
89+
}, nil
90+
}
91+
92+
func (a *userAdapter) FindBySub(ctx context.Context, sub string) (*user.User, error) {
93+
return a.internal.FindBySub(ctx, sub)
94+
}
95+
96+
func (a *userAdapter) FindByEmail(ctx context.Context, email string) (*user.User, error) {
97+
return a.internal.FindByEmail(ctx, email)
98+
}
99+
100+
func (a *userAdapter) FindByName(ctx context.Context, name string) (*user.User, error) {
101+
return a.internal.FindByName(ctx, name)
102+
}
103+
104+
func (a *userAdapter) FindByAlias(ctx context.Context, alias string) (*user.User, error) {
105+
return a.internal.FindByAlias(ctx, alias)
106+
}
107+
108+
func (a *userAdapter) FindByNameOrEmail(ctx context.Context, nameOrEmail string) (*user.User, error) {
109+
return a.internal.FindByNameOrEmail(ctx, nameOrEmail)
110+
}
111+
112+
func (a *userAdapter) SearchByKeyword(ctx context.Context, keyword string, fields ...string) ([]*user.User, error) {
113+
// Internal implementation only takes keyword, not fields
114+
list, err := a.internal.SearchByKeyword(ctx, keyword)
115+
return []*user.User(list), err
116+
}
117+
118+
func (a *userAdapter) FindByVerification(ctx context.Context, code string) (*user.User, error) {
119+
return a.internal.FindByVerification(ctx, code)
120+
}
121+
122+
func (a *userAdapter) FindByPasswordResetRequest(ctx context.Context, token string) (*user.User, error) {
123+
return a.internal.FindByPasswordResetRequest(ctx, token)
124+
}
125+
126+
func (a *userAdapter) FindBySubOrCreate(ctx context.Context, u *user.User, sub string) (*user.User, error) {
127+
return a.internal.FindBySubOrCreate(ctx, u, sub)
128+
}
129+
130+
func (a *userAdapter) Create(ctx context.Context, u *user.User) error {
131+
return a.internal.Create(ctx, u)
132+
}
133+
134+
func (a *userAdapter) Save(ctx context.Context, u *user.User) error {
135+
return a.internal.Save(ctx, u)
136+
}
137+
138+
func (a *userAdapter) Remove(ctx context.Context, uid id.UserID) error {
139+
return a.internal.Remove(ctx, user.ID(uid))
140+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package infrastructure
2+
3+
import (
4+
"context"
5+
6+
"github.com/reearth/reearth-accounts/server/pkg/id"
7+
"github.com/reearth/reearth-accounts/server/pkg/repo"
8+
"github.com/reearth/reearth-accounts/server/pkg/user"
9+
"github.com/reearth/reearth-accounts/server/pkg/workspace"
10+
internalRepo "github.com/reearth/reearth-accounts/server/internal/usecase/repo"
11+
"github.com/reearth/reearthx/usecasex"
12+
)
13+
14+
// workspaceAdapter adapts internal repo.Workspace to pkg repo.Workspace interface
15+
type workspaceAdapter struct {
16+
internal internalRepo.Workspace
17+
}
18+
19+
// NewWorkspaceAdapter creates an adapter that bridges internal Workspace implementation to pkg Workspace interface
20+
func NewWorkspaceAdapter(internal internalRepo.Workspace) repo.Workspace {
21+
return &workspaceAdapter{internal: internal}
22+
}
23+
24+
func (a *workspaceAdapter) Filtered(f repo.WorkspaceFilter) repo.Workspace {
25+
internalFilter := internalRepo.WorkspaceFilter{
26+
Readable: f.Readable,
27+
Writable: f.Writable,
28+
}
29+
return &workspaceAdapter{
30+
internal: a.internal.Filtered(internalFilter),
31+
}
32+
}
33+
34+
func (a *workspaceAdapter) FindByID(ctx context.Context, wid id.WorkspaceID) (*workspace.Workspace, error) {
35+
return a.internal.FindByID(ctx, workspace.ID(wid))
36+
}
37+
38+
func (a *workspaceAdapter) FindByName(ctx context.Context, name string) (*workspace.Workspace, error) {
39+
return a.internal.FindByName(ctx, name)
40+
}
41+
42+
func (a *workspaceAdapter) FindByAlias(ctx context.Context, alias string) (*workspace.Workspace, error) {
43+
return a.internal.FindByAlias(ctx, alias)
44+
}
45+
46+
func (a *workspaceAdapter) FindByIDs(ctx context.Context, ids id.WorkspaceIDList) ([]*workspace.Workspace, error) {
47+
wsIDs := make(workspace.IDList, len(ids))
48+
for i, wid := range ids {
49+
wsIDs[i] = workspace.ID(wid)
50+
}
51+
list, err := a.internal.FindByIDs(ctx, wsIDs)
52+
return []*workspace.Workspace(list), err
53+
}
54+
55+
func (a *workspaceAdapter) FindByUser(ctx context.Context, uid id.UserID) ([]*workspace.Workspace, error) {
56+
list, err := a.internal.FindByUser(ctx, user.ID(uid))
57+
return []*workspace.Workspace(list), err
58+
}
59+
60+
func (a *workspaceAdapter) FindByUserWithPagination(ctx context.Context, uid id.UserID, pagination *usecasex.Pagination) ([]*workspace.Workspace, *usecasex.PageInfo, error) {
61+
list, pageInfo, err := a.internal.FindByUserWithPagination(ctx, user.ID(uid), pagination)
62+
return []*workspace.Workspace(list), pageInfo, err
63+
}
64+
65+
func (a *workspaceAdapter) FindByIntegration(ctx context.Context, iid id.IntegrationID) ([]*workspace.Workspace, error) {
66+
list, err := a.internal.FindByIntegration(ctx, workspace.IntegrationID(iid))
67+
return []*workspace.Workspace(list), err
68+
}
69+
70+
func (a *workspaceAdapter) FindByIntegrations(ctx context.Context, iids id.IntegrationIDList) ([]*workspace.Workspace, error) {
71+
integrationIDs := make(workspace.IntegrationIDList, len(iids))
72+
for i, iid := range iids {
73+
integrationIDs[i] = workspace.IntegrationID(iid)
74+
}
75+
list, err := a.internal.FindByIntegrations(ctx, integrationIDs)
76+
return []*workspace.Workspace(list), err
77+
}
78+
79+
func (a *workspaceAdapter) CheckWorkspaceAliasUnique(ctx context.Context, wid id.WorkspaceID, alias string) error {
80+
// Internal implementation doesn't have this method
81+
// We need to check manually
82+
ws, err := a.internal.FindByAlias(ctx, alias)
83+
if err != nil {
84+
// If not found, alias is unique
85+
return nil
86+
}
87+
if ws.ID() == workspace.ID(wid) {
88+
// Same workspace, alias is unique
89+
return nil
90+
}
91+
// Different workspace with same alias
92+
return internalRepo.ErrOperationDenied
93+
}
94+
95+
func (a *workspaceAdapter) Create(ctx context.Context, ws *workspace.Workspace) error {
96+
return a.internal.Create(ctx, ws)
97+
}
98+
99+
func (a *workspaceAdapter) Save(ctx context.Context, ws *workspace.Workspace) error {
100+
return a.internal.Save(ctx, ws)
101+
}
102+
103+
func (a *workspaceAdapter) SaveAll(ctx context.Context, wsList []*workspace.Workspace) error {
104+
list := workspace.List(wsList)
105+
return a.internal.SaveAll(ctx, list)
106+
}
107+
108+
func (a *workspaceAdapter) Remove(ctx context.Context, wid id.WorkspaceID) error {
109+
return a.internal.Remove(ctx, workspace.ID(wid))
110+
}
111+
112+
func (a *workspaceAdapter) RemoveAll(ctx context.Context, ids id.WorkspaceIDList) error {
113+
wsIDs := make(workspace.IDList, len(ids))
114+
for i, wid := range ids {
115+
wsIDs[i] = workspace.ID(wid)
116+
}
117+
return a.internal.RemoveAll(ctx, wsIDs)
118+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package infrastructure
2+
3+
import (
4+
"github.com/reearth/reearth-accounts/server/internal/infrastructure/memory"
5+
"github.com/reearth/reearth-accounts/server/pkg/repo"
6+
)
7+
8+
// NewMemoryUser creates a new in-memory User repository
9+
func NewMemoryUser() repo.User {
10+
internal := memory.NewUser()
11+
return NewUserAdapter(internal)
12+
}
13+
14+
// NewMemoryWorkspace creates a new in-memory Workspace repository
15+
func NewMemoryWorkspace() repo.Workspace {
16+
internal := memory.NewWorkspace()
17+
return NewWorkspaceAdapter(internal)
18+
}

server/pkg/infrastructure/mongo.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package infrastructure
2+
3+
import (
4+
"github.com/reearth/reearth-accounts/server/internal/infrastructure/mongo"
5+
"github.com/reearth/reearth-accounts/server/pkg/repo"
6+
"github.com/reearth/reearthx/mongox"
7+
)
8+
9+
// NewMongoUser creates a new MongoDB-backed User repository
10+
func NewMongoUser(client *mongox.Client) repo.User {
11+
internal := mongo.NewUser(client)
12+
return NewUserAdapter(internal)
13+
}
14+
15+
// NewMongoWorkspace creates a new MongoDB-backed Workspace repository
16+
func NewMongoWorkspace(client *mongox.Client) repo.Workspace {
17+
internal := mongo.NewWorkspace(client)
18+
return NewWorkspaceAdapter(internal)
19+
}

0 commit comments

Comments
 (0)