Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit bb28840

Browse files
author
Noah Lee
authored
Refactoring the PermInteractor (#345)
1 parent 389fedb commit bb28840

File tree

6 files changed

+134
-20
lines changed

6 files changed

+134
-20
lines changed

internal/interactor/interface.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ type (
1616
UserStore
1717
ChatUserStore
1818
RepoStore
19-
20-
ListPermsOfRepo(ctx context.Context, r *ent.Repo, q string, page, perPage int) ([]*ent.Perm, error)
21-
FindPermOfRepo(ctx context.Context, r *ent.Repo, u *ent.User) (*ent.Perm, error)
22-
CreatePerm(ctx context.Context, p *ent.Perm) (*ent.Perm, error)
23-
UpdatePerm(ctx context.Context, p *ent.Perm) (*ent.Perm, error)
24-
DeletePermsOfUserLessThanSyncedAt(ctx context.Context, u *ent.User, t time.Time) (int, error)
19+
PermStore
2520

2621
CountDeployments(ctx context.Context) (int, error)
2722
SearchDeployments(ctx context.Context, u *ent.User, s []deployment.Status, owned bool, from time.Time, to time.Time, page, perPage int) ([]*ent.Deployment, error)
@@ -67,6 +62,22 @@ type (
6762
CheckNotificationRecordOfEvent(ctx context.Context, e *ent.Event) bool
6863
}
6964

65+
// PermStore defines operations for working with perms.
66+
PermStore interface {
67+
ListPermsOfRepo(ctx context.Context, r *ent.Repo, opt *ListPermsOfRepoOptions) ([]*ent.Perm, error)
68+
FindPermOfRepo(ctx context.Context, r *ent.Repo, u *ent.User) (*ent.Perm, error)
69+
CreatePerm(ctx context.Context, p *ent.Perm) (*ent.Perm, error)
70+
UpdatePerm(ctx context.Context, p *ent.Perm) (*ent.Perm, error)
71+
DeletePermsOfUserLessThanSyncedAt(ctx context.Context, u *ent.User, t time.Time) (int, error)
72+
}
73+
74+
ListPermsOfRepoOptions struct {
75+
ListOptions
76+
77+
// Query search the 'login' contains the query.
78+
Query string
79+
}
80+
7081
SCM interface {
7182
UserSCM
7283
RepoSCM

internal/interactor/mock/pkg.go

Lines changed: 102 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/store/perm.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ import (
55
"fmt"
66
"time"
77

8+
i "github.com/gitploy-io/gitploy/internal/interactor"
89
"github.com/gitploy-io/gitploy/model/ent"
910
"github.com/gitploy-io/gitploy/model/ent/perm"
1011
"github.com/gitploy-io/gitploy/model/ent/repo"
1112
"github.com/gitploy-io/gitploy/model/ent/user"
1213
"github.com/gitploy-io/gitploy/pkg/e"
1314
)
1415

15-
func (s *Store) ListPermsOfRepo(ctx context.Context, r *ent.Repo, q string, page, perPage int) ([]*ent.Perm, error) {
16+
func (s *Store) ListPermsOfRepo(ctx context.Context, r *ent.Repo, opt *i.ListPermsOfRepoOptions) ([]*ent.Perm, error) {
1617
perms, err := s.c.Perm.
1718
Query().
1819
Where(
1920
perm.And(
2021
perm.HasRepoWith(repo.IDEQ(r.ID)),
21-
perm.HasUserWith(user.LoginContains(q)),
22+
perm.HasUserWith(user.LoginContains(opt.Query)),
2223
),
2324
).
2425
WithRepo().
2526
WithUser().
26-
Limit(perPage).
27-
Offset(offset(page, perPage)).
27+
Limit(opt.PerPage).
28+
Offset(offset(opt.Page, opt.PerPage)).
2829
All(ctx)
2930
if err != nil {
3031
return nil, e.NewError(e.ErrorCodeInternalError, err)

internal/server/api/v1/repos/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type (
1414
Interactor interface {
1515
FindUserByID(ctx context.Context, id int64) (*ent.User, error)
1616

17-
ListPermsOfRepo(ctx context.Context, r *ent.Repo, q string, page, perPage int) ([]*ent.Perm, error)
17+
ListPermsOfRepo(ctx context.Context, r *ent.Repo, opt *i.ListPermsOfRepoOptions) ([]*ent.Perm, error)
1818
FindPermOfRepo(ctx context.Context, r *ent.Repo, u *ent.User) (*ent.Perm, error)
1919

2020
ListReposOfUser(ctx context.Context, u *ent.User, opt *i.ListReposOfUserOptions) ([]*ent.Repo, error)

internal/server/api/v1/repos/mock/interactor.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/api/v1/repos/perm_list.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/gin-gonic/gin"
88
"go.uber.org/zap"
99

10+
i "github.com/gitploy-io/gitploy/internal/interactor"
1011
gb "github.com/gitploy-io/gitploy/internal/server/global"
1112
"github.com/gitploy-io/gitploy/model/ent"
1213
"github.com/gitploy-io/gitploy/pkg/e"
@@ -42,7 +43,10 @@ func (s *PermAPI) List(c *gin.Context) {
4243
perPage = 100
4344
}
4445

45-
perms, err := s.i.ListPermsOfRepo(ctx, re, q, page, perPage)
46+
perms, err := s.i.ListPermsOfRepo(ctx, re, &i.ListPermsOfRepoOptions{
47+
ListOptions: i.ListOptions{Page: page, PerPage: perPage},
48+
Query: q,
49+
})
4650
if err != nil {
4751
s.log.Check(gb.GetZapLogLevel(err), "Failed to list permissions.").Write(zap.Error(err))
4852
gb.ResponseWithError(c, err)

0 commit comments

Comments
 (0)