Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package slugser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/actress"
|
||||
"videoweb/pub/models/contenttagmod"
|
||||
"videoweb/pub/models/playletcategorymod"
|
||||
"videoweb/pub/models/playletmod"
|
||||
"videoweb/pub/models/videocategorymod"
|
||||
"videoweb/pub/models/videoinformationmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// Resolve 解析实体ID:slug 非空时先按 slug 查 ID,否则使用传入 ID。
|
||||
func Resolve(id int64, slug string, required bool, emptyMsg string, lookup func(string) (int64, *srvdef.SrvErr)) (int64, *srvdef.SrvErr) {
|
||||
slug = strings.TrimSpace(slug)
|
||||
if slug != "" {
|
||||
if lookup == nil {
|
||||
return 0, srvdef.New(srvdef.ErrParamError, emptyMsg, emptyMsg)
|
||||
}
|
||||
return lookup(slug)
|
||||
}
|
||||
if required && id <= 0 {
|
||||
if emptyMsg == "" {
|
||||
emptyMsg = "参数错误"
|
||||
}
|
||||
return 0, srvdef.New(srvdef.ErrParamError, emptyMsg, emptyMsg)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// ParseIDOrSlug 解析路径参数:纯正整数当作ID,否则当作slug。
|
||||
func ParseIDOrSlug(raw string) (int64, string) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return 0, ""
|
||||
}
|
||||
if id, err := strconv.ParseInt(raw, 10, 64); err == nil && id > 0 {
|
||||
return id, ""
|
||||
}
|
||||
return 0, raw
|
||||
}
|
||||
|
||||
func VideoID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "视频ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := videoinformationmod.FindOneVideoInformation(ctx, bson.M{
|
||||
videoinformationmod.FN.VideoInformation.Slug: s,
|
||||
}, options.FindOne().SetProjection(bson.M{videoinformationmod.FN.VideoInformation.Id: 1}))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
|
||||
func VideoCategoryID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "视频分类ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := videocategorymod.FindOneVideoCategory(ctx, bson.M{
|
||||
videocategorymod.FN.VideoCategory.Slug: s,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
|
||||
func PlayletID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "短剧ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := playletmod.FindOnePlaylet(ctx, bson.M{
|
||||
playletmod.FN.Playlet.Slug: s,
|
||||
}, options.FindOne().SetProjection(bson.M{playletmod.FN.Playlet.Id: 1}))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
|
||||
func PlayletCategoryID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "短剧分类ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := playletcategorymod.FindOnePlayletCategory(ctx, bson.M{
|
||||
playletcategorymod.FN.PlayletCategory.Slug: s,
|
||||
}, options.FindOne().SetProjection(bson.M{playletcategorymod.FN.PlayletCategory.Id: 1}))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
|
||||
func ActressID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "女优ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := actress.FindOneActress(ctx, bson.M{
|
||||
actress.FN.Actress.Slug: s,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
|
||||
func ContentTagID(ctx context.Context, id int64, slug string, required bool) (int64, *srvdef.SrvErr) {
|
||||
return Resolve(id, slug, required, "标签ID不能为空", func(s string) (int64, *srvdef.SrvErr) {
|
||||
item, err := contenttagmod.FindOneContentTag(ctx, bson.M{
|
||||
contenttagmod.FN.ContentTag.Slug: s,
|
||||
}, options.FindOne().SetProjection(bson.M{contenttagmod.FN.ContentTag.Id: 1}))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return item.Id, nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user