Add pages for search

This commit is contained in:
video-prize-ranch 2023-08-10 16:00:42 +00:00
parent 043150d85b
commit 498e9282e9
No known key found for this signature in database
3 changed files with 20 additions and 10 deletions

View File

@ -21,9 +21,9 @@ type SearchResult struct {
RelTime string RelTime string
} }
func (client *Client) Search(query string) ([]SearchResult, error) { func (client *Client) Search(query string, page string) ([]SearchResult, error) {
query = url.QueryEscape(query) query = url.QueryEscape(query)
req, err := http.NewRequest("GET", "https://imgur.com/search?qs=list&q=" + query, nil) req, err := http.NewRequest("GET", "https://imgur.com/search/all/page/" + page + "?scrolled&q_size_is_mpx=off&qs=list&q=" + query, nil)
if err != nil { if err != nil {
return []SearchResult{}, err return []SearchResult{}, err
} }

View File

@ -1,6 +1,8 @@
package pages package pages
import ( import (
"strconv"
"codeberg.org/rimgo/rimgo/utils" "codeberg.org/rimgo/rimgo/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -17,7 +19,7 @@ func HandleSearch(c *fiber.Ctx) error {
return c.Redirect(utils.ImgurRe.ReplaceAllString(query, "")) return c.Redirect(utils.ImgurRe.ReplaceAllString(query, ""))
} }
/*page := "1" page := "0"
if c.Query("page") != "" { if c.Query("page") != "" {
page = c.Query("page") page = c.Query("page")
} }
@ -28,21 +30,21 @@ func HandleSearch(c *fiber.Ctx) error {
} }
displayPrevPage := true displayPrevPage := true
if page == "1" { if page == "0" {
displayPrevPage = false displayPrevPage = false
}*/ }
results, err := ApiClient.Search(query) results, err := ApiClient.Search(query, page)
if err != nil { if err != nil {
return err return err
} }
return c.Render("search", fiber.Map{ return c.Render("search", fiber.Map{
"query": query, "query": query,
"results": results, "results": results,
/*"page": page, "page": pageNumber + 1,
"displayPrev": displayPrevPage, "displayPrev": displayPrevPage,
"nextPage": pageNumber + 1, "nextPage": pageNumber + 1,
"prevPage": pageNumber - 1,*/ "prevPage": pageNumber - 1,
}) })
} }

View File

@ -20,6 +20,14 @@
{{> partials/result }} {{> partials/result }}
{{/each}} {{/each}}
</div> </div>
<div class="flex justify-between mt-4 font-bold">
{{#if displayPrev}}
<a href="/search?q={{query}}&page={{prevPage}}">Previous page</a>
{{/if}}
<p>Page {{page}}</p>
<a href="/search?q={{query}}&page={{nextPage}}">Next page</a>
</div>
</main> </main>
{{> partials/footer }} {{> partials/footer }}