Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from uberswe/issue-15
Browse files Browse the repository at this point in the history
Adds search pagination
  • Loading branch information
uberswe authored Jan 29, 2022
2 parents 1b52e86 + b9a591a commit a2a1f03
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
25 changes: 25 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ func seed(db *gorm.DB) {
Description: "Learn how to install Go on your machine and read the documentation on the Go website.",
URL: "https://go.dev/learn/",
},
{
Title: "Uberswe on Github",
Description: "I am the creator of Golang Base Project. This is my Github profile.",
URL: "https://github.com/uberswe",
},
{
Title: "Tournify",
Description: "A website to create tournaments or free which uses this project as a base.",
URL: "https://tournify.io",
},
{
Title: "GORM",
Description: "The fantastic ORM library for Golang.",
URL: "https://gorm.io/",
},
{
Title: "Bootstrap",
Description: "Quickly design and customize responsive mobile-first sites with Bootstrap, the world’s most popular front-end open source toolkit, featuring Sass variables and mixins, responsive grid system, extensive prebuilt components, and powerful JavaScript plugins.",
URL: "https://getbootstrap.com/",
},
{
Title: "Gin Web Framework",
Description: "Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.",
URL: "https://github.com/gin-gonic/gin",
},
}

for _, w := range websites {
Expand Down
8 changes: 8 additions & 0 deletions dist/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ <h3>{{ $result.Title }}</h3>
<p><a href="{{ $result.URL }}">{{ $result.URL }}</a></p>
</div>
{{ end }}
<div class="pagination">
{{ if .Prev }}
<a href="{{ .PrevURL }}">{{ call .Trans "Previous" }}</a>
{{ end }}
{{ if .Next }}
<a href="{{ .NextURL }}">{{ call .Trans "Next" }}</a>
{{ end }}
</div>
</div>
</main>

Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func Run() {
// We want to handle both POST and GET requests on the /search route. We define both but use the same function to handle the requests.
r.GET("/search", controller.Search)
r.POST("/search", controller.Search)
r.Any("/search/:page", controller.Search)
r.Any("/search/:page/:query", controller.Search)

// We define our 404 handler for when a page can not be found
r.NoRoute(controller.NoRoute)
Expand Down
37 changes: 33 additions & 4 deletions routes/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,51 @@ import (
"github.com/uberswe/golang-base-project/models"
"log"
"net/http"
"net/url"
"strconv"
)

// SearchData holds additional data needed to render the search HTML page
type SearchData struct {
PageData
Results []models.Website
Prev bool
Next bool
PrevURL string
NextURL string
}

// Search renders the search HTML page and any search results
func (controller Controller) Search(c *gin.Context) {
page := 1
resultsPerPage := 5
pdS := controller.DefaultPageData(c)
pdS.Title = pdS.Trans("Search")
pd := SearchData{
PageData: pdS,
}
search := c.PostForm("search")
search := ""
if c.Request.Method == "POST" && c.Request.RequestURI == "/search" {
search = c.PostForm("search")
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("/search/1/%s", url.QueryEscape(search)))
return
} else {
search = c.Param("query")
if i, err := strconv.Atoi(c.Param("page")); err == nil {
page = i
}
}

var results []models.Website

log.Println(search)
search = fmt.Sprintf("%s%s%s", "%", search, "%")
searchFilter := fmt.Sprintf("%s%s%s", "%", search, "%")
search2 := fmt.Sprintf("%s%s", "%", search)
search4 := fmt.Sprintf("%s%s", search, "%")

log.Println(search)
res := controller.db.Where("title LIKE ? OR description LIKE ?", search, search).Find(&results)
res := controller.db.
Raw(fmt.Sprintf("SELECT * FROM websites WHERE title LIKE ? OR description LIKE ? ORDER BY CASE WHEN title LIKE ? OR description LIKE ? THEN 1 WHEN title LIKE ? OR description LIKE ? THEN 2 WHEN title LIKE ? OR description LIKE ? THEN 4 ELSE 3 END LIMIT %d OFFSET %d", resultsPerPage, resultsPerPage*(page-1)), searchFilter, searchFilter, search, search, search2, search2, search4, search4).
Find(&results)

if res.Error != nil || len(results) == 0 {
pd.Messages = append(pd.Messages, Message{
Expand All @@ -42,6 +63,14 @@ func (controller Controller) Search(c *gin.Context) {
}

pd.Results = results
if len(pd.Results) >= resultsPerPage {
pd.Next = true
pd.NextURL = fmt.Sprintf("/search/%d/%s", page+1, url.QueryEscape(search))
}
if page > 1 {
pd.Prev = true
pd.PrevURL = fmt.Sprintf("/search/%d/%s", page-1, url.QueryEscape(search))
}

c.HTML(http.StatusOK, "search.html", pd)
}

0 comments on commit a2a1f03

Please sign in to comment.