From f9ed9752399d5bf5fe76d16846aed8a037a50fc3 Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Wed, 9 Aug 2023 16:23:57 +0800 Subject: [PATCH] feat(controller, poll): get poll list with poll records --- api/poll/v1/get.go | 14 +++---- internal/consts/poll.go | 1 + .../poll/poll_v1_get_poll_detail.go | 2 +- internal/controller/poll/poll_v1_get_polls.go | 18 ++++----- internal/logic/poll/convert.go | 38 +++++++++++++++++++ internal/logic/poll/poll.go | 30 +++++++++++++-- internal/model/poll.go | 32 ++++++++++------ internal/service/poll.go | 3 ++ 8 files changed, 105 insertions(+), 33 deletions(-) diff --git a/api/poll/v1/get.go b/api/poll/v1/get.go index 798331b..d4a3667 100644 --- a/api/poll/v1/get.go +++ b/api/poll/v1/get.go @@ -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 diff --git a/internal/consts/poll.go b/internal/consts/poll.go index 285d8ed..ec7e734 100644 --- a/internal/consts/poll.go +++ b/internal/consts/poll.go @@ -8,6 +8,7 @@ const ( type PollMethod int const ( + PollMethodUnknown PollMethod = 0 // 未知 PollMethodApprove PollMethod = 1 // 赞同 PollMethodReject PollMethod = 2 // 驳回 PollMethodNeedModify PollMethod = 3 // 需要修改 diff --git a/internal/controller/poll/poll_v1_get_poll_detail.go b/internal/controller/poll/poll_v1_get_poll_detail.go index 89995bb..71220c9 100644 --- a/internal/controller/poll/poll_v1_get_poll_detail.go +++ b/internal/controller/poll/poll_v1_get_poll_detail.go @@ -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), diff --git a/internal/controller/poll/poll_v1_get_polls.go b/internal/controller/poll/poll_v1_get_polls.go index cc7e303..39ad5e5 100644 --- a/internal/controller/poll/poll_v1_get_polls.go +++ b/internal/controller/poll/poll_v1_get_polls.go @@ -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, "获取投票列表失败") diff --git a/internal/logic/poll/convert.go b/internal/logic/poll/convert.go index f114724..c248fd2 100644 --- a/internal/logic/poll/convert.go +++ b/internal/logic/poll/convert.go @@ -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{ @@ -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 +} diff --git a/internal/logic/poll/poll.go b/internal/logic/poll/poll.go index d9566d1..bf05364 100644 --- a/internal/logic/poll/poll.go +++ b/internal/logic/poll/poll.go @@ -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") @@ -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 }) } } diff --git a/internal/model/poll.go b/internal/model/poll.go index 6de6108..1f39493 100644 --- a/internal/model/poll.go +++ b/internal/model/poll.go @@ -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:"更新时间"` @@ -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 { diff --git a/internal/service/poll.go b/internal/service/poll.go index cd39693..dc68e39 100644 --- a/internal/service/poll.go +++ b/internal/service/poll.go @@ -16,11 +16,14 @@ import ( type ( IPoll interface { GetPointsByRole(role consts.UserRole) consts.UserPollPoints + ConvertPollLogToPollRecord(in *entity.PollLog, isAdmin bool) (out *model.PollRecord, err error) + MustConvertPollLogToPollRecord(in *entity.PollLog, isAdmin bool) (out *model.PollRecord) GetPollByID(ctx context.Context, pid int) (poll *entity.Poll, err error) // GetPollBySentenceUUID 根据 Sentence UUID 获取最新发起的投票 GetPollBySentenceUUID(ctx context.Context, sentenceUUID string) (poll *entity.Poll, err error) CountOpenedPoll(ctx context.Context) (int, error) CreatePollByPending(ctx context.Context, pending *entity.Pending) (*entity.Poll, error) + //nolint:gocyclo GetPollList(ctx context.Context, in *model.GetPollListInput) (*model.GetPollListOutput, error) // Poll 投票 Poll(ctx context.Context, in *model.PollInput) error