Skip to content

Commit

Permalink
feat: reviewed/unreviewed filter
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 27, 2023
1 parent fd1b81c commit 862e780
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 10 deletions.
16 changes: 9 additions & 7 deletions api/poll/v1/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package v1

import (
"github.com/gogf/gf/v2/frame/g"
"github.com/hitokoto-osc/reviewer/internal/consts"
"github.com/hitokoto-osc/reviewer/internal/model"
)

type GetPollsReq struct {
g.Meta `path:"/poll" tags:"Poll" method:"get" summary:"获取投票列表"`
StatusStart int `json:"status_start" dc:"起始状态" v:"required|min:0"`
StatusEnd int `json:"status_end" dc:"结束状态" v:"required|max:300"`
WithRecords bool `json:"with_records" dc:"是否返回投票记录" v:"boolean" d:"false"`
Order string `json:"order" dc:"排序方式" v:"in:desc,asc" d:"asc"`
Page int `json:"page" dc:"页码" v:"min:1" d:"1"`
PageSize int `json:"page_size" dc:"每页数量" v:"min:0|max:1000" d:"30"`
g.Meta `path:"/poll" tags:"Poll" method:"get" summary:"获取投票列表"`
StatusStart int `json:"status_start" dc:"起始状态" v:"required|min:0"`
StatusEnd int `json:"status_end" dc:"结束状态" v:"required|max:300"`
WithRecords bool `json:"with_records" dc:"是否返回投票记录" v:"boolean" d:"false"`
PolledFilter consts.PollListPolledFilter `json:"polled_filter" dc:"投票过滤器" v:"in:0,1,2" d:"0"`
Order string `json:"order" dc:"排序方式" v:"in:desc,asc" d:"asc"`
Page int `json:"page" dc:"页码" v:"min:1" d:"1"`
PageSize int `json:"page_size" dc:"每页数量" v:"min:0|max:1000" d:"30"`
}

type GetPollsRes model.GetPollListOutput
1 change: 1 addition & 0 deletions api/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions api/user/v1/poll_unreviewed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package v1

import "github.com/gogf/gf/v2/frame/g"

type GetUserPollUnreviewedReq struct {
g.Meta `path:"/user/poll/unreviewed" tags:"User" method:"get" summary:"获得未审查的投票数量"`
}

type GetUserPollUnreviewedRes struct {
Count int `json:"count" dc:"未审查的投票数量"`
}
9 changes: 9 additions & 0 deletions internal/consts/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ const (
PollMaxOpenPolls = 100 // 最大开放投票数
)

type PollListPolledFilter int

const (
PollListPolledFilterAll PollListPolledFilter = iota // 全部
PollListPolledFilterReviewed // 已投票
PollListPolledFilterUnreviewed // 未投票

)

// PollMethod 审核员投票的类型
type PollMethod int

Expand Down
1 change: 1 addition & 0 deletions internal/controller/poll/poll_v1_get_polls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (c *ControllerV1) GetPolls(ctx context.Context, req *v1.GetPollsReq) (res *
WithPollRecords: req.WithRecords,
WithMarks: true,
WithCache: true,
PolledFilter: req.PolledFilter,
Page: req.Page,
PageSize: req.PageSize,
})
Expand Down
26 changes: 26 additions & 0 deletions internal/controller/user/user_v1_get_user_poll_unreviewed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package user

import (
"context"

"github.com/hitokoto-osc/reviewer/internal/service"

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"

v1 "github.com/hitokoto-osc/reviewer/api/user/v1"
)

