package handlers import ( "fmt" "math/rand" "github.com/labstack/echo/v4" "git.grosinger.net/tgrosinger/saasitone/pkg/page" "git.grosinger.net/tgrosinger/saasitone/pkg/services" "git.grosinger.net/tgrosinger/saasitone/templ/layouts" "git.grosinger.net/tgrosinger/saasitone/templ/pages" ) const routeNameSearch = "search" type ( Search struct { *services.TemplateRenderer } ) 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 // Fake search results var results []pages.SearchResult 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:] results = append(results, pages.SearchResult{ Title: title, URL: fmt.Sprintf("https://www.%s.com", search), }) } } component := pages.Search(p, results) return h.RenderPageTempl(ctx, p, component) }