From 6d62a19493ecf9d089dc5f52aca6df2917b105c3 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Mon, 27 Dec 2021 12:54:27 -0500 Subject: [PATCH] Added search example. --- routes/router.go | 3 ++ routes/search.go | 44 +++++++++++++++++++++++++++ templates/layouts/main.gohtml | 39 +++++++++++++++++++++--- templates/pages/home.gohtml | 2 +- templates/pages/login.gohtml | 4 +-- templates/pages/register.gohtml | 8 ++--- templates/pages/reset-password.gohtml | 4 +-- templates/pages/search.gohtml | 5 +++ 8 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 routes/search.go create mode 100644 templates/pages/search.gohtml diff --git a/routes/router.go b/routes/router.go index 3b83724..2d3931f 100644 --- a/routes/router.go +++ b/routes/router.go @@ -68,6 +68,9 @@ func navRoutes(c *services.Container, g *echo.Group, ctr controller.Controller) home := Home{Controller: ctr} g.GET("/", home.Get).Name = "home" + search := Search{Controller: ctr} + g.GET("/search", search.Get).Name = "search" + about := About{Controller: ctr} g.GET("/about", about.Get).Name = "about" diff --git a/routes/search.go b/routes/search.go new file mode 100644 index 0000000..a6739ad --- /dev/null +++ b/routes/search.go @@ -0,0 +1,44 @@ +package routes + +import ( + "fmt" + "math/rand" + + "goweb/controller" + + "github.com/labstack/echo/v4" +) + +type ( + Search struct { + controller.Controller + } + + SearchResult struct { + Title string + URL string + } +) + +func (c *Search) Get(ctx echo.Context) error { + page := controller.NewPage(ctx) + page.Layout = "main" + page.Name = "search" + + // Fake search results + var results []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, SearchResult{ + Title: title, + URL: fmt.Sprintf("https://www.%s.com", search), + }) + } + } + page.Data = results + + return c.RenderPage(ctx, page) +} diff --git a/templates/layouts/main.gohtml b/templates/layouts/main.gohtml index 8ad3a7a..5b901c4 100644 --- a/templates/layouts/main.gohtml +++ b/templates/layouts/main.gohtml @@ -7,12 +7,13 @@