Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 26 additions & 13 deletions api/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func ConnectDB() *mongo.Client {
dbInstance = &DBSingleton{
client: client,
}

})

return dbInstance.client
Expand Down Expand Up @@ -81,24 +80,38 @@ func GetOptionLimit(query *bson.M, c *gin.Context) (*options.FindOptions, error)
return options.Find().SetSkip(offset).SetLimit(limit), err
}

// TODO: Is there a chance we can combine this with GetOptionLimit to reduce repretiveness ?
// Returns pairs of the offset and limit for pagination stage for aggregate endpoints pipeline
// returns (offset, limit, err)
func GetAggregateLimit(query *bson.M, c *gin.Context) (int64, int64, error) {
delete(*query, "offset") // remove offset field (if present) in the query
// Returns the offsets and limit for pagination stage for aggregate endpoints pipeline
// (former offset, latter offset, limit, err)
func GetAggregateLimit(query *bson.M, c *gin.Context) (int64, int64, int64, error) {
// remove formerOffset and latterOffset field (if present) in the query
delete(*query, "former_offset")
delete(*query, "latter_offset")

// parses offset if included in the query
var limit int64 = GetEnvLimit()
var offset int64
var formerOffset, latterOffset int64
var err error

if c.Query("offset") == "" {
offset = 0 // default value
var limit int64 = GetEnvLimit()

// get the offset on the "former" part of the endpoint
if c.Query("former_offset") == "" {
formerOffset = 0
} else {
offset, err = strconv.ParseInt(c.Query("offset"), 10, 64)
formerOffset, err = strconv.ParseInt(c.Query("former_offset"), 10, 64)
if err != nil {
return offset, limit, err // default value
return 0, 0, limit, err
}
}
return offset, limit, err

// get offset on the "latter" part of the endpoint
if c.Query("latter_offset") == "" {
latterOffset = 0
} else {
latterOffset, err = strconv.ParseInt(c.Query("latter_offset"), 10, 64)
if err != nil {
return 0, 0, limit, err
}
}

return formerOffset, latterOffset, limit, err
}
18 changes: 14 additions & 4 deletions api/controllers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -75,6 +76,7 @@ func CourseSearch(c *gin.Context) {
}

// return result
log.Logger.Print(len(courses))
c.JSON(http.StatusOK, responses.MultiCourseResponse{Status: http.StatusOK, Message: "success", Data: courses})
}

Expand Down Expand Up @@ -200,14 +202,16 @@ func courseSection(flag string, c *gin.Context) {
}
courseQuery = bson.M{"_id": courseObjId}
} else {
err = errors.New("broken endpoint")
// otherwise, something that messed up the server
c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "internal error", Data: "broken endpoint"})
c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "internal error", Data: err.Error()})
return
}

// determine the offset and limit for pagination stage
// and delete "offset" field in professorQuery
offset, limit, err := configs.GetAggregateLimit(&courseQuery, c)
formerOffset, latterOffset, limit, err := configs.GetAggregateLimit(&courseQuery, c)

if err != nil {
log.WriteErrorWithMsg(err, log.OffsetNotTypeInteger)
c.JSON(http.StatusConflict, responses.ErrorResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
Expand All @@ -220,8 +224,8 @@ func courseSection(flag string, c *gin.Context) {
bson.D{{Key: "$match", Value: courseQuery}},

// paginate the courses before pulling the sections from thoses courses
bson.D{{Key: "$skip", Value: offset}}, // skip to the specified offset
bson.D{{Key: "$limit", Value: limit}}, // limit to the specified number of courses
bson.D{{Key: "$skip", Value: formerOffset}}, // skip to the specified offset
bson.D{{Key: "$limit", Value: limit}}, // limit to the specified number of courses

// lookup the sections of the courses
bson.D{{Key: "$lookup", Value: bson.D{
Expand All @@ -239,6 +243,10 @@ func courseSection(flag string, c *gin.Context) {

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

// paginate the sections
bson.D{{Key: "$skip", Value: latterOffset}},
bson.D{{Key: "$limit", Value: limit}},
}

// perform aggregation on the pipeline
Expand All @@ -253,5 +261,7 @@ func courseSection(flag string, c *gin.Context) {
if err = cursor.All(ctx, &courseSections); err != nil {
panic(err)
}

log.Logger.Print(len(courseSections))
c.JSON(http.StatusOK, responses.MultiSectionResponse{Status: http.StatusOK, Message: "success", Data: courseSections})
}
Loading