feat: hashmap with SO sort values, ssr selection

This commit is contained in:
httpjamesm 2023-02-04 13:05:10 -05:00
parent d7bca93af8
commit eee8ec42c5
No known key found for this signature in database
2 changed files with 24 additions and 6 deletions

View File

@ -21,6 +21,13 @@ import (
var codeBlockRegex = regexp.MustCompile(`(?s)<pre><code>(.+?)<\/code><\/pre>`) var codeBlockRegex = regexp.MustCompile(`(?s)<pre><code>(.+?)<\/code><\/pre>`)
var questionCodeBlockRegex = regexp.MustCompile(`(?s)<pre class=".+"><code( class=".+")?>(.+?)</code></pre>`) var questionCodeBlockRegex = regexp.MustCompile(`(?s)<pre class=".+"><code( class=".+")?>(.+?)</code></pre>`)
var soSortValues = map[string]string{
"votes": "scoredesc",
"trending": "trending",
"newest": "modifieddesc",
"oldest": "createdasc",
}
func ViewQuestion(c *gin.Context) { func ViewQuestion(c *gin.Context) {
client := resty.New() client := resty.New()
@ -36,7 +43,17 @@ func ViewQuestion(c *gin.Context) {
questionTitle := c.Param("title") 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) resp, err := client.R().Get(soLink)
if err != nil { if err != nil {
@ -251,6 +268,7 @@ func ViewQuestion(c *gin.Context) {
"imagePolicy": imagePolicy, "imagePolicy": imagePolicy,
"theme": c.MustGet("theme").(string), "theme": c.MustGet("theme").(string),
"currentUrl": fmt.Sprintf("%s/questions/%s/%s", os.Getenv("APP_URL"), questionId, questionTitle), "currentUrl": fmt.Sprintf("%s/questions/%s/%s", os.Getenv("APP_URL"), questionId, questionTitle),
"sortValue": sortValue,
}) })
} }

View File

@ -47,11 +47,11 @@
<div class="sorting"> <div class="sorting">
<form> <form>
<select name="sort_by"> <select name="sort_by">
<option disabled value="votes">Sort answers by...</option> <option disabled value="">Sort answers by...</option>
<option value="votes">Votes</option> <option value="votes"{{ if eq .sortValue "votes" }} selected{{ end }}>Votes</option>
<option value="newest">Trending</option> <option value="trending"{{ if eq .sortValue "trending" }} selected{{ end }}>Trending</option>
<option value="newest">Newest</option> <option value="newest"{{ if eq .sortValue "newest" }} selected{{ end }}>Newest</option>
<option value="oldest">Oldest</option> <option value="oldest"{{ if eq .sortValue "oldest" }} selected{{ end }}>Oldest</option>
</select> </select>
<button type="submit"> <button type="submit">
<img <img