Archived
Template
1
0
This repository has been archived on 2023-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
Files
golang-base-project/routes/search.go

48 lines
1.0 KiB
Go
Raw Normal View History

2021-12-12 14:56:13 +01:00
package routes
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/uberswe/golang-base-project/models"
"log"
"net/http"
)
// SearchData holds additional data needed to render the search HTML page
2021-12-12 14:56:13 +01:00
type SearchData struct {
PageData
Results []models.Website
}
// Search renders the search HTML page and any search results
2021-12-12 14:56:13 +01:00
func (controller Controller) Search(c *gin.Context) {
2022-01-29 10:21:05 +01:00
pdS := controller.DefaultPageData(c)
pdS.Title = pdS.Trans("Search")
2021-12-12 14:56:13 +01:00
pd := SearchData{
2022-01-29 10:21:05 +01:00
PageData: pdS,
2021-12-12 14:56:13 +01:00
}
search := c.PostForm("search")
var results []models.Website
log.Println(search)
search = fmt.Sprintf("%s%s%s", "%", search, "%")
log.Println(search)
res := controller.db.Where("title LIKE ? OR description LIKE ?", search, search).Find(&results)
if res.Error != nil || len(results) == 0 {
pd.Messages = append(pd.Messages, Message{
Type: "error",
2022-01-29 10:21:05 +01:00
Content: pdS.Trans("No results found"),
2021-12-12 14:56:13 +01:00
})
log.Println(res.Error)
c.HTML(http.StatusOK, "search.html", pd)
return
}
pd.Results = results
c.HTML(http.StatusOK, "search.html", pd)
}