You've already forked golang-base-project
Adds search pagination
This commit is contained in:
25
database.go
25
database.go
@ -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.",
|
Description: "Learn how to install Go on your machine and read the documentation on the Go website.",
|
||||||
URL: "https://go.dev/learn/",
|
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 {
|
for _, w := range websites {
|
||||||
|
8
dist/templates/search.html
vendored
8
dist/templates/search.html
vendored
@ -11,6 +11,14 @@
|
|||||||
<p><a href="{{ $result.URL }}">{{ $result.URL }}</a></p>
|
<p><a href="{{ $result.URL }}">{{ $result.URL }}</a></p>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ 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>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
2
main.go
2
main.go
@ -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.
|
// 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.GET("/search", controller.Search)
|
||||||
r.POST("/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
|
// We define our 404 handler for when a page can not be found
|
||||||
r.NoRoute(controller.NoRoute)
|
r.NoRoute(controller.NoRoute)
|
||||||
|
@ -6,30 +6,51 @@ import (
|
|||||||
"github.com/uberswe/golang-base-project/models"
|
"github.com/uberswe/golang-base-project/models"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SearchData holds additional data needed to render the search HTML page
|
// SearchData holds additional data needed to render the search HTML page
|
||||||
type SearchData struct {
|
type SearchData struct {
|
||||||
PageData
|
PageData
|
||||||
Results []models.Website
|
Results []models.Website
|
||||||
|
Prev bool
|
||||||
|
Next bool
|
||||||
|
PrevURL string
|
||||||
|
NextURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search renders the search HTML page and any search results
|
// Search renders the search HTML page and any search results
|
||||||
func (controller Controller) Search(c *gin.Context) {
|
func (controller Controller) Search(c *gin.Context) {
|
||||||
|
page := 1
|
||||||
|
resultsPerPage := 5
|
||||||
pdS := controller.DefaultPageData(c)
|
pdS := controller.DefaultPageData(c)
|
||||||
pdS.Title = pdS.Trans("Search")
|
pdS.Title = pdS.Trans("Search")
|
||||||
pd := SearchData{
|
pd := SearchData{
|
||||||
PageData: pdS,
|
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
|
var results []models.Website
|
||||||
|
|
||||||
log.Println(search)
|
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.
|
||||||
res := controller.db.Where("title LIKE ? OR description LIKE ?", search, search).Find(&results)
|
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 {
|
if res.Error != nil || len(results) == 0 {
|
||||||
pd.Messages = append(pd.Messages, Message{
|
pd.Messages = append(pd.Messages, Message{
|
||||||
@ -42,6 +63,14 @@ func (controller Controller) Search(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pd.Results = results
|
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)
|
c.HTML(http.StatusOK, "search.html", pd)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user