Skip to content

Commit

Permalink
feat(controller, poll): get poll list with poll records
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 9, 2023
1 parent 7a9b9d0 commit f9ed975
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 33 deletions.
14 changes: 7 additions & 7 deletions api/poll/v1/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
)

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"`
ReturnPolled bool `json:"return_polled" 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"`
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 internal/consts/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
type PollMethod int

const (
PollMethodUnknown PollMethod = 0 // 未知
PollMethodApprove PollMethod = 1 // 赞同
PollMethodReject PollMethod = 2 // 驳回
PollMethodNeedModify PollMethod = 3 // 需要修改
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/poll/poll_v1_get_poll_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *ControllerV1) GetPollDetail(ctx context.Context, req *v1.GetPollDetailR
records[i] = model.PollRecord{
UserID: uint(log.UserId),
Point: log.Point,
Type: consts.PollMethod(log.Type),
Method: consts.PollMethod(log.Type),
Comment: log.Comment,
CreatedAt: (*time.Time)(log.CreatedAt),
UpdatedAt: (*time.Time)(log.UpdatedAt),
Expand Down
18 changes: 9 additions & 9 deletions internal/controller/poll/poll_v1_get_polls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func (c *ControllerV1) GetPolls(ctx context.Context, req *v1.GetPollsReq) (res *
return nil, gerror.NewCode(gcode.CodeInvalidOperation, "权限不足")
}
out, err := service.Poll().GetPollList(ctx, &model.GetPollListInput{
StatusStart: req.StatusStart,
StatusEnd: req.StatusEnd,
Order: req.Order,
UserID: user.Id,
WithUserPolledData: req.ReturnPolled,
WithMarks: true,
WithCache: true,
Page: req.Page,
PageSize: req.PageSize,
StatusStart: req.StatusStart,
StatusEnd: req.StatusEnd,
Order: req.Order,
UserID: user.Id,
WithPollRecords: req.WithRecords,
WithMarks: true,
WithCache: true,
Page: req.Page,
PageSize: req.PageSize,
})
if err != nil {
return nil, gerror.WrapCode(gcode.CodeOperationFailed, err, "获取投票列表失败")
Expand Down
38 changes: 38 additions & 0 deletions internal/logic/poll/convert.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package poll

import (
"github.com/gogf/gf/v2/errors/gerror"
"github.com/hitokoto-osc/reviewer/internal/consts"
"github.com/hitokoto-osc/reviewer/internal/dao"
"github.com/hitokoto-osc/reviewer/internal/model"
"github.com/hitokoto-osc/reviewer/internal/model/entity"
"github.com/hitokoto-osc/reviewer/utility/time"
)

var pollMethodsMap = map[consts.PollMethod]string{
Expand All @@ -25,3 +29,37 @@ var pointsMap = map[consts.UserRole]consts.UserPollPoints{
func (s *sPoll) GetPointsByRole(role consts.UserRole) consts.UserPollPoints {
return pointsMap[role]
}

func (s *sPoll) ConvertPollLogToPollRecord(in *entity.PollLog, isAdmin bool) (out *model.PollRecord, err error) {
if in == nil {
err = gerror.New("nil poll log")
return
}
if isAdmin {
out = &model.PollRecord{
UserID: uint(in.UserId),
Point: in.Point,
Method: consts.PollMethod(in.Type),
Comment: in.Comment,
CreatedAt: (*time.Time)(in.CreatedAt),
UpdatedAt: (*time.Time)(in.UpdatedAt),
}
} else {
out = &model.PollRecord{
UserID: uint(in.UserId),
Comment: in.Comment,
CreatedAt: (*time.Time)(in.CreatedAt),
UpdatedAt: (*time.Time)(in.UpdatedAt),
}
}
return
}

func (s *sPoll) MustConvertPollLogToPollRecord(in *entity.PollLog, isAdmin bool) (out *model.PollRecord) {
var err error
out, err = s.ConvertPollLogToPollRecord(in, isAdmin)
if err != nil {
panic(err)
}
return out
}
30 changes: 26 additions & 4 deletions internal/logic/poll/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (s *sPoll) CreatePollByPending(ctx context.Context, pending *entity.Pending
return poll, nil
}

//nolint:gocyclo
func (s *sPoll) GetPollList(ctx context.Context, in *model.GetPollListInput) (*model.GetPollListOutput, error) {
if in == nil {
return nil, gerror.New("input is nil")
Expand Down Expand Up @@ -181,11 +182,32 @@ func (s *sPoll) GetPollList(ctx context.Context, in *model.GetPollListInput) (*m
return e
})
}
if in.WithUserPolledData {
if in.WithPollRecords {
eg.Go(func() error {
var e error
collection[index].PolledData, e = service.User().GetUserPolledDataWithPollID(egCtx, in.UserID, uint(value.Id))
return e
records, e := service.Poll().GetPollLogsByPollID(egCtx, value.Id)
if e != nil {
return e
} else if len(records) == 0 {
collection[index].Records = make([]model.PollRecord, 0)
return nil
}
users := service.BizCtx().GetUser(ctx)
isAdmin := users != nil && users.Role == consts.UserRoleAdmin
collection[index].Records = make([]model.PollRecord, 0, len(records))
for recordIndex, record := range records {
if uint(record.UserId) == in.UserID {
collection[index].PolledData = &model.PolledData{
Point: record.Point,
Method: consts.PollMethod(record.Type),
CreatedAt: (*vtime.Time)(record.CreatedAt),
UpdatedAt: (*vtime.Time)(record.UpdatedAt),
}
}
if isAdmin || record.Comment != "" {
collection[index].Records[recordIndex] = *s.MustConvertPollLogToPollRecord(&records[recordIndex], isAdmin)
}
}
return nil
})
}
}
Expand Down
32 changes: 20 additions & 12 deletions internal/model/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type PollRecord struct {
UserID uint `json:"user_id" dc:"用户 ID"`
Point int `json:"point" dc:"投票点数"`
Type consts.PollMethod `json:"type" dc:"投票类型"`
Method consts.PollMethod `json:"method" dc:"投票类型"`
Comment string `json:"comment" dc:"理由"`
CreatedAt *time.Time `json:"created_at" dc:"投票时间"`
UpdatedAt *time.Time `json:"updated_at" dc:"更新时间"`
Expand Down Expand Up @@ -44,21 +44,29 @@ type PollElement struct {
}

type GetPollListInput struct {
StatusStart int
StatusEnd int
Order string
UserID uint // 仅当 WithUserPolledData 为 true 时有效
WithUserPolledData bool
WithMarks bool
WithCache bool
Page int
PageSize int
StatusStart int
StatusEnd int
Order string
UserID uint // 仅当 WithPollRecords 为 true 时有效
WithPollRecords bool
WithMarks bool
WithCache bool
Page int
PageSize int
}

type PollListCommentElement struct {
UserID uint `json:"user_id" dc:"用户 ID"`
Comment string `json:"comment" dc:"评论"`
CreatedAt *time.Time `json:"created_at" dc:"评论时间"`
UpdatedAt *time.Time `json:"updated_at" dc:"更新时间"`
}

type PollListElement struct {
PollElement
Marks []int `json:"marks" dc:"标记"`
PolledData *PolledData `json:"polled_data" dc:"投票数据"`
Marks []int `json:"marks" dc:"标记"`
PolledData *PolledData `json:"polled_data" dc:"投票数据"`
Records []PollRecord `json:"records" dc:"评论列表"`
}

type GetPollListOutput struct {
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 f9ed975

Please sign in to comment.