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

  1. package article
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "practice_ArticleVote_Go/api/article"
  7. "practice_ArticleVote_Go/internal/consts"
  8. "practice_ArticleVote_Go/internal/dao"
  9. "practice_ArticleVote_Go/internal/service"
  10. )
  11. var ArticleController = &cArticle{}
  12. type cArticle struct {
  13. service service.IArticle
  14. }
  15. // 添加文章与投票
  16. func (c *cArticle) AddArticle(ctx context.Context, req *article.ArticleReq) (res *article.ArticleRes, err error) {
  17. var Aid int64 // 文章ID
  18. var Vid int64 // 投票活动ID
  19. type VoteOption struct {
  20. VoteId int64
  21. OptionContent string
  22. OptionIndex int
  23. Status int
  24. }
  25. var VoteOptions []VoteOption
  26. // 创建文章
  27. Aid, err = dao.Article.Ctx(ctx).Data(req).InsertAndGetId()
  28. if req.VoteStatus == 1 {
  29. req.ArticleId = Aid
  30. // 如果发起投票,创建投票活动
  31. Vid, err = dao.VotePoll.Ctx(ctx).Data(req).InsertAndGetId()
  32. if err != nil {
  33. return nil, fmt.Errorf("创建投票活动失败: %w", err)
  34. }
  35. // 如果发起投票,创建投票选项
  36. for _, option := range req.OptionList {
  37. VoteOptions = append(VoteOptions, VoteOption{
  38. VoteId: Vid,
  39. OptionContent: option.OptionContent,
  40. OptionIndex: option.OptionIndex,
  41. Status: 1,
  42. })
  43. }
  44. _, err = dao.VoteOption.Ctx(ctx).Data(VoteOptions).Insert()
  45. if err != nil {
  46. return nil, fmt.Errorf("创建投票选项失败: %w", err)
  47. }
  48. }
  49. if err != nil {
  50. return nil, fmt.Errorf("创建文章失败: %w", err)
  51. }
  52. articleList := fmt.Sprintf(consts.ARTICLE_LIST)
  53. redis := g.Redis()
  54. _, err = redis.Del(ctx, articleList)
  55. fmt.Println(articleList)
  56. return
  57. }