| 
									
										
										
										
											2016-03-06 19:47:39 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2016-03-06 19:47:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 14:22:27 +08:00
										 |  |  | // POST /api/preferences/set-home-dash
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func SetHomeDashboard(c *m.ReqContext, cmd m.SavePreferencesCommand) Response { | 
					
						
							| 
									
										
										
										
											2016-03-17 14:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 14:35:06 +08:00
										 |  |  | 	cmd.UserId = c.UserId | 
					
						
							|  |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2016-03-17 14:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 14:35:06 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to set home dashboard", err) | 
					
						
							| 
									
										
										
										
											2016-03-17 14:35:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-17 14:22:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Home dashboard set") | 
					
						
							| 
									
										
										
										
											2016-03-17 14:22:27 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GET /api/user/preferences
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetUserPreferences(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | 	return getPreferencesFor(c.OrgId, c.UserId, 0) | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | func getPreferencesFor(orgID, userID, teamID int64) Response { | 
					
						
							|  |  |  | 	prefsQuery := m.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID} | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 	if err := bus.Dispatch(&prefsQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to get preferences", err) | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 	dto := dtos.Prefs{ | 
					
						
							|  |  |  | 		Theme:           prefsQuery.Result.Theme, | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		HomeDashboardID: prefsQuery.Result.HomeDashboardId, | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 		Timezone:        prefsQuery.Result.Timezone, | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, &dto) | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/user/preferences
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func UpdateUserPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response { | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | 	return updatePreferencesFor(c.OrgId, c.UserId, 0, &dtoCmd) | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) Response { | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	saveCmd := m.SavePreferencesCommand{ | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		UserId:          userID, | 
					
						
							|  |  |  | 		OrgId:           orgID, | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | 		TeamId:          teamId, | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 		Theme:           dtoCmd.Theme, | 
					
						
							|  |  |  | 		Timezone:        dtoCmd.Timezone, | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		HomeDashboardId: dtoCmd.HomeDashboardID, | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&saveCmd); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to save preferences", err) | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("Preferences updated") | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GET /api/org/preferences
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func GetOrgPreferences(c *m.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | 	return getPreferencesFor(c.OrgId, 0, 0) | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/org/preferences
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:54:50 +08:00
										 |  |  | func UpdateOrgPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response { | 
					
						
							| 
									
										
										
										
											2018-11-13 03:01:53 +08:00
										 |  |  | 	return updatePreferencesFor(c.OrgId, 0, 0, &dtoCmd) | 
					
						
							| 
									
										
										
										
											2016-04-02 08:34:30 +08:00
										 |  |  | } |