Skip to content

Commit d4fb5c5

Browse files
authored
Merge pull request #66 from jiaozifs/feat/add_pagination
Feat/add pagination
2 parents b39bc97 + 68e76e4 commit d4fb5c5

File tree

13 files changed

+866
-162
lines changed

13 files changed

+866
-162
lines changed

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ components:
2626
schema:
2727
type: string
2828

29-
PaginationAfter:
29+
PaginationBranchAfter:
3030
in: query
3131
name: after
3232
description: return items after this value
3333
schema:
3434
type: string
35+
36+
PaginationRepoAfter:
37+
in: query
38+
name: after
39+
description: return items after this value
40+
schema:
41+
type: string
42+
format: date-time
3543

3644
PaginationAmount:
3745
in: query
@@ -186,6 +194,18 @@ components:
186194
properties:
187195
Description:
188196
type: string
197+
RepositoryList:
198+
type: object
199+
required:
200+
- pagination
201+
- results
202+
properties:
203+
pagination:
204+
$ref: "#/components/schemas/Pagination"
205+
results:
206+
type: array
207+
items:
208+
$ref: "#/components/schemas/Repository"
189209
Repository:
190210
type: object
191211
required:
@@ -1293,20 +1313,16 @@ paths:
12931313
operationId: listRepository
12941314
summary: list repository in specific owner
12951315
parameters:
1296-
- in: query
1297-
name: repoPrefix
1298-
required: false
1299-
schema:
1300-
type: string
1316+
- $ref: "#/components/parameters/PaginationPrefix"
1317+
- $ref: "#/components/parameters/PaginationRepoAfter"
1318+
- $ref: "#/components/parameters/PaginationAmount"
13011319
responses:
13021320
200:
13031321
description: repository list
13041322
content:
13051323
application/json:
13061324
schema:
1307-
type: array
1308-
items:
1309-
$ref: "#/components/schemas/Repository"
1325+
$ref: "#/components/schemas/RepositoryList"
13101326
400:
13111327
description: ValidationError
13121328
401:
@@ -1320,15 +1336,17 @@ paths:
13201336
- repo
13211337
operationId: listRepositoryOfAuthenticatedUser"
13221338
summary: list repository
1339+
parameters:
1340+
- $ref: "#/components/parameters/PaginationPrefix"
1341+
- $ref: "#/components/parameters/PaginationRepoAfter"
1342+
- $ref: "#/components/parameters/PaginationAmount"
13231343
responses:
13241344
200:
13251345
description: list repository
13261346
content:
13271347
application/json:
13281348
schema:
1329-
type: array
1330-
items:
1331-
$ref: "#/components/schemas/Repository"
1349+
$ref: "#/components/schemas/RepositoryList"
13321350
400:
13331351
description: ValidationError
13341352
401:
@@ -1379,6 +1397,10 @@ paths:
13791397
- branches
13801398
operationId: listBranches
13811399
summary: list branches
1400+
parameters:
1401+
- $ref: "#/components/parameters/PaginationPrefix"
1402+
- $ref: "#/components/parameters/PaginationBranchAfter"
1403+
- $ref: "#/components/parameters/PaginationAmount"
13821404
responses:
13831405
200:
13841406
description: branch list

config/default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var defaultCfg = Config{
3939
LoginCookieNames []string `mapstructure:"login_cookie_names"`
4040
LogoutURL string `mapstructure:"logout_url"`
4141
}{RBAC: AuthRBACSimplified,
42-
LoginURL: "api/v1/login",
42+
LoginURL: "api/v1/auth/login",
4343
LoginFailedMessage: "",
4444
LoginCookieNames: nil,
4545
LogoutURL: "auth/logout",

controller/branch_ctl.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/jiaozifs/jiaozifs/api"
1313
"github.com/jiaozifs/jiaozifs/auth"
1414
"github.com/jiaozifs/jiaozifs/models"
15+
"github.com/jiaozifs/jiaozifs/utils"
1516
"github.com/jiaozifs/jiaozifs/utils/hash"
1617
"go.uber.org/fx"
1718
)
@@ -47,7 +48,7 @@ type BranchController struct {
4748
Repo models.IRepo
4849
}
4950

50-
func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string) {
51+
func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.ListBranchesParams) {
5152
operator, err := auth.GetOperator(ctx)
5253
if err != nil {
5354
w.Error(err)
@@ -60,25 +61,39 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
6061
return
6162
}
6263

64+
if operator.Name != owner.Name {
65+
w.Forbidden()
66+
return
67+
}
68+
6369
repository, err := bct.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
6470
if err != nil {
6571
w.Error(err)
6672
return
6773
}
6874

69-
if operator.Name != owner.Name {
70-
w.Forbidden()
71-
return
75+
listBranchParams := models.NewListBranchParams()
76+
if params.Prefix != nil && len(*params.Prefix) > 0 {
77+
listBranchParams.SetName(*params.Prefix, models.PrefixMatch)
78+
}
79+
if params.After != nil && len(*params.After) > 0 {
80+
listBranchParams.SetAfter(*params.After)
81+
}
82+
pageAmount := utils.IntValue(params.Amount)
83+
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
84+
listBranchParams.SetAmount(utils.DefaultMaxPerPage)
85+
} else {
86+
listBranchParams.SetAmount(pageAmount)
7287
}
7388

74-
branches, err := bct.Repo.BranchRepo().List(ctx, models.NewListBranchParams().SetRepositoryID(repository.ID))
89+
branches, hasMore, err := bct.Repo.BranchRepo().List(ctx, listBranchParams.SetRepositoryID(repository.ID))
7590
if err != nil {
7691
w.Error(err)
7792
return
7893
}
79-
var apiBranches []api.Branch
94+
results := make([]api.Branch, 0, len(branches))
8095
for _, branch := range branches {
81-
branch := api.Branch{
96+
r := api.Branch{
8297
CommitHash: branch.CommitHash.Hex(),
8398
CreatedAt: branch.CreatedAt,
8499
CreatorID: branch.CreatorID,
@@ -88,9 +103,19 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
88103
RepositoryID: branch.RepositoryID,
89104
UpdatedAt: branch.UpdatedAt,
90105
}
91-
apiBranches = append(apiBranches, branch)
92-
}
93-
w.JSON(api.BranchList{Results: apiBranches})
106+
results = append(results, r)
107+
}
108+
pagMag := utils.PaginationFor(hasMore, results, "Name")
109+
pagination := api.Pagination{
110+
HasMore: pagMag.HasMore,
111+
MaxPerPage: pagMag.MaxPerPage,
112+
NextOffset: pagMag.NextOffset,
113+
Results: pagMag.Results,
114+
}
115+
w.JSON(api.BranchList{
116+
Pagination: pagination,
117+
Results: results,
118+
})
94119
}
95120

