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.

60 lines
1.3 KiB

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