2024-06-09 09:31:30 -07:00
|
|
|
package handlers
|
2021-12-27 09:54:27 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
|
2024-07-17 19:58:32 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
|
2021-12-27 09:54:27 -08:00
|
|
|
)
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
const routeNameSearch = "search"
|
|
|
|
|
2021-12-27 09:54:27 -08:00
|
|
|
type (
|
2024-06-09 09:31:30 -07:00
|
|
|
Search struct {
|
2024-06-15 12:34:24 -07:00
|
|
|
*services.TemplateRenderer
|
2021-12-27 09:54:27 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-06-09 09:31:30 -07:00
|
|
|
func init() {
|
|
|
|
Register(new(Search))
|
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Search) Init(c *services.Container) error {
|
|
|
|
h.TemplateRenderer = c.TemplateRenderer
|
2024-06-09 09:31:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Search) Routes(g *echo.Group) {
|
|
|
|
g.GET("/search", h.Page).Name = routeNameSearch
|
2024-06-09 09:31:30 -07:00
|
|
|
}
|
|
|
|
|
2024-06-15 12:34:24 -07:00
|
|
|
func (h *Search) Page(ctx echo.Context) error {
|
|
|
|
p := page.New(ctx)
|
2024-07-27 12:28:45 -07:00
|
|
|
p.LayoutComponent = layouts.Main
|
2021-12-27 09:54:27 -08:00
|
|
|
|
|
|
|
// Fake search results
|
2024-07-17 19:58:32 -07:00
|
|
|
var results []pages.SearchResult
|
2021-12-27 09:54:27 -08:00
|
|
|
if search := ctx.QueryParam("query"); search != "" {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
title := "Lorem ipsum example ddolor sit amet"
|
|
|
|
index := rand.Intn(len(title))
|
|
|
|
title = title[:index] + search + title[index:]
|
2024-07-17 19:58:32 -07:00
|
|
|
results = append(results, pages.SearchResult{
|
2021-12-27 09:54:27 -08:00
|
|
|
Title: title,
|
|
|
|
URL: fmt.Sprintf("https://www.%s.com", search),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 19:58:32 -07:00
|
|
|
component := pages.Search(p, results)
|
|
|
|
return h.RenderPageTempl(ctx, p, component)
|
2021-12-27 09:54:27 -08:00
|
|
|
}
|