77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
|
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)
|
||
|
}
|