Browse Source

添加文章列表接口

master demo测试
maziyang 1 day ago
parent
commit
721a51b0d3
  1. 12
      api/vote_record/v1/vote_record.go
  2. 1
      api/vote_record/vote_record.go
  3. 7
      internal/consts/consts.go
  4. 6
      internal/controller/article/article.go
  5. 12
      internal/controller/vote_record/vote_record_v1_get_all_article.go
  6. 50
      internal/logic/vote_record/get_all_article.go
  7. 1
      internal/service/vote_record.go

12
api/vote_record/v1/vote_record.go

@ -5,6 +5,18 @@ import (
"github.com/gogf/gf/v2/os/gtime"
)
type GetAllArticleReq struct {
g.Meta `path:"/getVoteList" method:"get" tags:"获取文章及活动列表"`
}
type GetAllArticleListRes struct {
ArticleList []*GetAllArticleRes `json:"articleList"`
}
type GetAllArticleRes struct {
Id string `json:"articleId" dc:"文章/视频Id"`
ArticleTitle string `json:"articleTitle" dc:"文章/视频标题"`
VoteStatus int `json:"voteStatus" dc:"是否开启投票"`
CreateTime string `json:"createTime" dc:"<UNK>"`
}
type GetExportFileReq struct {
g.Meta `path:"/getExportVoteDetail" method:"get" tags:"获取投票名单列表"`
VoteId int `p:"voteId" v:"required" dc:"投票活动ID"`

1
api/vote_record/vote_record.go

@ -11,6 +11,7 @@ import (
)
type IVoteRecordV1 interface {
GetAllArticle(ctx context.Context, req *v1.GetAllArticleReq) (res *v1.GetAllArticleListRes, err error)
GetExportFile(ctx context.Context, req *v1.GetExportFileReq) (res *v1.GetExportFileRes, err error)
GetVoteDetail(ctx context.Context, req *v1.GetVoteDetailReq) (res *v1.GetVoteDetailListRes, err error)
GetVote(ctx context.Context, req *v1.GetVoteReq) (res *v1.GetVoteRes, err error)

7
internal/consts/consts.go

@ -6,7 +6,8 @@ const (
)
const (
VOTE_DETAIL = "voteDetail" //投票名单查询
ARTICLE = "article" //文章查询
EXPORT_FILE = "exportFile" //导出数据查询
VOTE_DETAIL = "voteDetail" //投票名单查询
ARTICLE = "article" //文章查询
EXPORT_FILE = "exportFile" //导出数据查询
ARTICLE_LIST = "articleList" //文章列表
)

6
internal/controller/article/article.go

@ -3,7 +3,9 @@ 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"
)
@ -57,5 +59,9 @@ func (c *cArticle) AddArticle(ctx context.Context, req *article.ArticleReq) (res
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
}

12
internal/controller/vote_record/vote_record_v1_get_all_article.go

@ -0,0 +1,12 @@
package vote_record
import (
"context"
"practice_ArticleVote_Go/internal/service"
"practice_ArticleVote_Go/api/vote_record/v1"
)
func (c *ControllerV1) GetAllArticle(ctx context.Context, req *v1.GetAllArticleReq) (res *v1.GetAllArticleListRes, err error) {
return service.NewVoteRecordService().GetAllArticleList(ctx, req)
}

50
internal/logic/vote_record/get_all_article.go

@ -0,0 +1,50 @@
package vote_record
import (
"context"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/frame/g"
v1 "practice_ArticleVote_Go/api/vote_record/v1"
"practice_ArticleVote_Go/internal/consts"
"practice_ArticleVote_Go/internal/dao"
"practice_ArticleVote_Go/internal/utils"
)
func (l *VoteRecordLogic) GetAllArticleList(ctx context.Context, req *v1.GetAllArticleReq) (res *v1.GetAllArticleListRes, err error) {
redis := g.Redis()
cacheKey := fmt.Sprintf(consts.ARTICLE_LIST)
fmt.Println(cacheKey)
data, _ := redis.Get(ctx, cacheKey)
fmt.Println("ARTICLE_LIST-data", data)
if data != nil {
var cached v1.GetAllArticleListRes
err = json.Unmarshal([]byte(data.String()), &cached)
if err == nil {
return &cached, nil
}
}
var articleList []*v1.GetAllArticleRes
err = dao.Article.
Ctx(ctx).
Fields(
dao.Article.Columns().Id,
dao.Article.Columns().ArticleTitle,
dao.Article.Columns().VoteStatus,
dao.Article.Columns().CreateTime,
).
OrderDesc(dao.Article.Columns().CreateTime).
Scan(&articleList)
if err != nil {
return nil, err
}
res = &v1.GetAllArticleListRes{
ArticleList: articleList,
}
buf, err := json.Marshal(res)
if err == nil {
ttl := utils.GetCacheTTL()
_ = redis.SetEX(ctx, cacheKey, string(buf), int64(ttl))
}
return res, nil
}

1
internal/service/vote_record.go

@ -7,6 +7,7 @@ import (
)
type IVoteRecord interface {
GetAllArticleList(ctx context.Context, req *v1.GetAllArticleReq) (res *v1.GetAllArticleListRes, err error)
ExportVoteDetail(ctx context.Context, req *v1.GetExportFileReq) (res *v1.GetExportFileRes, err error)
GetIndex(ctx context.Context, req *v1.GetIndexReq) (res *v1.GetIndexRes, err error)
AddRecord(ctx context.Context, req *v1.AddRecordReq) (res *v1.AddRecordRes, err error)

Loading…
Cancel
Save