From b51dfea18b0a1975b6d6786680cd3b94adb6319e Mon Sep 17 00:00:00 2001 From: coward Date: Thu, 11 Jul 2024 16:06:52 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E6=96=B0=E5=A2=9E=E4=BA=86=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E7=9B=B8=E5=85=B3=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/api/setting.go | 76 ++++++++++++++++++++++++++++++++++++++++++ http/param/setting.go | 8 +++++ http/router/root.go | 1 + http/router/setting.go | 18 ++++++++++ 4 files changed, 103 insertions(+) create mode 100644 http/api/setting.go create mode 100644 http/param/setting.go create mode 100644 http/router/setting.go diff --git a/http/api/setting.go b/http/api/setting.go new file mode 100644 index 0000000..9cafa91 --- /dev/null +++ b/http/api/setting.go @@ -0,0 +1,76 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "wireguard-ui/http/param" + "wireguard-ui/http/response" + "wireguard-ui/model" + "wireguard-ui/service" +) + +type setting struct{} + +func Setting() setting { + return setting{} +} + +// Set +// @description: 设置配置 +// @receiver setting +// @param c +func (setting) Set(c *gin.Context) { + var p param.SetSetting + if err := c.ShouldBind(&p); err != nil { + response.R(c).Validator(err) + return + } + + if err := service.Setting().SetData(&model.Setting{ + Code: p.Code, + Data: p.Data, + }); err != nil { + response.R(c).FailedWithError(err) + return + } + + response.R(c).OK() +} + +// Delete +// @description: 删除配置 +// @receiver setting +// @param c +func (setting) Delete(c *gin.Context) { + code := c.Param("code") + if code == "" || code == "undefined" { + response.R(c).FailedWithError("code不能为空") + return + } + + if err := service.Setting().Model(&model.Setting{}).Where("code = ?", code).Delete(&model.Setting{}).Error; err != nil { + response.R(c).FailedWithError("删除失败") + return + } + + response.R(c).OK() +} + +// GetSetting +// @description: 获取指定配置 +// @receiver setting +// @param c +func (setting) GetSetting(c *gin.Context) { + code := c.Query("code") + if code == "" { + response.R(c).FailedWithError("code不能为空") + return + } + + var data *model.Setting + if err := service.Setting().Model(&model.Setting{}).Where("code = ?", code).Take(&data).Error; err != nil { + response.R(c).FailedWithError("获取指定配置失败") + return + } + + response.R(c).OkWithData(data.Data) +} diff --git a/http/param/setting.go b/http/param/setting.go new file mode 100644 index 0000000..06a0d33 --- /dev/null +++ b/http/param/setting.go @@ -0,0 +1,8 @@ +package param + +// SetSetting +// @description: 添加/编辑设置 +type SetSetting struct { + Code string `json:"code" form:"code" binding:"required"` + Data string `json:"data" form:"data" binding:"required"` +} diff --git a/http/router/root.go b/http/router/root.go index fb20a2d..43a74c5 100644 --- a/http/router/root.go +++ b/http/router/root.go @@ -36,5 +36,6 @@ func Rooters() { LoginApi, UserApi, ClientApi, + SettingApi, ) } diff --git a/http/router/setting.go b/http/router/setting.go new file mode 100644 index 0000000..eb69407 --- /dev/null +++ b/http/router/setting.go @@ -0,0 +1,18 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "wireguard-ui/http/api" +) + +// SettingApi +// @description: 设置相关API +// @param r +func SettingApi(r *gin.RouterGroup) { + setting := r.Group("setting") + { + setting.POST("", api.Setting().Set) // 新增/编辑设置 + setting.DELETE("/:code", api.Setting().Delete) // 删除配置 + setting.GET("", api.Setting().GetSetting) // 获取指定配置 + } +}