board: support ident field
This commit is contained in:
parent
445d03e096
commit
4130a5df02
|
@ -13,6 +13,7 @@ type Board struct {
|
||||||
Id int64 `json:"id" gorm:"primaryKey"`
|
Id int64 `json:"id" gorm:"primaryKey"`
|
||||||
GroupId int64 `json:"group_id"`
|
GroupId int64 `json:"group_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Ident string `json:"ident"`
|
||||||
Tags string `json:"tags"`
|
Tags string `json:"tags"`
|
||||||
CreateAt int64 `json:"create_at"`
|
CreateAt int64 `json:"create_at"`
|
||||||
CreateBy string `json:"create_by"`
|
CreateBy string `json:"create_by"`
|
||||||
|
@ -38,11 +39,36 @@ func (b *Board) Verify() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Board) CanRenameIdent(ident string) (bool, error) {
|
||||||
|
if ident == "" {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err := Count(DB().Model(b).Where("ident=? and id <> ?", b.Ident, b.Id))
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt == 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Board) Add() error {
|
func (b *Board) Add() error {
|
||||||
if err := b.Verify(); err != nil {
|
if err := b.Verify(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.Ident != "" {
|
||||||
|
// ident duplicate check
|
||||||
|
cnt, err := Count(DB().Model(b).Where("ident=?", b.Ident))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt > 0 {
|
||||||
|
return errors.New("Ident duplicate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
b.CreateAt = now
|
b.CreateAt = now
|
||||||
b.UpdateAt = now
|
b.UpdateAt = now
|
||||||
|
|
|
@ -30,6 +30,7 @@ var (
|
||||||
"Name is blank": "名称不能为空",
|
"Name is blank": "名称不能为空",
|
||||||
"Name has invalid characters": "名称含有非法字符",
|
"Name has invalid characters": "名称含有非法字符",
|
||||||
"Dashboard already exists": "监控大盘已存在",
|
"Dashboard already exists": "监控大盘已存在",
|
||||||
|
"Ident duplicate": "英文标识已存在",
|
||||||
"No such dashboard": "监控大盘不存在",
|
"No such dashboard": "监控大盘不存在",
|
||||||
"AlertRule already exists": "告警规则已存在,不能重复创建",
|
"AlertRule already exists": "告警规则已存在,不能重复创建",
|
||||||
"No such AlertRule": "告警规则不存在",
|
"No such AlertRule": "告警规则不存在",
|
||||||
|
|
|
@ -6,11 +6,13 @@ import (
|
||||||
|
|
||||||
"github.com/didi/nightingale/v5/src/models"
|
"github.com/didi/nightingale/v5/src/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/toolkits/pkg/ginx"
|
"github.com/toolkits/pkg/ginx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type boardForm struct {
|
type boardForm struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Ident string `json:"ident"`
|
||||||
Tags string `json:"tags"`
|
Tags string `json:"tags"`
|
||||||
Configs string `json:"configs"`
|
Configs string `json:"configs"`
|
||||||
Public int `json:"public"`
|
Public int `json:"public"`
|
||||||
|
@ -25,6 +27,7 @@ func boardAdd(c *gin.Context) {
|
||||||
board := &models.Board{
|
board := &models.Board{
|
||||||
GroupId: ginx.UrlParamInt64(c, "id"),
|
GroupId: ginx.UrlParamInt64(c, "id"),
|
||||||
Name: f.Name,
|
Name: f.Name,
|
||||||
|
Ident: f.Ident,
|
||||||
Tags: f.Tags,
|
Tags: f.Tags,
|
||||||
Configs: f.Configs,
|
Configs: f.Configs,
|
||||||
CreateBy: me.Username,
|
CreateBy: me.Username,
|
||||||
|
@ -42,7 +45,8 @@ func boardAdd(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func boardGet(c *gin.Context) {
|
func boardGet(c *gin.Context) {
|
||||||
board, err := models.BoardGet("id = ?", ginx.UrlParamInt64(c, "bid"))
|
bid := ginx.UrlParamStr(c, "bid")
|
||||||
|
board, err := models.BoardGet("id = ? or ident = ?", bid, bid)
|
||||||
ginx.Dangerous(err)
|
ginx.Dangerous(err)
|
||||||
|
|
||||||
if board == nil {
|
if board == nil {
|
||||||
|
@ -117,12 +121,20 @@ func boardPut(c *gin.Context) {
|
||||||
// check permission
|
// check permission
|
||||||
bgrwCheck(c, bo.GroupId)
|
bgrwCheck(c, bo.GroupId)
|
||||||
|
|
||||||
|
can, err := bo.CanRenameIdent(f.Ident)
|
||||||
|
ginx.Dangerous(err)
|
||||||
|
|
||||||
|
if !can {
|
||||||
|
ginx.Bomb(http.StatusOK, "Ident duplicate")
|
||||||
|
}
|
||||||
|
|
||||||
bo.Name = f.Name
|
bo.Name = f.Name
|
||||||
|
bo.Ident = f.Ident
|
||||||
bo.Tags = f.Tags
|
bo.Tags = f.Tags
|
||||||
bo.UpdateBy = me.Username
|
bo.UpdateBy = me.Username
|
||||||
bo.UpdateAt = time.Now().Unix()
|
bo.UpdateAt = time.Now().Unix()
|
||||||
|
|
||||||
err := bo.Update("name", "tags", "update_by", "update_at")
|
err = bo.Update("name", "ident", "tags", "update_by", "update_at")
|
||||||
ginx.NewRender(c).Data(bo, err)
|
ginx.NewRender(c).Data(bo, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,6 +198,10 @@ func boardClone(c *gin.Context) {
|
||||||
UpdateBy: me.Username,
|
UpdateBy: me.Username,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if bo.Ident != "" {
|
||||||
|
newBoard.Ident = uuid.NewString()
|
||||||
|
}
|
||||||
|
|
||||||
ginx.Dangerous(newBoard.Add())
|
ginx.Dangerous(newBoard.Add())
|
||||||
|
|
||||||
// clone payload
|
// clone payload
|
||||||
|
|
Loading…
Reference in New Issue