/
exam.go
193 lines (150 loc) · 5.48 KB
/
exam.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
package db
import (
"context"
"github.com/obcode/plexams.go/graph/model"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Deprecated: rm me
func (db *DB) AddAdditionalExam(ctx context.Context, exam model.AdditionalExamInput) (bool, error) {
collection := db.Client.Database(db.databaseName).Collection(collectionNameAdditionalExams)
_, err := collection.InsertOne(ctx, exam)
if err != nil {
return false, err
}
return true, nil
}
// Deprecated: rm me
func (db *DB) AdditionalExams(ctx context.Context) ([]*model.AdditionalExam, error) {
collection := db.Client.Database(db.databaseName).Collection(collectionNameAdditionalExams)
exams := make([]*model.AdditionalExam, 0)
findOptions := options.Find()
findOptions.SetSort(bson.D{{Key: "ancode", Value: 1}})
cur, err := collection.Find(ctx, bson.M{}, findOptions)
if err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameAdditionalExams).Msg("MongoDB Find")
return exams, err
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var exam model.AdditionalExam
err := cur.Decode(&exam)
if err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameAdditionalExams).Interface("cur", cur).
Msg("Cannot decode to additional exam")
return exams, err
}
exams = append(exams, &exam)
}
if err := cur.Err(); err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameAdditionalExams).Msg("Cursor returned error")
return exams, err
}
return exams, nil
}
func (db *DB) ExamsAlreadyPrepared(ctx context.Context) bool {
collection := db.Client.Database(db.databaseName).Collection("exams")
docsCount, err := collection.CountDocuments(ctx, bson.D{})
if err != nil {
log.Error().Err(err).Msg("cannot count exams in db")
}
return docsCount != 0
}
// func (db *DB) AddExam(ctx context.Context, exam *model.Exam) error {
// collection := db.Client.Database(db.databaseName).Collection("exams")
// result := collection.FindOne(ctx, bson.D{{Key: "ancode", Value: exam.AnCode}})
// if result.Err() == nil {
// log.Error().Int("ancode", exam.AnCode).Msg("cannot add exam, exam with ancode already in db")
// return fmt.Errorf("cannot add exam, exam with ancode %d already in db", exam.AnCode)
// }
// if result.Err() == mongo.ErrNoDocuments {
// _, err := collection.InsertOne(ctx, exam)
// if err != nil {
// return err
// }
// return nil
// }
// return result.Err()
// }
func (db *DB) SaveExamsWithRegs(ctx context.Context, exams []*model.ExamWithRegs) error {
collection := db.Client.Database(db.databaseName).Collection(collectionNameExamsWithRegs)
err := collection.Drop(ctx)
if err != nil {
log.Error().Err(err).
Str("collectionName", collectionNameExamsWithRegs).
Msg("cannot drop collection")
return err
}
examsToInsert := make([]interface{}, 0, len(exams))
for _, exam := range exams {
examsToInsert = append(examsToInsert, exam)
}
_, err = collection.InsertMany(ctx, examsToInsert)
if err != nil {
log.Error().Err(err).
Str("collectionName", collectionNameExamsWithRegs).
Msg("cannot insert exams")
return err
}
return nil
}
func (db *DB) ExamWithRegs(ctx context.Context, ancode int) (*model.ExamWithRegs, error) {
collection := db.Client.Database(db.databaseName).Collection(collectionNameExamsWithRegs)
res := collection.FindOne(ctx, bson.D{{Key: "ancode", Value: ancode}})
if res.Err() != nil {
log.Error().Err(res.Err()).Int("ancode", ancode).Msg("no constraint found")
return nil, nil // no constraint available
}
var exam model.ExamWithRegs
err := res.Decode(&exam)
if err != nil {
log.Error().Err(res.Err()).Int("ancode", ancode).Msg("cannot decode constraint")
return nil, err
}
return &exam, nil
}
func (db *DB) ExamsWithRegs(ctx context.Context) ([]*model.ExamWithRegs, error) {
collection := db.Client.Database(db.databaseName).Collection(collectionNameExamsWithRegs)
exams := make([]*model.ExamWithRegs, 0)
findOptions := options.Find()
findOptions.SetSort(bson.D{{Key: "ancode", Value: 1}})
cur, err := collection.Find(ctx, bson.M{}, findOptions)
if err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameExamsWithRegs).Msg("MongoDB Find")
return exams, err
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var exam model.ExamWithRegs
err := cur.Decode(&exam)
if err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameExamsWithRegs).Interface("cur", cur).
Msg("Cannot decode to additional exam")
return exams, err
}
exams = append(exams, &exam)
}
if err := cur.Err(); err != nil {
log.Error().Err(err).Str("semester", db.semester).Str("collection", collectionNameExamsWithRegs).Msg("Cursor returned error")
return exams, err
}
return exams, nil
}
func (db *DB) ExamsInPlan(ctx context.Context) ([]*model.ExamInPlan, error) {
collection := db.getCollectionSemester(collectionExamsInPlan)
findOptions := options.Find()
findOptions.SetSort(bson.D{{Key: "slot.starttime", Value: 1}, {Key: "exam.ancode", Value: 1}})
cur, err := collection.Find(ctx, bson.M{}, findOptions)
if err != nil {
log.Error().Err(err).Msg("error while trying to find exams in plan")
}
defer cur.Close(ctx)
exams := make([]*model.ExamInPlan, 0)
err = cur.All(ctx, &exams)
if err != nil {
log.Error().Err(err).Str("collection", collectionRooms).Msg("Cannot decode to rooms for exams")
return nil, err
}
return exams, nil
}