func (c *ControllerV1) GetUserPollUnreviewed(ctx context.Context, req *v1.GetUserPollUnreviewedReq) (
res *v1.GetUserPollUnreviewedRes,
err error,
) {
user := service.BizCtx().GetUser(ctx)
count, err := service.Poll().CountUserUnreviewedPoll(ctx, user.Id)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "获取未审查的投票数量失败")
}
return &v1.GetUserPollUnreviewedRes{
Count: count,
}, nil
}
1 change: 1 addition & 0 deletions internal/logic/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (s *sCache) ClearCacheAfterPollUpdated(ctx context.Context, userID, pollID
"SelectCache:poll_log:sentence_uuid:" + sentenceUUID,
"SelectCache:poll_marks:pid:" + gconv.String(pollID),
"SelectCache:user:poll:uid:" + gconv.String(userID),
"SelectCache:user:poll:unreviewed:uid:" + gconv.String(userID) + ":count",
}); e != nil {
e = gerror.Wrap(e, "failed to remove cache: ")
g.Log().Error(ctx, e)
Expand Down
84 changes: 82 additions & 2 deletions internal/logic/poll/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,33 @@ func (s *sPoll) GetPollList(ctx context.Context, in *model.GetPollListInput) (*m
count int
)
// fetch poll list
if in.StatusStart == in.StatusEnd || in.StatusEnd == 0 {
if in.PolledFilter != consts.PollListPolledFilterAll {
filter := ""
if in.PolledFilter == consts.PollListPolledFilterReviewed {
filter = "NOT"
}
sql := fmt.Sprintf(`
SELECT
poll.*
FROM
%s poll
LEFT JOIN %s log ON poll.%s = log.%s AND log.%s = %d
WHERE
log.%s IS %s NULL AND
poll.%s = 1
`,
dao.Poll.Table(),
dao.PollLog.Table(),
dao.Poll.Columns().Id,
dao.PollLog.Columns().PollId,
dao.PollLog.Columns().UserId,
in.UserID,
dao.PollLog.Columns().Id,
filter,
dao.Poll.Columns().Status,
)
query = query.Raw(sql)
} else if in.StatusStart == in.StatusEnd || in.StatusEnd == 0 { // 之后才判断状态
query = query.Where(dao.Poll.Columns().Status, in.StatusStart)
} else {
query = query.WhereBetween(dao.Poll.Columns().Status, in.StatusStart, in.StatusEnd)
Expand All @@ -138,9 +164,31 @@ func (s *sPoll) GetPollList(ctx context.Context, in *model.GetPollListInput) (*m
eg.Go(func() error {
q := query.Clone()
if in.WithCache {
name := ""
if in.PolledFilter == consts.PollListPolledFilterAll {
name = fmt.Sprintf(
"poll:list:status:%d:%d:page:%d:limit:%d:%s",
in.Page,
in.PageSize,
in.StatusStart,
in.StatusEnd,
in.Order,
)
} else {
name = fmt.Sprintf(
"poll:list:reviewed_filter:%d:user:%d:status:%d:%d:page:%d:limit:%d:%s",
in.PolledFilter,
in.UserID,
in.Page,
in.PageSize,
in.StatusStart,
in.StatusEnd,
in.Order,
)
}
q = q.Cache(gdb.CacheOption{
Duration: time.Minute * 10,
Name: fmt.Sprintf("poll:list:status:%d:%d:page:%d:limit:%d:%s", in.Page, in.PageSize, in.StatusStart, in.StatusEnd, in.Order),
Name: name,
Force: false,
})
}
Expand Down Expand Up @@ -407,3 +455,35 @@ func (s *sPoll) CancelPollByID(ctx context.Context, in *model.CancelPollInput) e
go service.Cache().ClearCacheAfterPollUpdated(ctx, in.UserID, in.PollID, in.SentenceUUID)
return nil
}

func (s *sPoll) CountUserUnreviewedPoll(ctx context.Context, uid uint) (int, error) {
record, err := dao.Poll.Ctx(ctx).
Cache(gdb.CacheOption{
Duration: time.Minute * 10,
Name: fmt.Sprintf("user:poll:unreviewed:uid:%d:count", uid),
}).
Raw(
fmt.Sprintf(`
SELECT
COUNT(1)
FROM
%s poll
LEFT JOIN %s log ON poll.%s = log.%s AND log.%s = %d
WHERE
log.%s IS NULL AND
poll.%s = 1`,
dao.Poll.Table(),
dao.PollLog.Table(),
dao.Poll.Columns().Id,
dao.PollLog.Columns().PollId,
dao.PollLog.Columns().UserId,
uid,
dao.PollLog.Columns().Id,
dao.Poll.Columns().Status,
),
).Value()
if err != nil {
return 0, err
}
return record.Int(), nil
}
2 changes: 1 addition & 1 deletion internal/logic/poll/ruling.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *sPoll) DoRuling(ctx context.Context, poll *entity.Poll, target consts.P
}
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 首先先锁定投票
_, e := dao.Poll.Ctx(ctx).TX(tx).Where(dao.Poll.Columns().Id, poll.Id).LockUpdate().Update(do.Poll{
_, e := dao.Poll.Ctx(ctx).TX(tx).Where(dao.Poll.Columns().Id, poll.Id).Update(do.Poll{
Status: int(consts.PollStatusProcessing),
})
if e != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/model/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type GetPollListInput struct {
WithPollRecords bool
WithMarks bool
WithCache bool
PolledFilter consts.PollListPolledFilter
Page int
PageSize int
}
Expand Down
3 changes: 3 additions & 0 deletions internal/service/poll.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 862e780

Please sign in to comment.