saasitone/pkg/handlers/search.go

57 lines
1.2 KiB
Go
Raw Permalink Normal View History

package handlers
2021-12-27 09:54:27 -08:00
import (
"fmt"
"math/rand"
"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
)
const routeNameSearch = "search"
2021-12-27 09:54:27 -08:00
type (
Search struct {
*services.TemplateRenderer
2021-12-27 09:54:27 -08:00
}
)
func init() {
Register(new(Search))
}
func (h *Search) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
return nil
}
func (h *Search) Routes(g *echo.Group) {
g.GET("/search", h.Page).Name = routeNameSearch
}
func (h *Search) Page(ctx echo.Context) error {
p := page.New(ctx)
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
}