Files
web_huakui_server/web/service/srv_videotopic/topic.go
T
rootandClaude Opus 4.7 85c0071fd8 feat: 从黄果 NET hgdjnet-server 9bfbafe 导入 HKDJ 官网后端
来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83
项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet
测试目录 /server/hkdj_net,App/Web API 30331/30332

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-10-16 11:20:04 +08:00

117 lines
2.9 KiB
Go

package srv_videotopic
import (
"context"
"hkdj-web-server/pub/common/srvdef"
"hkdj-web-server/pub/models/videotopicmod"
"hkdj-web-server/pub/mongos"
"hkdj-web-server/web/service/srv_videoslug"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// GetVideoTopics 按照参数搜索视频分类
func GetVideoTopics(ctx context.Context, param *VideoTopicReq) (*VideoTopicResp, *srvdef.SrvErr) {
filter := bson.M{}
if param.VideoCategoryId != nil {
filter[videotopicmod.FN.VideoTopicModel.VideoCategoryId] = *param.VideoCategoryId
}
opt := &options.FindOptions{}
resp := &VideoTopicResp{}
opt.SetSort(bson.M{videotopicmod.FN.VideoTopicModel.Rank: -1})
opt.SetSkip((param.PageNum - 1) * param.PageSize).SetLimit(param.PageSize)
total, serr := videotopicmod.CountVideoTopicModels(ctx, filter)
if serr != nil {
return resp, serr
}
resp.Total = total
if total == 0 {
return resp, nil
}
ads, serr := videotopicmod.FindManyVideoTopicModels(ctx, filter, opt)
if serr != nil {
return resp, serr
}
resp.List = ads
return resp, nil
}
// AddVideoTopic 添加一个视频分类
func AddVideoTopic(ctx context.Context,
ac *videotopicmod.VideoTopicModel) *srvdef.SrvErr {
ac.Id = mongos.Lid.VideoTopic.Get()
now := time.Now()
ac.CreatedAt = now
ac.UpdatedAt = now
if serr := srv_videoslug.EnsureTopic(ctx, ac, 0); serr != nil {
return serr
}
serr := videotopicmod.InsertOneVideoTopicModel(ctx, ac)
if serr != nil {
return serr
}
return nil
}
// DelVideoTopic 删除指定视频分类
func DelVideoTopic(ctx context.Context, id int64) *srvdef.SrvErr {
adFilter := bson.M{videotopicmod.FN.VideoTopicModel.Id: id}
//删除
count, serr := videotopicmod.DelOneVideoTopicModel(ctx, adFilter)
if serr != nil {
return serr
}
if count == 0 {
return srvdef.New(srvdef.ErrNoData, "", "")
}
return nil
}
// UpdateVideoTopic 更新指定视频分类
func UpdateVideoTopic(ctx context.Context, id int64, param VideoTopicUpdate) *srvdef.SrvErr {
now := time.Now()
param.UpdatedAt = now
info, serr := videotopicmod.FindOneVideoTopicModel(ctx, bson.M{videotopicmod.FN.VideoTopicModel.Id: id})
if serr != nil {
return serr
}
name := info.Name
if param.Name != nil {
name = *param.Name
}
keywords := info.SeoInfo.Keyworld
if param.SeoInfo != nil {
keywords = param.SeoInfo.Keyworld
}
slug, changed, slugErr := srv_videoslug.PrepareTopicOnUpdate(ctx, info, param.Slug, name, keywords)
if slugErr != nil {
return slugErr
}
if changed {
param.Slug = &slug
} else {
param.Slug = nil
}
adFilter := bson.M{videotopicmod.FN.VideoTopicModel.Id: id}
param.IndexNow = nil
if param.IsReIndexNow != nil && *param.IsReIndexNow {
indexNow := int64(0)
param.IndexNow = &indexNow
}
updateDoc := bson.M{"$set": param}
serr = videotopicmod.UpdateOneVideoTopicModel(ctx, adFilter, updateDoc)
if serr != nil {
return serr
}
return nil
}