org.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package org
  2. import (
  3. "gitote/gitote/models"
  4. "gitote/gitote/pkg/context"
  5. "gitote/gitote/routes/api/v1/convert"
  6. "gitote/gitote/routes/api/v1/user"
  7. api "gitlab.com/gitote/go-gitote-client"
  8. )
  9. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *models.User) {
  10. if c.Written() {
  11. return
  12. }
  13. org := &models.User{
  14. Name: apiForm.UserName,
  15. FullName: apiForm.FullName,
  16. Description: apiForm.Description,
  17. Website: apiForm.Website,
  18. Location: apiForm.Location,
  19. IsActive: true,
  20. Type: models.USER_TYPE_ORGANIZATION,
  21. }
  22. if err := models.CreateOrganization(org, user); err != nil {
  23. if models.IsErrUserAlreadyExist(err) ||
  24. models.IsErrNameReserved(err) ||
  25. models.IsErrNamePatternNotAllowed(err) {
  26. c.Error(422, "", err)
  27. } else {
  28. c.Error(500, "CreateOrganization", err)
  29. }
  30. return
  31. }
  32. c.JSON(201, convert.ToOrganization(org))
  33. }
  34. func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
  35. if err := u.GetOrganizations(all); err != nil {
  36. c.Error(500, "GetOrganizations", err)
  37. return
  38. }
  39. apiOrgs := make([]*api.Organization, len(u.Orgs))
  40. for i := range u.Orgs {
  41. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  42. }
  43. c.JSON(200, &apiOrgs)
  44. }
  45. func ListMyOrgs(c *context.APIContext) {
  46. listUserOrgs(c, c.User, true)
  47. }
  48. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  49. CreateOrgForUser(c, apiForm, c.User)
  50. }
  51. func ListUserOrgs(c *context.APIContext) {
  52. u := user.GetUserByParams(c)
  53. if c.Written() {
  54. return
  55. }
  56. listUserOrgs(c, u, false)
  57. }
  58. func Get(c *context.APIContext) {
  59. c.JSON(200, convert.ToOrganization(c.Org.Organization))
  60. }
  61. func Edit(c *context.APIContext, form api.EditOrgOption) {
  62. org := c.Org.Organization
  63. if !org.IsOwnedBy(c.User.ID) {
  64. c.Status(403)
  65. return
  66. }
  67. org.FullName = form.FullName
  68. org.Description = form.Description
  69. org.Website = form.Website
  70. org.Location = form.Location
  71. if err := models.UpdateUser(org); err != nil {
  72. c.Error(500, "UpdateUser", err)
  73. return
  74. }
  75. c.JSON(200, convert.ToOrganization(org))
  76. }