Skip to content

Commit 1b90692

Browse files
shashvat-kediatechknowlogick
authored andcommitted
New API routes added (#5594)
* New API routes added * Comments added * Build fix * swagger_v1_json.tmpl without new line character * Typo fix * Code review changes * Code review changes * Add copyright * Add copyright * Add copyright * Update per @lafriks feedback * Update org.go * Update user.go * Update user.go * make fmt
1 parent b9f8737 commit 1b90692

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

models/user.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -1358,7 +1359,7 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
13581359
return nil, 0, fmt.Errorf("Count: %v", err)
13591360
}
13601361

1361-
if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum {
1362+
if opts.PageSize == 0 || opts.PageSize > setting.UI.ExplorePagingNum {
13621363
opts.PageSize = setting.UI.ExplorePagingNum
13631364
}
13641365
if opts.Page <= 0 {
@@ -1368,11 +1369,13 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
13681369
opts.OrderBy = SearchOrderByAlphabetically
13691370
}
13701371

1372+
sess := x.Where(cond)
1373+
if opts.PageSize > 0 {
1374+
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
1375+
}
1376+
13711377
users = make([]*User, 0, opts.PageSize)
1372-
return users, count, x.Where(cond).
1373-
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
1374-
OrderBy(opts.OrderBy.String()).
1375-
Find(&users)
1378+
return users, count, sess.OrderBy(opts.OrderBy.String()).Find(&users)
13761379
}
13771380

13781381
// GetStarredRepos returns the repos starred by a particular user

routers/api/v1/admin/org.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -66,3 +67,31 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
6667

6768
ctx.JSON(201, convert.ToOrganization(org))
6869
}
70+
71+
//GetAllOrgs API for getting information of all the organizations
72+
func GetAllOrgs(ctx *context.APIContext) {
73+
// swagger:operation GET /admin/orgs admin adminGetAllOrgs
74+
// ---
75+
// summary: List all organizations
76+
// produces:
77+
// - application/json
78+
// responses:
79+
// "200":
80+
// "$ref": "#/responses/OrganizationList"
81+
// "403":
82+
// "$ref": "#/responses/forbidden"
83+
users, _, err := models.SearchUsers(&models.SearchUserOptions{
84+
Type: models.UserTypeOrganization,
85+
OrderBy: models.SearchOrderByAlphabetically,
86+
PageSize: -1,
87+
})
88+
if err != nil {
89+
ctx.Error(500, "SearchOrganizations", err)
90+
return
91+
}
92+
orgs := make([]*api.Organization, len(users))
93+
for i := range users {
94+
orgs[i] = convert.ToOrganization(users[i])
95+
}
96+
ctx.JSON(200, &orgs)
97+
}

routers/api/v1/admin/user.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -291,3 +292,27 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
291292

292293
ctx.Status(204)
293294
}
295+
296+
//GetAllUsers API for getting information of all the users
297+
func GetAllUsers(ctx *context.APIContext) {
298+
// swagger:operation GET /admin/users admin adminGetAllUsers
299+
// ---
300+
// summary: List all users
301+
// produces:
302+
// - application/json
303+
// responses:
304+
// "200":
305+
// "$ref": "#/responses/UserList"
306+
// "403":
307+
// "$ref": "#/responses/forbidden"
308+
users, _, err := models.SearchUsers(&models.SearchUserOptions{
309+
Type: models.UserTypeIndividual,
310+
OrderBy: models.SearchOrderByAlphabetically,
311+
PageSize: -1,
312+
})
313+
if err != nil {
314+
ctx.Error(500, "SearchUsers", err)
315+
return
316+
}
317+
ctx.JSON(200, &users)
318+
}

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ func RegisterRoutes(m *macaron.Macaron) {
671671
})
672672

673673
m.Group("/admin", func() {
674+
m.Get("/orgs", admin.GetAllOrgs)
674675
m.Group("/users", func() {
676+
m.Get("", admin.GetAllUsers)
675677
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
676678
m.Group("/:username", func() {
677679
m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
@@ -680,6 +682,7 @@ func RegisterRoutes(m *macaron.Macaron) {
680682
m.Post("", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
681683
m.Delete("/:id", admin.DeleteUserPublicKey)
682684
})
685+
m.Get("/orgs", org.ListUserOrgs)
683686
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
684687
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
685688
})

templates/swagger/v1_json.tmpl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,45 @@
2323
},
2424
"basePath": "{{AppSubUrl}}/api/v1",
2525
"paths": {
26+
"/admin/orgs": {
27+
"get": {
28+
"produces": [
29+
"application/json"
30+
],
31+
"tags": [
32+
"admin"
33+
],
34+
"summary": "List all organizations",
35+
"operationId": "adminGetAllOrgs",
36+
"responses": {
37+
"200": {
38+
"$ref": "#/responses/OrganizationList"
39+
},
40+
"403": {
41+
"$ref": "#/responses/forbidden"
42+
}
43+
}
44+
}
45+
},
2646
"/admin/users": {
47+
"get": {
48+
"produces": [
49+
"application/json"
50+
],
51+
"tags": [
52+
"admin"
53+
],
54+
"summary": "List all users",
55+
"operationId": "adminGetAllUsers",
56+
"responses": {
57+
"200": {
58+
"$ref": "#/responses/UserList"
59+
},
60+
"403": {
61+
"$ref": "#/responses/forbidden"
62+
}
63+
}
64+
},
2765
"post": {
2866
"consumes": [
2967
"application/json"

0 commit comments

Comments
 (0)