From eee8ec42c580de229c92be4f451ce4d5ab0e7323 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Sat, 4 Feb 2023 13:05:10 -0500 Subject: [PATCH] feat: hashmap with SO sort values, ssr selection --- src/routes/question.go | 20 +++++++++++++++++++- templates/question.html | 10 +++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/routes/question.go b/src/routes/question.go index 36d8fdf..0571153 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -21,6 +21,13 @@ import ( var codeBlockRegex = regexp.MustCompile(`(?s)
(.+?)<\/code><\/pre>`)
 var questionCodeBlockRegex = regexp.MustCompile(`(?s)
(.+?)
`) +var soSortValues = map[string]string{ + "votes": "scoredesc", + "trending": "trending", + "newest": "modifieddesc", + "oldest": "createdasc", +} + func ViewQuestion(c *gin.Context) { client := resty.New() @@ -36,7 +43,17 @@ func ViewQuestion(c *gin.Context) { questionTitle := c.Param("title") - soLink := fmt.Sprintf("https://stackoverflow.com/questions/%s/%s", questionId, questionTitle) + sortValue := c.Query("sort_by") + if sortValue == "" { + sortValue = "votes" + } + + soSortValue, ok := soSortValues[sortValue] + if !ok { + soSortValue = soSortValues["votes"] + } + + soLink := fmt.Sprintf("https://stackoverflow.com/questions/%s/%s?answertab=%s", questionId, questionTitle, soSortValue) resp, err := client.R().Get(soLink) if err != nil { @@ -251,6 +268,7 @@ func ViewQuestion(c *gin.Context) { "imagePolicy": imagePolicy, "theme": c.MustGet("theme").(string), "currentUrl": fmt.Sprintf("%s/questions/%s/%s", os.Getenv("APP_URL"), questionId, questionTitle), + "sortValue": sortValue, }) } diff --git a/templates/question.html b/templates/question.html index 06e39eb..d62bc50 100644 --- a/templates/question.html +++ b/templates/question.html @@ -47,11 +47,11 @@