init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package slugx
|
||||
|
||||
import (
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/common/utils"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// Exists 判断 slug 是否已被占用
|
||||
type Exists func(slug string) bool
|
||||
|
||||
// Ensure 新增时保证 slug:有值则校验唯一且不能全数字,无值则按 source 自动生成
|
||||
func Ensure(slug *string, source string, exists Exists) *srvdef.SrvErr {
|
||||
if slug == nil {
|
||||
return srvdef.New(srvdef.ErrParamError, "Slug不能为空", "Slug不能为空")
|
||||
}
|
||||
*slug = utils.SanitizeSlug(*slug)
|
||||
if *slug != "" {
|
||||
if utils.IsSlugAllDigits(*slug) {
|
||||
return srvdef.New(srvdef.ErrParamError, "Slug不能全是数字", "Slug不能全是数字")
|
||||
}
|
||||
if exists(*slug) {
|
||||
return srvdef.New(srvdef.ErrDbInputExist, "Slug已存在", "Slug已存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*slug = utils.MakeUniqueSlug(source, exists)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrepareOnUpdate 更新时处理 slug:手动传入则校验,历史为空则自动生成
|
||||
func PrepareOnUpdate(current string, reqSlug *string, source string, exists Exists) (string, bool, *srvdef.SrvErr) {
|
||||
slug := current
|
||||
if reqSlug != nil {
|
||||
slug = utils.SanitizeSlug(*reqSlug)
|
||||
}
|
||||
if slug != "" {
|
||||
if reqSlug != nil {
|
||||
if utils.IsSlugAllDigits(slug) {
|
||||
return "", false, srvdef.New(srvdef.ErrParamError, "Slug不能全是数字", "Slug不能全是数字")
|
||||
}
|
||||
if exists(slug) {
|
||||
return "", false, srvdef.New(srvdef.ErrDbInputExist, "Slug已存在", "Slug已存在")
|
||||
}
|
||||
return slug, true, nil
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
||||
return utils.MakeUniqueSlug(source, exists), true, nil
|
||||
}
|
||||
|
||||
// Filter 构造按 slug 查重条件,excludeId>0 时排除自身
|
||||
func Filter(slugField, idField, slug string, excludeId int64) bson.M {
|
||||
filter := bson.M{slugField: slug}
|
||||
if excludeId > 0 {
|
||||
filter[idField] = bson.M{"$ne": excludeId}
|
||||
}
|
||||
return filter
|
||||
}
|
||||
|
||||
// EmptyFilter 查询 slug 为空或不存在的历史数据
|
||||
func EmptyFilter() bson.M {
|
||||
return bson.M{
|
||||
"$or": []bson.M{
|
||||
{"slug": ""},
|
||||
{"slug": bson.M{"$exists": false}},
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user