96121
func (bct BranchController) CreateBranch(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, body api.CreateBranchJSONRequestBody, ownerName string, repositoryName string) {

controller/repository_ctl.go

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,57 @@ type RepositoryController struct {
5656
PublicStorageConfig params.AdapterConfig
5757
}
5858

59-
func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request) {
59+
func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, params api.ListRepositoryOfAuthenticatedUserParams) {
6060
operator, err := auth.GetOperator(ctx)
6161
if err != nil {
6262
w.Error(err)
6363
return
6464
}
6565

66-
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, models.NewListRepoParams().SetOwnerID(operator.ID)) //operator is owner
66+
listRepoParams := models.NewListRepoParams()
67+
if params.Prefix != nil && len(*params.Prefix) > 0 {
68+
listRepoParams.SetName(*params.Prefix, models.PrefixMatch)
69+
}
70+
if params.After != nil {
71+
listRepoParams.SetAfter(*params.After)
72+
}
73+
pageAmount := utils.IntValue(params.Amount)
74+
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
75+
listRepoParams.SetAmount(utils.DefaultMaxPerPage)
76+
} else {
77+
listRepoParams.SetAmount(pageAmount)
78+
}
79+
80+
repositories, hasMore, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listRepoParams.
81+
SetOwnerID(operator.ID))
6782
if err != nil {
6883
w.Error(err)
6984
return
7085
}
71-
w.JSON(repositories)
86+
results := make([]api.Repository, 0, len(repositories))
87+
for _, repo := range repositories {
88+
r := api.Repository{
89+
CreatedAt: repo.CreatedAt,
90+
CreatorID: repo.CreatorID,
91+
Description: repo.Description,
92+
Head: repo.HEAD,
93+
ID: repo.ID,
94+
Name: repo.Name,
95+
UpdatedAt: repo.UpdatedAt,
96+
}
97+
results = append(results, r)
98+
}
99+
pagMag := utils.PaginationFor(hasMore, results, "UpdatedAt")
100+
pagination := api.Pagination{
101+
HasMore: pagMag.HasMore,
102+
MaxPerPage: pagMag.MaxPerPage,
103+
NextOffset: pagMag.NextOffset,
104+
Results: pagMag.Results,
105+
}
106+
w.JSON(api.RepositoryList{
107+
Pagination: pagination,
108+
Results: results,
109+
})
72110
}
73111

74112
func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, params api.ListRepositoryParams) {
@@ -88,16 +126,49 @@ func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w
88126
return
89127
}
90128

91-
listParams := models.NewListRepoParams().SetOwnerID(owner.ID)
92-
if params.RepoPrefix != nil && len(*params.RepoPrefix) > 0 {
93-
listParams.SetName(*params.RepoPrefix, models.PrefixMatch)
129+
listRepoParams := models.NewListRepoParams().SetOwnerID(owner.ID)
130+
if params.Prefix != nil && len(*params.Prefix) > 0 {
131+
listRepoParams.SetName(*params.Prefix, models.PrefixMatch)
94132
}
95-
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listParams)
133+
if params.After != nil {
134+
listRepoParams.SetAfter(*params.After)
135+
}
136+
pageAmount := utils.IntValue(params.Amount)
137+
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
138+
listRepoParams.SetAmount(utils.DefaultMaxPerPage)
139+
} else {
140+
listRepoParams.SetAmount(pageAmount)
141+
}
142+
143+
repositories, hasMore, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listRepoParams)
96144
if err != nil {
97145
w.Error(err)
98146
return
99147
}
100-
w.JSON(repositories)
148+
results := make([]api.Repository, 0, len(repositories))
149+
for _, repo := range repositories {
150+
r := api.Repository{
151+
CreatedAt: repo.CreatedAt,
152+
CreatorID: repo.CreatorID,
153+
Description: repo.Description,
154+
Head: repo.HEAD,
155+
ID: repo.ID,
156+
Name: repo.Name,
157+
UpdatedAt: repo.UpdatedAt,
158+
}
159+
results = append(results, r)
160+
}
161+
pagMag := utils.PaginationFor(hasMore, results, "UpdatedAt")
162+
pagination := api.Pagination{
163+
HasMore: pagMag.HasMore,
164+
MaxPerPage: pagMag.MaxPerPage,
165+
NextOffset: pagMag.NextOffset,
166+
Results: pagMag.Results,
167+
}
168+
w.JSON(api.RepositoryList{
169+
Pagination: pagination,
170+
Results: results,
171+
})
101172
}
102173

103174
func (repositoryCtl RepositoryController) CreateRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, body api.CreateRepositoryJSONRequestBody) {

0 commit comments

Comments
 (0)