| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-03 18:31:56 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-03-30 16:12:24 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2015-06-22 14:13:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | // GET /api/org
 | 
					
						
							| 
									
										
										
										
											2022-02-23 18:12:37 +08:00
										 |  |  | func (hs *HTTPServer) GetCurrentOrg(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return hs.getOrgHelper(c.Req.Context(), c.OrgId) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GET /api/orgs/:orgId
 | 
					
						
							| 
									
										
										
										
											2022-02-23 18:12:37 +08:00
										 |  |  | func (hs *HTTPServer) GetOrgByID(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "orgId is invalid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-02-23 18:12:37 +08:00
										 |  |  | 	return hs.getOrgHelper(c.Req.Context(), orgId) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 22:19:45 +08:00
										 |  |  | // GET /api/orgs/name/:name
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	org, err := hs.SQLStore.GetOrgByName(web.Params(c.Req)[":name"]) | 
					
						
							| 
									
										
										
										
											2021-01-07 18:36:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(404, "Organization not found", err) | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get organization", err) | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	result := models.OrgDetailsDTO{ | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | 		Id:   org.Id, | 
					
						
							|  |  |  | 		Name: org.Name, | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		Address: models.Address{ | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | 			Address1: org.Address1, | 
					
						
							|  |  |  | 			Address2: org.Address2, | 
					
						
							|  |  |  | 			City:     org.City, | 
					
						
							|  |  |  | 			ZipCode:  org.ZipCode, | 
					
						
							|  |  |  | 			State:    org.State, | 
					
						
							|  |  |  | 			Country:  org.Country, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, &result) | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-23 18:12:37 +08:00
										 |  |  | func (hs *HTTPServer) getOrgHelper(ctx context.Context, orgID int64) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetOrgByIdQuery{Id: orgID} | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-23 18:12:37 +08:00
										 |  |  | 	if err := hs.SQLStore.GetOrgById(ctx, &query); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(404, "Organization not found", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get organization", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 	org := query.Result | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	result := models.OrgDetailsDTO{ | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 		Id:   org.Id, | 
					
						
							|  |  |  | 		Name: org.Name, | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		Address: models.Address{ | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 			Address1: org.Address1, | 
					
						
							|  |  |  | 			Address2: org.Address2, | 
					
						
							|  |  |  | 			City:     org.City, | 
					
						
							|  |  |  | 			ZipCode:  org.ZipCode, | 
					
						
							|  |  |  | 			State:    org.State, | 
					
						
							|  |  |  | 			Country:  org.Country, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, &result) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | // POST /api/orgs
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := models.CreateOrgCommand{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-25 16:42:09 +08:00
										 |  |  | 	acEnabled := !hs.AccessControl.IsDisabled() | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	if !acEnabled && !(setting.AllowUserOrgCreate || c.IsGrafanaAdmin) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(403, "Access denied", nil) | 
					
						
							| 
									
										
										
										
											2015-03-30 16:12:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 16:12:24 +08:00
										 |  |  | 	cmd.UserId = c.UserId | 
					
						
							| 
									
										
										
										
											2022-04-26 01:07:11 +08:00
										 |  |  | 	if err := hs.SQLStore.CreateOrg(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgNameTaken) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(409, "Organization name taken", err) | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to create organization", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	metrics.MApiOrgCreate.Inc() | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, &util.DynMap{ | 
					
						
							| 
									
										
										
										
											2015-06-22 14:13:21 +08:00
										 |  |  | 		"orgId":   cmd.Result.Id, | 
					
						
							|  |  |  | 		"message": "Organization created", | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | // PUT /api/org
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) UpdateCurrentOrg(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	form := dtos.UpdateOrgForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	return hs.updateOrgHelper(c.Req.Context(), form, c.OrgId) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/orgs/:orgId
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) UpdateOrg(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	form := dtos.UpdateOrgForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "orgId is invalid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	return hs.updateOrgHelper(c.Req.Context(), form, orgId) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) updateOrgHelper(ctx context.Context, form dtos.UpdateOrgForm, orgID int64) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID} | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	if err := hs.SQLStore.UpdateOrg(ctx, &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgNameTaken) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(400, "Organization name taken", err) | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update organization", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Organization updated") | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | // PUT /api/org/address
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) UpdateCurrentOrgAddress(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	form := dtos.UpdateOrgAddressForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	return hs.updateOrgAddressHelper(c.Req.Context(), form, c.OrgId) | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/orgs/:orgId/address
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) UpdateOrgAddress(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	form := dtos.UpdateOrgAddressForm{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &form); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "orgId is invalid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	return hs.updateOrgAddressHelper(c.Req.Context(), form, orgId) | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) updateOrgAddressHelper(ctx context.Context, form dtos.UpdateOrgAddressForm, orgID int64) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	cmd := models.UpdateOrgAddressCommand{ | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		OrgId: orgID, | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		Address: models.Address{ | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 			Address1: form.Address1, | 
					
						
							|  |  |  | 			Address2: form.Address2, | 
					
						
							|  |  |  | 			City:     form.City, | 
					
						
							|  |  |  | 			State:    form.State, | 
					
						
							|  |  |  | 			ZipCode:  form.ZipCode, | 
					
						
							|  |  |  | 			Country:  form.Country, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	if err := hs.SQLStore.UpdateOrgAddress(ctx, &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update org address", err) | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Address updated") | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | // DELETE /api/orgs/:orgId
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | func (hs *HTTPServer) DeleteOrgByID(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "orgId is invalid", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-23 14:58:35 +08:00
										 |  |  | 	// before deleting an org, check if user does not belong to the current org
 | 
					
						
							|  |  |  | 	if c.OrgId == orgID { | 
					
						
							|  |  |  | 		return response.Error(400, "Can not delete org for current user", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 03:30:08 +08:00
										 |  |  | 	if err := hs.SQLStore.DeleteOrg(c.Req.Context(), &models.DeleteOrgCommand{Id: orgID}); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrOrgNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(404, "Failed to delete organization. ID not found", nil) | 
					
						
							| 
									
										
										
										
											2016-11-28 06:09:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update organization", err) | 
					
						
							| 
									
										
										
										
											2015-08-12 14:59:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Organization deleted") | 
					
						
							| 
									
										
										
										
											2015-08-12 14:59:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 18:45:31 +08:00
										 |  |  | func (hs *HTTPServer) SearchOrgs(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-08-12 15:59:07 +08:00
										 |  |  | 	perPage := c.QueryInt("perpage") | 
					
						
							|  |  |  | 	if perPage <= 0 { | 
					
						
							|  |  |  | 		perPage = 1000 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	page := c.QueryInt("page") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.SearchOrgsQuery{ | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | 		Query: c.Query("query"), | 
					
						
							|  |  |  | 		Name:  c.Query("name"), | 
					
						
							| 
									
										
										
										
											2020-08-12 15:59:07 +08:00
										 |  |  | 		Page:  page, | 
					
						
							|  |  |  | 		Limit: perPage, | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 18:45:31 +08:00
										 |  |  | 	if err := hs.SQLStore.SearchOrgs(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to search orgs", err) | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, query.Result) | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08:00
										 |  |  | } |