|
| 1 | +package adminController |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gin-gonic/gin" |
| 5 | + "wejh-go/app/apiException" |
| 6 | + "wejh-go/app/models" |
| 7 | + "wejh-go/app/services/themeServices" |
| 8 | + "wejh-go/app/utils" |
| 9 | +) |
| 10 | + |
| 11 | +type CreateThemeData struct { |
| 12 | + Name string `json:"name" binding:"required"` |
| 13 | + Type string `json:"type" binding:"required"` |
| 14 | + ThemeConfig string `json:"theme_config"` |
| 15 | +} |
| 16 | + |
| 17 | +// 管理员创建主题色 |
| 18 | +func CreateTheme(c *gin.Context) { |
| 19 | + var data CreateThemeData |
| 20 | + err := c.ShouldBindJSON(&data) |
| 21 | + if err != nil { |
| 22 | + _ = c.AbortWithError(200, apiException.ParamError) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + record := models.Theme{ |
| 27 | + Name: data.Name, |
| 28 | + Type: data.Type, |
| 29 | + ThemeConfig: data.ThemeConfig, |
| 30 | + } |
| 31 | + themeID, err := themeServices.CreateTheme(record) |
| 32 | + if err != nil { |
| 33 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 34 | + return |
| 35 | + } |
| 36 | + if data.Type == "all" { |
| 37 | + studentIDs, err := themeServices.GetAllStudentIDs() |
| 38 | + if err != nil { |
| 39 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + err = themeServices.AddThemePermission(themeID, studentIDs) |
| 44 | + if err != nil { |
| 45 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 46 | + return |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + utils.JsonSuccessResponse(c, nil) |
| 51 | +} |
| 52 | + |
| 53 | +type UpdateThemeData struct { |
| 54 | + ID int `json:"id" binding:"required"` |
| 55 | + Name string `json:"name" binding:"required"` |
| 56 | + ThemeConfig string `json:"theme_config" binding:"required"` |
| 57 | +} |
| 58 | + |
| 59 | +// 管理员更新主题色 |
| 60 | +func UpdateTheme(c *gin.Context) { |
| 61 | + var data UpdateThemeData |
| 62 | + err := c.ShouldBindJSON(&data) |
| 63 | + if err != nil { |
| 64 | + _ = c.AbortWithError(200, apiException.ParamError) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + record := models.Theme{ |
| 69 | + Name: data.Name, |
| 70 | + ThemeConfig: data.ThemeConfig, |
| 71 | + } |
| 72 | + err = themeServices.UpdateTheme(data.ID, record) |
| 73 | + if err != nil { |
| 74 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + utils.JsonSuccessResponse(c, nil) |
| 79 | +} |
| 80 | + |
| 81 | +// 管理员获取主题色列表 |
| 82 | +func GetThemes(c *gin.Context) { |
| 83 | + themes, err := themeServices.GetThemes() |
| 84 | + if err != nil { |
| 85 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + utils.JsonSuccessResponse(c, gin.H{ |
| 90 | + "theme_list": themes, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +type DeleteThemeData struct { |
| 95 | + ID int `form:"id" binding:"required"` |
| 96 | +} |
| 97 | + |
| 98 | +// 管理员根据id删除主题色 |
| 99 | +func DeleteTheme(c *gin.Context) { |
| 100 | + var data DeleteThemeData |
| 101 | + err := c.ShouldBindQuery(&data) |
| 102 | + if err != nil { |
| 103 | + _ = c.AbortWithError(200, apiException.ParamError) |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + err = themeServices.CheckThemeExist(data.ID) |
| 108 | + if err != nil { |
| 109 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + err = themeServices.DeleteTheme(data.ID) |
| 114 | + if err != nil { |
| 115 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 116 | + return |
| 117 | + } |
| 118 | + utils.JsonSuccessResponse(c, nil) |
| 119 | +} |
| 120 | + |
| 121 | +type AddThemePermissionData struct { |
| 122 | + ThemeID int `json:"theme_id" binding:"required"` |
| 123 | + StudentID []string `json:"student_id" binding:"required"` |
| 124 | +} |
| 125 | + |
| 126 | +// 管理员添加用户主题色权限 |
| 127 | +func AddThemePermission(c *gin.Context) { |
| 128 | + var data AddThemePermissionData |
| 129 | + err := c.ShouldBindJSON(&data) |
| 130 | + if err != nil { |
| 131 | + _ = c.AbortWithError(200, apiException.ParamError) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + err = themeServices.CheckThemeExist(data.ThemeID) |
| 136 | + if err != nil { |
| 137 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + err = themeServices.AddThemePermission(data.ThemeID, data.StudentID) |
| 142 | + if err != nil { |
| 143 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 144 | + return |
| 145 | + } |
| 146 | + utils.JsonSuccessResponse(c, nil) |
| 147 | +} |
| 148 | + |
| 149 | +type GetThemePermissionData struct { |
| 150 | + StudentID string `form:"student_id" binding:"required"` |
| 151 | +} |
| 152 | + |
| 153 | +// 管理员根据学号查询用户主题色权限 |
| 154 | +func GetThemePermission(c *gin.Context) { |
| 155 | + var data GetThemePermissionData |
| 156 | + err := c.ShouldBindQuery(&data) |
| 157 | + if err != nil { |
| 158 | + _ = c.AbortWithError(200, apiException.ParamError) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + themePermission, err := themeServices.GetThemePermissionByStudentID(data.StudentID) |
| 163 | + if err != nil { |
| 164 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + themeNames, err := themeServices.GetThemeNameByID(themePermission) |
| 169 | + if err != nil { |
| 170 | + _ = c.AbortWithError(200, apiException.ServerError) |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + utils.JsonSuccessResponse(c, gin.H{ |
| 175 | + "theme_name": themeNames, |
| 176 | + }) |
| 177 | +} |
0 commit comments