You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.6 KiB

package article
import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"practice_ArticleVote_Go/api/article"
"practice_ArticleVote_Go/internal/consts"
"practice_ArticleVote_Go/internal/dao"
"practice_ArticleVote_Go/internal/service"
)
var ArticleController = &cArticle{}
type cArticle struct {
service service.IArticle
}
// 添加文章与投票
func (c *cArticle) AddArticle(ctx context.Context, req *article.ArticleReq) (res *article.ArticleRes, err error) {
var Aid int64 // 文章ID
var Vid int64 // 投票活动ID
type VoteOption struct {
VoteId int64
OptionContent string
OptionIndex int
Status int
}
var VoteOptions []VoteOption
// 创建文章
Aid, err = dao.Article.Ctx(ctx).Data(req).InsertAndGetId()
if req.VoteStatus == 1 {
req.ArticleId = Aid
// 如果发起投票,创建投票活动
Vid, err = dao.VotePoll.Ctx(ctx).Data(req).InsertAndGetId()
if err != nil {
return nil, fmt.Errorf("创建投票活动失败: %w", err)
}
// 如果发起投票,创建投票选项
for _, option := range req.OptionList {
VoteOptions = append(VoteOptions, VoteOption{
VoteId: Vid,
OptionContent: option.OptionContent,
OptionIndex: option.OptionIndex,
Status: 1,
})
}
_, err = dao.VoteOption.Ctx(ctx).Data(VoteOptions).Insert()
if err != nil {
return nil, fmt.Errorf("创建投票选项失败: %w", err)
}
}
if err != nil {
return nil, fmt.Errorf("创建文章失败: %w", err)
}
articleList := fmt.Sprintf(consts.ARTICLE_LIST)
redis := g.Redis()
_, err = redis.Del(ctx, articleList)
fmt.Println(articleList)
return
}