Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions api/controllers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"errors"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -295,15 +296,15 @@ func courseProfessor(flag string, c *gin.Context) {
if courseQuery, err = getCourseQuery(flag, c); err != nil {
return
}

fmt.Println("getCourseQuery error:", courseQuery)
// determine the offset and limit for pagination stage and delete
// "offset" field in professorQuery
paginateMap, err := configs.GetAggregateLimit(&courseQuery, c)
if err != nil {
respond(c, http.StatusBadRequest, "Error offset is not type integer", err.Error())
return
}

fmt.Println("getCourseQuery error:", paginateMap)
// Pipeline to query the professors from the filtered courses
courseProfessorPipeline := mongo.Pipeline{
// filter the courses
Expand Down Expand Up @@ -337,15 +338,19 @@ func courseProfessor(flag string, c *gin.Context) {

// replace the courses with professors
bson.D{{Key: "$replaceWith", Value: "$professors"}},

bson.D{{Key: "$group", Value: bson.D{ // Groups all documents by professords _id
{Key: "_id", Value: "$_id"},
{Key: "doc", Value: bson.D{{Key: "$first", Value: "$$ROOT"}}}, // Keeps first full document
}}},
bson.D{{Key: "$replaceWith", Value: "$doc"}},
// keep order deterministic between calls
bson.D{{Key: "$sort", Value: bson.D{{Key: "_id", Value: 1}}}},

// paginate the professors
bson.D{{Key: "$skip", Value: paginateMap["latter_offset"]}},
bson.D{{Key: "$limit", Value: paginateMap["limit"]}},
}

fmt.Println(courseProfessorPipeline)
// perform aggregation on the pipeline
cursor, err := courseCollection.Aggregate(ctx, courseProfessorPipeline)
if err != nil {
Expand Down
Loading