/
passageTask.go
93 lines (81 loc) · 2.79 KB
/
passageTask.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
package models
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
"github.com/pmpavl/tsyst/pkg/constants"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type PassageTask struct {
Task `json:",inline" bson:",inline"` // Задача
Points constants.Points `json:"points" bson:"points"` // Баллов за задачу
Correct bool `json:"correct" bson:"correct"` // Правильность ответа
AnswerUser string `json:"answerUser,omitempty" bson:"answerUser,omitempty"` // Ответ пользователя
TimeSpent constants.Duration `json:"timeSpent,omitempty" bson:"timeSpent,omitempty"` // Времени потрачено
}
func NewPassageTask(task *Task, points constants.Points) *PassageTask {
return &PassageTask{
Task: *task,
Points: points,
Correct: false,
}
}
func (t *PassageTask) IsAnswered() bool { return t.AnswerUser != "" }
func (t *PassageTask) IsCorrect() bool { return strings.EqualFold(t.Answer, t.AnswerUser) }
func (t PassageTask) MarshalBSON() ([]byte, error) {
bytes, err := bson.Marshal(t.marshalBSON())
if err != nil {
return nil, errors.Wrap(err, "bson marshal")
}
return bytes, nil
}
func (t PassageTask) marshalBSON() any {
return &struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Condition string `bson:"condition"`
Answer string `bson:"answer"`
Tags *TaskTags `bson:"tags,omitempty"`
Radio []string `bson:"radio,omitempty"`
Points constants.Points `bson:"points"`
Correct bool `bson:"correct"`
AnswerUser string `bson:"answerUser,omitempty"`
TimeSpent constants.Duration `bson:"timeSpent,omitempty"`
}{
ID: t.ID,
Condition: t.Condition,
Answer: t.Answer,
Tags: t.Tags,
Radio: t.Radio,
Points: t.Points,
Correct: t.Correct,
AnswerUser: t.AnswerUser,
TimeSpent: t.TimeSpent,
}
}
func (t PassageTask) MarshalJSON() ([]byte, error) {
bytes, err := json.Marshal(t.marshalJSON())
if err != nil {
return nil, errors.Wrap(err, "json marshal")
}
return bytes, nil
}
func (t PassageTask) marshalJSON() any {
return &struct {
Condition string `json:"condition"`
Tags *TaskTags `json:"tags,omitempty"`
Radio []string `json:"radio,omitempty"`
Points string `json:"points"`
Correct bool `json:"correct"`
AnswerUser string `json:"answerUser,omitempty"`
TimeSpent string `json:"timeSpent,omitempty"`
}{
Condition: t.Condition,
Tags: t.Tags,
Radio: t.Radio,
Points: t.Points.Readable(),
Correct: t.Correct,
AnswerUser: t.AnswerUser,
TimeSpent: t.TimeSpent.Readable(),
}
}