From 5ed48520e68b984208fc466cafb35546bd8fd5be Mon Sep 17 00:00:00 2001 From: Kervyn <73232587+kervyntan@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:38:03 +0800 Subject: [PATCH] Reordered code segments --- peer-prep-be/src/controllers/question.go | 96 ++++++++++++------------ 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/peer-prep-be/src/controllers/question.go b/peer-prep-be/src/controllers/question.go index c8951b4ec6..51770a06a9 100644 --- a/peer-prep-be/src/controllers/question.go +++ b/peer-prep-be/src/controllers/question.go @@ -23,6 +23,55 @@ var validate = validator.New() var errMessage = "error" var successMessage = "success" +// Helper Functions +// Takes in the sortField and sortOrder from the query string and returns the FindOptions object +func ProcessSortParams(sortField string, sortOrder string) *options.FindOptions { + var findOptions *options.FindOptions + + if sortField != "" { + order := 1 // Default to ascending order + if sortOrder == "desc" { + order = -1 // If 'desc' is provided, sort in descending order + } + + // Set the sorting options + findOptions = options.Find().SetCollation(&options.Collation{Locale: "en_US"}).SetSort(bson.D{{Key: sortField, Value: order}}) + } else { + // No sorting specified, natural MongoDB order + findOptions = options.Find() + } + + return findOptions +} + +func ProcessFilterParams(filterField string, filterValues string) bson.D { + filter := bson.D{{}} + + if filterField != "" && filterValues != "" { + values := strings.Split(filterValues, ",") + + if len(values) == 1 { + filter = bson.D{{Key: filterField, Value: values[0]}} + } else { + filterConditions := bson.A{} + for _, value := range values { + filterConditions = append(filterConditions, bson.D{{Key: filterField, Value: value}}) + } + + filter = bson.D{ + { + Key: "$or", + Value: filterConditions, + }, + } + } + + } + + return filter +} + +// Services func CreateQuestion(c echo.Context) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) var existingQuestion models.Question @@ -229,53 +278,6 @@ func SearchQuestion(c echo.Context) error { return c.JSON(http.StatusOK, responses.StatusResponse{Status: http.StatusOK, Message: successMessage, Data: &echo.Map{"data": questions}}) } -// Takes in the sortField and sortOrder from the query string and returns the FindOptions object -func ProcessSortParams(sortField string, sortOrder string) *options.FindOptions { - var findOptions *options.FindOptions - - if sortField != "" { - order := 1 // Default to ascending order - if sortOrder == "desc" { - order = -1 // If 'desc' is provided, sort in descending order - } - - // Set the sorting options - findOptions = options.Find().SetCollation(&options.Collation{Locale: "en_US"}).SetSort(bson.D{{Key: sortField, Value: order}}) - } else { - // No sorting specified, natural MongoDB order - findOptions = options.Find() - } - - return findOptions -} - -func ProcessFilterParams(filterField string, filterValues string) bson.D { - filter := bson.D{{}} - - if filterField != "" && filterValues != "" { - values := strings.Split(filterValues, ",") - - if len(values) == 1 { - filter = bson.D{{Key: filterField, Value: values[0]}} - } else { - filterConditions := bson.A{} - for _, value := range values { - filterConditions = append(filterConditions, bson.D{{Key: filterField, Value: value}}) - } - - filter = bson.D{ - { - Key: "$or", - Value: filterConditions, - }, - } - } - - } - - return filter -} - func GetDistinctQuestionCategories(c echo.Context) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel()