-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer.go
282 lines (238 loc) · 6.67 KB
/
answer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package service
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/google/uuid"
"github.com/ictsc/ictsc-rikka/pkg/entity"
e "github.com/ictsc/ictsc-rikka/pkg/error"
"github.com/ictsc/ictsc-rikka/pkg/repository"
"github.com/pkg/errors"
)
type AnswerService struct {
preRoundMode bool
answerLimit time.Duration
webhook string
userRepo repository.UserRepository
answerRepo repository.AnswerRepository
problemRepo repository.ProblemRepository
}
type CreateAnswerRequest struct {
UserGroup *entity.UserGroup
Body string
ProblemID uuid.UUID
}
type UpdateAnswerRequest struct {
Point uint
}
func NewAnswerService(preRoundMode bool, answerLimit int, webhook string, userRepo repository.UserRepository, answerRepo repository.AnswerRepository, problemRepo repository.ProblemRepository) *AnswerService {
return &AnswerService{
preRoundMode: preRoundMode,
answerLimit: time.Duration(answerLimit) * time.Minute,
webhook: webhook,
userRepo: userRepo,
answerRepo: answerRepo,
problemRepo: problemRepo,
}
}
func (s *AnswerService) Create(req *CreateAnswerRequest) (*entity.Answer, error) {
lastAnswered := time.Time{}
pastAnswers, err := s.answerRepo.FindByUserGroup(req.UserGroup.ID)
if err != nil {
return nil, err
}
for _, answer := range pastAnswers {
if answer.CreatedAt.After(lastAnswered) && answer.ProblemID == req.ProblemID {
lastAnswered = answer.CreatedAt
}
}
if !time.Now().After(lastAnswered.Add(s.answerLimit)) {
return nil, e.NewForbiddenError("couldn't submit answer if you submit answer within last 20 minutes")
}
ans := &entity.Answer{
UserGroupID: req.UserGroup.ID,
Point: nil,
Body: req.Body,
ProblemID: req.ProblemID,
}
problem, err := s.problemRepo.FindByID(req.ProblemID)
if err != nil {
return nil, err
}
if problem == nil {
return nil, errors.New("problem id is invalid")
}
if problem.Type == entity.MultipleType {
type MultipleAnswer struct {
Group int `json:"group"`
Value []uint `json:"value"`
Type string `json:"type"`
}
var myAnswers []MultipleAnswer
err := json.Unmarshal([]byte(ans.Body), &myAnswers)
if err != nil {
return nil, e.NewBadRequestError("invalid ma format")
}
if len(myAnswers) > len(problem.CorrectAnswers) {
return nil, e.NewBadRequestError("invalid ma format")
}
var sum uint
for _, ma := range myAnswers {
group := ma.Group
if group < 0 || group > len(problem.CorrectAnswers) {
return nil, e.NewBadRequestError("ma group is invalid")
}
ca := problem.CorrectAnswers[group]
if ca.Type == entity.RadioButton {
if ca.Column[0] == ma.Value[0] {
sum += ca.Scoring.Correct
}
}
if ca.Type == entity.CheckBox {
correctCount := 0
for _, val := range ma.Value {
if contains(ca.Column, val) {
correctCount++
}
}
if correctCount == len(ca.Column) {
sum += ca.Scoring.Correct
} else if ca.Scoring.PartialCorrect != nil && correctCount > 0 {
sum += *ca.Scoring.PartialCorrect * uint(correctCount)
}
}
}
ans.Point = &sum
}
answer, err := s.answerRepo.Create(ans)
if err != nil {
return nil, err
}
//TODO: クリーンアーキテクチャ的にここでするべきではないので後でプレゼンターにする
{
text := fmt.Sprintf("<https://contest.mgmt.ictsc.net/scoring/%s?answer_id=%s |新着解答> 問題名:%s チーム名:%s",
strings.ToLower(problem.Code), answer.ID, problem.Title, req.UserGroup.Name)
param := struct {
Text string `json:"text"`
Channel string `json:"channel"`
}{
Text: text,
Channel: "#problem-" + strings.ToLower(problem.Code),
}
json_str, err := json.Marshal(param)
if err != nil {
log.Println(err.Error())
return answer, nil
}
resp, err := http.Post(s.webhook, "application/json", bytes.NewBuffer(json_str))
if err != nil {
log.Println(err.Error())
return answer, nil
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
return answer, nil
}
func (s *AnswerService) FindByID(group *entity.UserGroup, id uuid.UUID) (*entity.Answer, error) {
ans, err := s.answerRepo.FindByID(id)
if err != nil {
return nil, err
}
if !group.IsFullAccess && s.preRoundMode {
ans.Point = nil
}
if !group.IsFullAccess && !time.Now().After(ans.CreatedAt.Add(s.answerLimit)) {
ans.Point = nil
}
return ans, nil
}
// userGroupID is optional
func (s *AnswerService) FindByProblem(group *entity.UserGroup, probid uuid.UUID, userGroupID *uuid.UUID) ([]*entity.Answer, error) {
if !group.IsFullAccess && userGroupID != nil && group.ID != *userGroupID {
return nil, e.NewForbiddenError("you cannot fetch other group's answers")
}
answers, err := s.answerRepo.FindByProblem(probid, userGroupID)
if err != nil {
return nil, err
}
if !group.IsFullAccess {
now := time.Now()
for _, ans := range answers {
if s.preRoundMode {
ans.Point = nil
}
if !now.After(ans.CreatedAt.Add(s.answerLimit)) {
ans.Point = nil
}
}
}
return answers, nil
}
func (s *AnswerService) FindByUserGroup(id uuid.UUID) ([]*entity.Answer, error) {
return s.answerRepo.FindByUserGroup(id)
}
// userGroupID is require
func (s *AnswerService) FindByProblemAndUserGroup(group *entity.UserGroup, probid uuid.UUID, userGroupID uuid.UUID) ([]*entity.Answer, error) {
if !group.IsFullAccess && group.ID != userGroupID {
return nil, e.NewForbiddenError("you cannot fetch other group's answers")
}
answers, err := s.answerRepo.FindByProblemAndUserGroup(probid, userGroupID)
if err != nil {
return nil, err
}
if !group.IsFullAccess {
now := time.Now()
for _, ans := range answers {
if s.preRoundMode {
ans.Point = nil
}
if !now.After(ans.CreatedAt.Add(s.answerLimit)) {
ans.Point = nil
}
}
}
return answers, nil
}
func (s *AnswerService) Update(id uuid.UUID, req *UpdateAnswerRequest) (*entity.Answer, error) {
ans, err := s.answerRepo.FindByID(id)
if err != nil {
return nil, err
}
if ans == nil {
return nil, errors.New("answer not found")
}
problem, err := s.problemRepo.FindByID(ans.ProblemID)
if err != nil {
return nil, e.NewInternalServerError(err)
}
if problem == nil {
return nil, e.NewInternalServerError(fmt.Errorf("problem %s bound answer %s is not found", ans.ProblemID, ans.ID))
}
if !(req.Point <= problem.Point) {
return nil, e.NewBadRequestError("invalid point")
}
ans.Point = &req.Point
return s.answerRepo.Update(ans)
}
func (s *AnswerService) Delete(id uuid.UUID) error {
ans, err := s.answerRepo.FindByID(id)
if err != nil {
return err
}
return s.answerRepo.Delete(ans)
}
func contains(s []uint, e uint) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}