Skip to content

Commit

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

type CancelPollReq struct {
g.Meta `path:"/poll/cancel" tags:"Poll" method:"delete" summary:"取消投票"`
SentenceUUID string `json:"sentence_uuid" dc:"句子 UUID" v:"required|size:36"`
g.Meta `path:"/poll/:id/cancel" tags:"Poll" method:"delete" summary:"取消投票"`
ID int `json:"id" dc:"投票 ID" v:"required|min:1#请输入投票 ID"`
}

type CancelPollRes struct {
Expand Down
6 changes: 3 additions & 3 deletions api/poll/v1/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

type PollReq struct {
g.Meta `path:"/poll/:id" tags:"Poll" method:"put" summary:"投票"`
ID uint `json:"id" dc:"投票ID" v:"required|uint" in:"path"`
ID int `json:"id" dc:"投票ID" v:"required|int" in:"path"`
Method consts.PollMethod `json:"method" dc:"投票方式" v:"required|enums"`
MarkID uint `json:"mark_id" dc:"标记" v:"required-unless:method,1"`
Comment string `json:"comment" dc:"理由" v:"required-if:method,2,3|length:1,255"`
MarkIDs []int `json:"mark_ids" dc:"标记"`
Comment string `json:"comment" dc:"理由" v:"length:1,255"`
}

// PollRes 成功返回句子的投票记录
Expand Down
65 changes: 64 additions & 1 deletion internal/controller/poll/poll_v1_poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,75 @@ package poll
import (
"context"

"github.com/hitokoto-osc/reviewer/internal/consts"
"github.com/hitokoto-osc/reviewer/internal/model"
"github.com/hitokoto-osc/reviewer/internal/model/entity"
"golang.org/x/sync/errgroup"

"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/poll/v1"
)

func (c *ControllerV1) Poll(ctx context.Context, req *v1.PollReq) (res *v1.PollRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
if req.Method != consts.PollMethodApprove && req.Comment == "" && len(req.MarkIDs) == 0 { // 因为验证器规则太简单了,所以这里需要手动判断
return nil, gerror.NewCode(gcode.CodeInvalidOperation, "请至少选择一个标记或者填写理由。")
}

var (
poll *entity.Poll
polledData *model.PolledData
)
user := service.BizCtx().GetUser(ctx)
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
var e error
poll, e = service.Poll().GetPollByID(egCtx, req.ID)
return e
})
eg.Go(func() error {
var e error
polledData, e = service.User().GetUserPolledDataWithPollID(egCtx, user.Id, uint(req.ID))
return e
})
err = eg.Wait()
if err != nil {
return nil, err
} else if poll == nil {
return nil, gerror.NewCode(gcode.CodeNotFound, "投票不存在")
}
if polledData != nil {
return nil, gerror.NewCode(gcode.CodeOperationFailed, "您已经投过票了,请勿重复投票!")
}
if poll.Status != int(consts.PollStatusOpen) {
return nil, gerror.NewCode(gcode.CodeOperationFailed, "该投票未开放投票。")
}

sentence, err := service.Hitokoto().GetHitokotoV1SchemaByUUID(ctx, poll.SentenceUuid)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "获取句子信息失败")
} else if sentence == nil {
return nil, gerror.NewCode(gcode.CodeNotFound, "句子不存在")
}
if sentence.CreatorUID == user.Id && req.Method == consts.PollMethodApprove {
return nil, gerror.NewCode(gcode.CodeOperationFailed, "您不能给自己提交的句子投赞成票。")
}
points := service.Poll().GetPointsByRole(user.Role)
err = service.Poll().Poll(ctx, &model.PollInput{
Method: req.Method,
Point: int(points),
PollID: uint(req.ID),
SentenceUUID: poll.SentenceUuid,
Comment: req.Comment,
UserID: user.Id,
IsAdmin: user.Role == consts.UserRoleAdmin,
Marks: req.MarkIDs,
})
if err != nil {
return nil, gerror.WrapCode(gcode.CodeOperationFailed, err, "投票失败:")
}
return nil, nil
}
10 changes: 10 additions & 0 deletions internal/logic/poll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ var pollMethodsMap = map[consts.PollMethod]string{
func translatePollMethodToField(in consts.PollMethod) string {
return pollMethodsMap[in]
}

var pointsMap = map[consts.UserRole]consts.UserPollPoints{
consts.UserRoleUser: consts.UserPollPointsNormal,
consts.UserRoleReviewer: consts.UserPollPointsReviewer,
consts.UserRoleAdmin: consts.UserPollPointsAdmin,
}

func (s *sPoll) GetPointsByRole(role consts.UserRole) consts.UserPollPoints {
return pointsMap[role]
}
2 changes: 1 addition & 1 deletion internal/logic/poll/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (s *sPoll) GetPollList(ctx context.Context, in *model.GetPollListInput) (*m
}

// Poll 投票
func Poll(ctx context.Context, in *model.PollInput) error {
func (s *sPoll) Poll(ctx context.Context, in *model.PollInput) error {
if in == nil {
return gerror.New("input is empty")
}
Expand Down
4 changes: 4 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 7a9b9d0

Please sign in to comment.