You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
8 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package admin
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/user"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. )
  15. // CreateOrg api for create organization
  16. func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
  17. // swagger:operation POST /admin/users/{username}/orgs admin adminCreateOrg
  18. // ---
  19. // summary: Create an organization
  20. // consumes:
  21. // - application/json
  22. // produces:
  23. // - application/json
  24. // parameters:
  25. // - name: username
  26. // in: path
  27. // description: username of the user that will own the created organization
  28. // type: string
  29. // required: true
  30. // - name: organization
  31. // in: body
  32. // required: true
  33. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  34. // responses:
  35. // "201":
  36. // "$ref": "#/responses/Organization"
  37. // "403":
  38. // "$ref": "#/responses/forbidden"
  39. // "422":
  40. // "$ref": "#/responses/validationError"
  41. u := user.GetUserByParams(ctx)
  42. if ctx.Written() {
  43. return
  44. }
  45. visibility := api.VisibleTypePublic
  46. if form.Visibility != "" {
  47. visibility = api.VisibilityModes[form.Visibility]
  48. }
  49. org := &models.User{
  50. Name: form.UserName,
  51. FullName: form.FullName,
  52. Description: form.Description,
  53. Website: form.Website,
  54. Location: form.Location,
  55. IsActive: true,
  56. Type: models.UserTypeOrganization,
  57. Visibility: visibility,
  58. }
  59. if err := models.CreateOrganization(org, u); err != nil {
  60. if models.IsErrUserAlreadyExist(err) ||
  61. models.IsErrNameReserved(err) ||
  62. models.IsErrNameCharsNotAllowed(err) ||
  63. models.IsErrNamePatternNotAllowed(err) {
  64. ctx.Error(http.StatusUnprocessableEntity, "", err)
  65. } else {
  66. ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
  67. }
  68. return
  69. }
  70. ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
  71. }
  72. //GetAllOrgs API for getting information of all the organizations
  73. func GetAllOrgs(ctx *context.APIContext) {
  74. // swagger:operation GET /admin/orgs admin adminGetAllOrgs
  75. // ---
  76. // summary: List all organizations
  77. // produces:
  78. // - application/json
  79. // parameters:
  80. // - name: page
  81. // in: query
  82. // description: page number of results to return (1-based)
  83. // type: integer
  84. // - name: limit
  85. // in: query
  86. // description: page size of results, maximum page size is 50
  87. // type: integer
  88. // responses:
  89. // "200":
  90. // "$ref": "#/responses/OrganizationList"
  91. // "403":
  92. // "$ref": "#/responses/forbidden"
  93. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  94. Type: models.UserTypeOrganization,
  95. OrderBy: models.SearchOrderByAlphabetically,
  96. ListOptions: utils.GetListOptions(ctx),
  97. Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate},
  98. })
  99. if err != nil {
  100. ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
  101. return
  102. }
  103. orgs := make([]*api.Organization, len(users))
  104. for i := range users {
  105. orgs[i] = convert.ToOrganization(users[i])
  106. }
  107. ctx.JSON(http.StatusOK, &orgs)
  108. }