81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package annouser
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/stderr"
|
|
"91porn-server/models/v/annoumod"
|
|
"91porn-server/web/vidhelp"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// GetAnnouList 获取公告列表
|
|
func GetAnnouList(req annoumod.ListReq) (code stderr.Code, data interface{}) {
|
|
if len(req.Sort) == 0 {
|
|
req.Sort = "createdAt"
|
|
}
|
|
desc := -1
|
|
if req.Desc == 1 {
|
|
desc = req.Desc
|
|
}
|
|
aInfos, _, total, err := annoumod.GetAnnou(req.PageNumber, req.PageSize, req.Sort, desc)
|
|
if err != nil {
|
|
return stderr.ErrDbQueryError, nil
|
|
}
|
|
return stderr.Success, annoumod.ListResp{AInfos: aInfos, Total: total}
|
|
}
|
|
|
|
// UpdateAnnou 更新公告信息
|
|
func UpdateAnnou(req annoumod.EditReq) (code stderr.Code, data interface{}) {
|
|
oid, err := primitive.ObjectIDFromHex(req.ID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("exchange string(%s) to objectID error.", req.ID))
|
|
return stderr.ErrParamError, nil
|
|
}
|
|
m := vidhelp.Struct2Map(req.EditInfo)
|
|
cnt, err := annoumod.UpdateAnnou(oid, m)
|
|
if err != nil {
|
|
return stderr.ErrDbUpdateError, nil
|
|
}
|
|
return stderr.Success, annoumod.OpeResp{Count: cnt}
|
|
}
|
|
|
|
// DeleteAnnou 删除公告
|
|
func DeleteAnnou(ids []string) (code stderr.Code, data interface{}) {
|
|
oids := make([]primitive.ObjectID, 0, len(ids))
|
|
for _, id := range ids {
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("exchange string(%s) to objectID error.", id))
|
|
continue
|
|
}
|
|
oids = append(oids, oid)
|
|
}
|
|
cnt, err := annoumod.DeleteAnnou(oids)
|
|
if err != nil {
|
|
return stderr.ErrDbUpdateError, nil
|
|
}
|
|
return stderr.Success, annoumod.OpeResp{Count: cnt}
|
|
}
|
|
|
|
// AddAnnou 添加公告
|
|
func AddAnnou(req annoumod.AddReq) (code stderr.Code, data interface{}) {
|
|
infos := []annoumod.Annou{annoumod.Annou{
|
|
Title: req.Title,
|
|
Content: req.Content,
|
|
Cover: req.Cover,
|
|
Type: req.Type,
|
|
Href: req.Href,
|
|
Active: req.Active,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}}
|
|
if err := annoumod.AddAnnou(infos); err != nil {
|
|
return stderr.ErrDbInsertError, ""
|
|
}
|
|
return stderr.Success, nil
|
|
}
|