From 4dd207de9cd8fba9beaa935836d3bf0a7501899f Mon Sep 17 00:00:00 2001 From: NathanP9000 <73033177+NathanP9000@users.noreply.github.com> Date: Thu, 25 Sep 2025 17:13:18 -0500 Subject: [PATCH 1/4] added group function in pipeline to aggregate documents with the same professor id. This removes duplicates. --- api/controllers/course.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/api/controllers/course.go b/api/controllers/course.go index 02428c3..3b090e8 100644 --- a/api/controllers/course.go +++ b/api/controllers/course.go @@ -3,6 +3,7 @@ package controllers import ( "context" "errors" + "fmt" "net/http" "time" @@ -295,7 +296,7 @@ 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) @@ -303,7 +304,7 @@ func courseProfessor(flag string, c *gin.Context) { 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 @@ -337,7 +338,11 @@ 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}}}}, @@ -345,7 +350,7 @@ func courseProfessor(flag string, c *gin.Context) { 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 { From 29ffe3a313661870c56b2d840e557a909ce3a076 Mon Sep 17 00:00:00 2001 From: NathanP9000 <73033177+NathanP9000@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:27:40 -0500 Subject: [PATCH 2/4] Added back the errors import. --- api/controllers/course.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/controllers/course.go b/api/controllers/course.go index 3b090e8..380b552 100644 --- a/api/controllers/course.go +++ b/api/controllers/course.go @@ -340,7 +340,7 @@ func courseProfessor(flag string, c *gin.Context) { 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 + {Key: "doc", Value: bson.D{{Key: "$first", Value: "$$ROOT"}}}, // Keeps first full document }}}, bson.D{{Key: "$replaceWith", Value: "$doc"}}, // keep order deterministic between calls From 42163ed4f4baf6721e49389e8327665a958c2661 Mon Sep 17 00:00:00 2001 From: NathanP9000 <73033177+NathanP9000@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:31:17 -0500 Subject: [PATCH 3/4] No changes were made to my code besides the merge. I removed the errors import, since it is no longer needed. --- api/controllers/course.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/controllers/course.go b/api/controllers/course.go index ab98aa8..cf50d06 100644 --- a/api/controllers/course.go +++ b/api/controllers/course.go @@ -2,7 +2,6 @@ package controllers import ( "context" - "errors" "fmt" "net/http" "time" @@ -29,7 +28,7 @@ var courseCollection *mongo.Collection = configs.GetCollection("courses") // @Param subject_prefix query string false "The course's subject prefix" // @Param title query string false "The course's title" // @Param description query string false "The course's description" -// @Param school query string false "The course's school" +// @Param school query string a false "The course's school" // @Param credit_hours query string false "The number of credit hours awarded by successful completion of the course" // @Param class_level query string false "The level of education that this course course corresponds to" // @Param activity_type query string false "The type of class this course corresponds to" From 8bac8429b0241fb49add73a2cdf8901cdc8c1781 Mon Sep 17 00:00:00 2001 From: NathanP9000 <73033177+NathanP9000@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:46:53 -0500 Subject: [PATCH 4/4] Remove typo in @Param notation. --- api/controllers/course.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/controllers/course.go b/api/controllers/course.go index cf50d06..e4e3e6b 100644 --- a/api/controllers/course.go +++ b/api/controllers/course.go @@ -28,7 +28,7 @@ var courseCollection *mongo.Collection = configs.GetCollection("courses") // @Param subject_prefix query string false "The course's subject prefix" // @Param title query string false "The course's title" // @Param description query string false "The course's description" -// @Param school query string a false "The course's school" +// @Param school query string false "The course's school" // @Param credit_hours query string false "The number of credit hours awarded by successful completion of the course" // @Param class_level query string false "The level of education that this course course corresponds to" // @Param activity_type query string false "The type of class this course corresponds to"