| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/comments" | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) commentsGet(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := comments.GetCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	items, err := hs.commentsService.Get(c.Req.Context(), c.OrgID, c.SignedInUser, cmd) | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, comments.ErrPermissionDenied) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusForbidden, "permission denied", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "internal error", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 		"comments": items, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) commentsCreate(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := comments.CreateCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	if c.SignedInUser.UserID == 0 && !c.SignedInUser.HasRole(org.RoleAdmin) { | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 		return response.Error(http.StatusForbidden, "admin role required", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	comment, err := hs.commentsService.Create(c.Req.Context(), c.OrgID, c.SignedInUser, cmd) | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, comments.ErrPermissionDenied) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusForbidden, "permission denied", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "internal error", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 		"comment": comment, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |