安全与可靠性: 修复配额并发、投票事务、密码授权与导出完整性
This commit is contained in:
+29
-7
@@ -116,7 +116,10 @@ func computeResults(reqCtx stdctx.Context, v *db.Vote) (*Results, error) {
|
||||
|
||||
switch v.Type {
|
||||
case db.VoteTypeSingle:
|
||||
options, _ := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
options, err := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote options")
|
||||
}
|
||||
// 一次分组查询替代逐选项 COUNT(避免 N+1)
|
||||
counts, err := db.VoteSubmissions.CountGroupByOptionID(reqCtx, v.ID)
|
||||
if err != nil {
|
||||
@@ -132,9 +135,15 @@ func computeResults(reqCtx stdctx.Context, v *db.Vote) (*Results, error) {
|
||||
}
|
||||
|
||||
case db.VoteTypeMultiple:
|
||||
options, _ := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
options, err := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote options")
|
||||
}
|
||||
submissions, err := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote submissions")
|
||||
}
|
||||
counts := make(map[uint]int64, len(options))
|
||||
submissions, _ := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
var totalSelections int64
|
||||
for _, s := range submissions {
|
||||
var data struct {
|
||||
@@ -163,9 +172,12 @@ func computeResults(reqCtx stdctx.Context, v *db.Vote) (*Results, error) {
|
||||
}
|
||||
|
||||
case db.VoteTypeRating:
|
||||
submissions, err := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote submissions")
|
||||
}
|
||||
scoreCounts := make(map[int]int64)
|
||||
var sum int64
|
||||
submissions, _ := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
for _, s := range submissions {
|
||||
var data struct {
|
||||
Rating int `json:"rating"`
|
||||
@@ -184,10 +196,16 @@ func computeResults(reqCtx stdctx.Context, v *db.Vote) (*Results, error) {
|
||||
}
|
||||
|
||||
case db.VoteTypeRank:
|
||||
options, _ := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
options, err := db.VoteOptions.ListByVoteID(reqCtx, v.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote options")
|
||||
}
|
||||
submissions, err := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote submissions")
|
||||
}
|
||||
rankSums := make(map[uint]float64, len(options))
|
||||
rankCounts := make(map[uint]int64, len(options))
|
||||
submissions, _ := db.VoteSubmissions.ListByVoteID(reqCtx, v.ID, nil)
|
||||
for _, s := range submissions {
|
||||
var data struct {
|
||||
Ranking map[string]float64 `json:"ranking"`
|
||||
@@ -215,7 +233,11 @@ func computeResults(reqCtx stdctx.Context, v *db.Vote) (*Results, error) {
|
||||
}
|
||||
|
||||
case db.VoteTypeForm:
|
||||
res.FormFields, _ = db.VoteFormFields.ListByVoteID(reqCtx, v.ID)
|
||||
fields, err := db.VoteFormFields.ListByVoteID(reqCtx, v.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "list vote form fields")
|
||||
}
|
||||
res.FormFields = fields
|
||||
res.FormCount = total
|
||||
res.FormFieldStats = aggregateFormFieldStats(reqCtx, res.FormFields)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user