feat: 消息渠道
This commit is contained in:
parent
22dc1bbcc6
commit
9673f52efa
|
@ -10,4 +10,7 @@ var (
|
|||
NotFound = &BusinessErr{code: 404, message: "未找到"}
|
||||
|
||||
SystemError = &BusinessErr{code: 500, message: "系统错误"}
|
||||
|
||||
// 消息渠道
|
||||
ReportChannelNotfound = &BusinessErr{code: 1000, message: "消息渠道不存在"}
|
||||
)
|
||||
|
|
|
@ -38,3 +38,16 @@ func ReportChannelList(c *gin.Context) {
|
|||
Data: list,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func ReportChannelUpdate(c *gin.Context) {
|
||||
req, _ := controllers.GetRequest(c).(*backend.ReportChannelUpdateRequest)
|
||||
reportChannel := req.Request2DB()
|
||||
err := services.ReportChannelUpdate(&reportChannel)
|
||||
controllers.HandRes(c, req, err)
|
||||
}
|
||||
|
||||
func ReportChannelDelete(c *gin.Context) {
|
||||
req, _ := controllers.GetRequest(c).(*entities.IdRequest)
|
||||
err := services.ReportChannelDelete(*req)
|
||||
controllers.HandRes(c, req, err)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
zh_translations "gopkg.in/go-playground/validator.v9/translations/zh"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -44,7 +45,7 @@ func Error500(c *gin.Context) {
|
|||
*/
|
||||
func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error) {
|
||||
if c.Request.Method == "GET" || c.Request.Method == "DELETE" {
|
||||
err = c.ShouldBindQuery(request)
|
||||
err = c.ShouldBind(request)
|
||||
} else {
|
||||
err = c.ShouldBindJSON(request)
|
||||
}
|
||||
|
@ -53,6 +54,12 @@ func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error)
|
|||
validate := validator.New()
|
||||
zh_ch := zh.New()
|
||||
|
||||
//注册一个函数,获取struct tag里自定义的label作为字段名
|
||||
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||
name := fld.Tag.Get("label")
|
||||
return name
|
||||
})
|
||||
|
||||
uni := ut.New(zh_ch)
|
||||
trans, _ := uni.GetTranslator("zh")
|
||||
//验证器注册翻译器
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
type ReportChannelListRequest struct {
|
||||
entities.PageRequest
|
||||
ReportChannelId int `json:"report_channel_id" validate:"min=0"`
|
||||
Status int `json:"status"`
|
||||
ReportChannelId int `json:"report_channel_id" form:"report_channel_id" validate:"min=0"`
|
||||
Status int `json:"status" form:"status"`
|
||||
}
|
||||
|
||||
type ReportChannelListResponse struct {
|
||||
|
@ -30,10 +30,10 @@ func (this *ReportChannelListResponse) FromDb(in cronreportchannelmodel.CronRepo
|
|||
}
|
||||
|
||||
type ReportChannelCreateRequest struct {
|
||||
ClientKey string `json:"client_key"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
Config string `json:"config"`
|
||||
Status int `json:"status"`
|
||||
ClientKey string `json:"client_key" validate:"required" label:"服务key"`
|
||||
ClientSecret string `json:"client_secret" validate:"required" label:"服务secret"`
|
||||
Config string `json:"config" validate:"required" label:"服务配置"`
|
||||
Status int `json:"status" validate:"required" label:"状态"`
|
||||
}
|
||||
|
||||
func (this *ReportChannelCreateRequest) Request2DB() (db cronreportchannelmodel.CronReportChannel) {
|
||||
|
@ -43,3 +43,20 @@ func (this *ReportChannelCreateRequest) Request2DB() (db cronreportchannelmodel.
|
|||
db.Status = this.Status
|
||||
return db
|
||||
}
|
||||
|
||||
type ReportChannelUpdateRequest struct {
|
||||
ReportChannelId uint `json:"report_channel_id"`
|
||||
ClientKey string `json:"client_key" validate:"required" label:"配置key"`
|
||||
ClientSecret string `json:"client_secret" validate:"required" label:"配置密钥"`
|
||||
Config string `json:"config" validate:"required" label:"配置"`
|
||||
Status int `json:"status" validate:"required" label:"状态"`
|
||||
}
|
||||
|
||||
func (this *ReportChannelUpdateRequest) Request2DB() (db cronreportchannelmodel.CronReportChannel) {
|
||||
db.ReportChannelId = this.ReportChannelId
|
||||
db.ClientKey = this.ClientKey
|
||||
db.ClientSecret = this.ClientSecret
|
||||
db.Config = this.Config
|
||||
db.Status = this.Status
|
||||
return db
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package entities
|
||||
|
||||
type IdRequest struct {
|
||||
Id int64 `json:"id"`
|
||||
Id uint `json:"id"`
|
||||
}
|
||||
|
||||
type PageRequest struct {
|
||||
|
|
|
@ -2,6 +2,7 @@ package requestmapping
|
|||
|
||||
import (
|
||||
"cron_admin/app/constants/common"
|
||||
"cron_admin/app/http/entities"
|
||||
"cron_admin/app/http/entities/backend"
|
||||
)
|
||||
|
||||
|
@ -11,4 +12,10 @@ var BackendRequestMap = map[string]func() interface{}{
|
|||
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
|
||||
common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) },
|
||||
common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) },
|
||||
|
||||
// 消息渠道
|
||||
common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) },
|
||||
common.ADMIN_OAUTH_V1 + "/channel/list": func() interface{} { return new(backend.ReportChannelListRequest) },
|
||||
common.ADMIN_OAUTH_V1 + "/channel/update": func() interface{} { return new(backend.ReportChannelUpdateRequest) },
|
||||
common.ADMIN_OAUTH_V1 + "/channel/delete": func() interface{} { return new(entities.IdRequest) },
|
||||
}
|
||||
|
|
|
@ -56,6 +56,8 @@ func RegisterAdminRoute(router *gin.Engine) {
|
|||
{
|
||||
mes.GET("/list", backend.ReportChannelList)
|
||||
mes.POST("/create", backend.ReportChannelCreate)
|
||||
mes.POST("/delete", backend.ReportChannelDelete)
|
||||
mes.POST("/update", backend.ReportChannelUpdate)
|
||||
}
|
||||
|
||||
//日志
|
||||
|
|
|
@ -17,9 +17,10 @@ type CronReportChannel struct {
|
|||
ClientKey string `xorm:"'client_key' varchar(20)"`
|
||||
ClientSecret string `xorm:"'client_secret' varchar(50)"`
|
||||
Config string `xorm:"'config' JSON"`
|
||||
Status int `xorm:"'status' TINYINT"`
|
||||
CreateTime time.Time `xorm:"'create_time' created timestamp"`
|
||||
UpdateTime time.Time `xorm:"'update_time' updated timestamp"`
|
||||
Status int `xorm:"'status' TINYINT"`
|
||||
DeletedTime time.Time `xorm:"'deleted_time' deleted datetime"`
|
||||
}
|
||||
|
||||
// 表名
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"cron_admin/app/models/cronreportchannelmodel"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type ReportChannel struct {
|
||||
ICommonRepo[cronreportchannelmodel.CronReportChannel]
|
||||
}
|
||||
|
||||
func NewReportChannelRepo() *ReportChannel {
|
||||
return &ReportChannel{
|
||||
ICommonRepo: NewCommonRepo[cronreportchannelmodel.CronReportChannel](),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ReportChannel) WithByID(id uint) DBOption {
|
||||
return func(g *xorm.Session) *xorm.Session {
|
||||
return g.Where("report_channel_id = ?", id)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"cron_admin/app/constants/errorcode"
|
||||
"cron_admin/app/http/entities"
|
||||
"cron_admin/app/http/entities/backend"
|
||||
"cron_admin/app/models/cronreportchannelmodel"
|
||||
"cron_admin/app/repository"
|
||||
|
@ -17,7 +19,7 @@ func ReportChannelCreate(param *cronreportchannelmodel.CronReportChannel) (err e
|
|||
|
||||
func ReportChannelList(param backend.ReportChannelListRequest) (list []cronreportchannelmodel.CronReportChannel, total int64, err error) {
|
||||
var (
|
||||
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]()
|
||||
repo = repository.NewReportChannelRepo()
|
||||
opts = make([]repository.DBOption, 0)
|
||||
)
|
||||
|
||||
|
@ -34,3 +36,31 @@ func ReportChannelList(param backend.ReportChannelListRequest) (list []cronrepor
|
|||
total, err = repo.FindAndCount(&list, opts...)
|
||||
return
|
||||
}
|
||||
|
||||
func ReportChannelUpdate(param *cronreportchannelmodel.CronReportChannel) (err error) {
|
||||
var (
|
||||
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]()
|
||||
opts = make([]repository.DBOption, 0)
|
||||
)
|
||||
if param.ReportChannelId > 0 {
|
||||
opts = append(opts, repo.WithByID(param.ReportChannelId))
|
||||
}
|
||||
|
||||
_, err = repo.Update(param, opts...)
|
||||
return
|
||||
}
|
||||
|
||||
func ReportChannelDelete(param entities.IdRequest) (err error) {
|
||||
var (
|
||||
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]()
|
||||
opts = make([]repository.DBOption, 0)
|
||||
)
|
||||
if param.Id > 0 {
|
||||
opts = append(opts, repo.WithByID(param.Id))
|
||||
} else {
|
||||
return errorcode.ReportChannelNotfound
|
||||
}
|
||||
|
||||
_, err = repo.Delete(new(cronreportchannelmodel.CronReportChannel), opts...)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue