mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-05-11 20:48:03 +08:00
66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package topical
|
|
|
|
import (
|
|
"errors"
|
|
"gofiber.study.skcks.cn/global"
|
|
"gofiber.study.skcks.cn/logger"
|
|
"gofiber.study.skcks.cn/model/generic/models"
|
|
"gofiber.study.skcks.cn/model/topical"
|
|
"strconv"
|
|
)
|
|
|
|
type Service struct {
|
|
}
|
|
|
|
var Services *Service
|
|
|
|
func InitService() {
|
|
Services = &Service{}
|
|
}
|
|
|
|
var (
|
|
NotExist = errors.New("主题不存在")
|
|
)
|
|
|
|
func (s *Service) CreateTopical(dto topical.CreateTopicalDTO, userId string) error {
|
|
id, _ := global.SonyFlake.NextID()
|
|
_, err := global.DataSources.Insert(&models.Topical{
|
|
Id: strconv.FormatUint(id, 10),
|
|
Active: true,
|
|
Content: dto.Content,
|
|
ModifyTimes: 0,
|
|
Title: dto.Title,
|
|
UserId: userId,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) DeleteTopical(dto topical.DeleteTopicalDTO) error {
|
|
t := &models.Topical{
|
|
Id: dto.Id,
|
|
}
|
|
|
|
logger.Logger().Infof("%#v", t)
|
|
|
|
exist, err := global.DataSources.Get(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !exist {
|
|
return NotExist
|
|
}
|
|
|
|
_, err = global.DataSources.Limit(1).Delete(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|