Skip to content

Commit 6ae8470

Browse files
committed
Minor refactor
1 parent 09a3880 commit 6ae8470

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

api/controllers/course.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
var courseCollection *mongo.Collection = configs.GetCollection("courses")
19+
var trendsCourseCollection *mongo.Collection = configs.GetCollection("trends_course_sections")
1920

2021
// @Id courseSearch
2122
// @Router /course [get]
@@ -418,9 +419,8 @@ func TrendsCourseSectionSearch(c *gin.Context) {
418419
bson.D{{Key: "$sort", Value: bson.D{{Key: "_id", Value: 1}}}},
419420
}
420421

421-
trendsCollection := configs.GetCollection("trends_course_sections")
422422
// perform aggregation on the pipeline
423-
cursor, err := trendsCollection.Aggregate(ctx, pipeline)
423+
cursor, err := trendsCourseCollection.Aggregate(ctx, pipeline)
424424
if err != nil {
425425
// return error for any aggregation problem
426426
respondWithInternalError(c, err)

api/controllers/grades.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,11 @@ func gradesAggregation(flag string, c *gin.Context) {
203203
{Key: "subject_prefix", Value: prefix},
204204
{Key: "course_number", Value: number},
205205
}
206-
// Parse the queried document into the sample course
206+
207207
err = courseCollection.FindOne(ctx, sampleCourseFind).Decode(&sampleCourse)
208-
// If the error is not that there is no matching documents, throw an internal server error
209208
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
209+
// If the error is not that there is no matching documents,
210+
// throw an internal server error
210211
respondWithInternalError(c, err)
211212
return
212213
}
@@ -216,24 +217,25 @@ func gradesAggregation(flag string, c *gin.Context) {
216217
typeRegexes := [14]string{"0[0-9][0-9]", "0W[0-9]", "0H[0-9]", "0L[0-9]", "5H[0-9]", "1[0-9][0-9]", "2[0-9][0-9]", "3[0-9][0-9]", "5[0-9][0-9]", "6[0-9][0-9]", "7[0-9][0-9]", "HN[0-9]", "HON", "[0-9]U[0-9]"}
217218
typeStrings := [14]string{"0xx", "0Wx", "0Hx", "0Lx", "5Hx", "1xx", "2xx", "3xx", "5xx", "6xx", "7xx", "HNx", "HON", "xUx"}
218219

219-
var branches []bson.D // for without section pipeline
220-
var withSectionBranches []bson.D // for with section pipeline
220+
var branches [14]bson.D // for without-section pipeline
221+
var withSectionBranches [14]bson.D // for with-section pipeline
222+
221223
for i, typeRegex := range typeRegexes {
222-
branches = append(branches, bson.D{
224+
branches[i] = bson.D{
223225
{Key: "case", Value: bson.D{{Key: "$regexMatch", Value: bson.D{
224226
{Key: "input", Value: "$sections.section_number"},
225227
{Key: "regex", Value: typeRegex},
226228
}}}},
227229
{Key: "then", Value: typeStrings[i]},
228-
})
230+
}
229231

230-
withSectionBranches = append(withSectionBranches, bson.D{
232+
withSectionBranches[i] = bson.D{
231233
{Key: "case", Value: bson.D{{Key: "$regexMatch", Value: bson.D{
232234
{Key: "input", Value: "$section_number"},
233235
{Key: "regex", Value: typeRegex},
234236
}}}},
235237
{Key: "then", Value: typeStrings[i]},
236-
})
238+
}
237239
}
238240

239241
// Stage to look up sections
@@ -293,7 +295,7 @@ func gradesAggregation(flag string, c *gin.Context) {
293295
{Key: "ix", Value: "$ix"},
294296
}
295297
if flag == "section_type" {
296-
// add section_type to _id to group grades by both academic_session and section_type
298+
// Add section_type to _id to group grades by both academic_session and section_type
297299
groupID = append(groupID, bson.E{Key: "section_type", Value: "$section_type"})
298300
}
299301
groupGradesStage := bson.D{
@@ -320,7 +322,7 @@ func gradesAggregation(flag string, c *gin.Context) {
320322
// Stage to group grade distribution
321323
var groupDistributionID any = "$_id.academic_session"
322324
if flag == "section_type" {
323-
// add the section-type criteria
325+
// Add the section-type criteria
324326
groupDistributionID = bson.D{
325327
{Key: "academic_section", Value: "$_id.academic_session"},
326328
{Key: "section_type", Value: "$_id.section_type"},

api/controllers/professor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
var professorCollection *mongo.Collection = configs.GetCollection("professors")
19+
var trendsProfCollection *mongo.Collection = configs.GetCollection("trends_prof_sections")
1920

2021
// @Id professorSearch
2122
// @Router /professor [get]
@@ -454,9 +455,7 @@ func TrendsProfessorSectionSearch(c *gin.Context) {
454455
bson.D{{Key: "$sort", Value: bson.D{{Key: "_id", Value: 1}}}},
455456
}
456457

457-
trendsCollection := configs.GetCollection("trends_prof_sections")
458-
459-
cursor, err := trendsCollection.Aggregate(ctx, pipeline)
458+
cursor, err := trendsProfCollection.Aggregate(ctx, pipeline)
460459
if err != nil {
461460
respondWithInternalError(c, err)
462461
return

0 commit comments

Comments
 (0)