Example #1
0
func panelCreate(c *gin.Context) {
	panel := types.Panel{}
	response := gin.H{"code": http.StatusOK}
	defer c.JSON(http.StatusOK, response)
	if err := c.BindJSON(&panel); err != nil {
		response["code"] = http.StatusBadRequest
		response["message"] = err.Error()
		return
	}
	if !mydb.Table("panel").Where("user_id = ? and name = ?", GetUser(c).ID, panel.Name).First(&panel).RecordNotFound() {
		response["code"] = http.StatusBadRequest
		response["message"] = "panel already exists"
		return
	}
	panel.UserID = GetUser(c).ID
	if err := mydb.Select("name", "thumbnail", "user_id").Save(&panel).Error; err != nil {
		response["code"] = http.StatusInternalServerError
		response["message"] = err.Error()
		return
	}
	mydb.Model(&panel).Association("Charts").Replace(panel.Charts)
	mydb.Model(&panel).Find(&panel)
	for _, chart := range panel.Charts {
		mydb.Model(&chart).Related(&chart.Elements)
	}
	response["panel"] = panel
}
Example #2
0
func Favor(c *gin.Context, favor int) {
	id, _ := strconv.Atoi(c.Param("id"))
	user_id := GetUser(c).ID
	panel := types.Panel{}
	response := gin.H{}
	defer c.JSON(http.StatusOK, response)
	if mydb.Table("panel").Where("id = ? and user_id = ?", id, user_id).First(&panel).RecordNotFound() {
		response["code"] = http.StatusNotFound
		response["message"] = "panel not found"
		return
	}
	panel.Favor = favor
	mydb.Table("panel").Select("favor").Save(&panel)
	response["code"] = http.StatusOK
	response["panel"] = panel
}