Skip to content

Commit

Permalink
Reordered code segments
Browse files Browse the repository at this point in the history
  • Loading branch information
kervyntan committed Sep 28, 2024
1 parent 129e2aa commit 5ed4852
Showing 1 changed file with 49 additions and 47 deletions.
96 changes: 49 additions & 47 deletions peer-prep-be/src/controllers/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5ed4852

Please sign in to comment.