Initial
This commit is contained in:
17
app/handlers/about.go
Normal file
17
app/handlers/about.go
Normal file
@ -0,0 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/templates"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var pcAbout templates.PageContext = templates.PageContext{
|
||||
Title: "Welcome to the demo - About",
|
||||
ActivePage: "about",
|
||||
}
|
||||
|
||||
func About(f *fiber.Ctx) error {
|
||||
Render(f, templates.About(pcAbout))
|
||||
return nil
|
||||
}
|
||||
23
app/handlers/home.go
Normal file
23
app/handlers/home.go
Normal file
@ -0,0 +1,23 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/templates"
|
||||
"fiber-sessions/app/types"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Home(f *fiber.Ctx) error {
|
||||
user := types.User{
|
||||
FirstName: "Pero",
|
||||
LastName: "Perić",
|
||||
}
|
||||
|
||||
pc := templates.PageContext{
|
||||
Title: "Welcome to the demo",
|
||||
ActivePage: "home",
|
||||
User: &user,
|
||||
}
|
||||
Render(f, templates.Home(pc))
|
||||
return nil
|
||||
}
|
||||
91
app/handlers/interactive.go
Normal file
91
app/handlers/interactive.go
Normal file
@ -0,0 +1,91 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/data"
|
||||
"fiber-sessions/app/templates"
|
||||
"fiber-sessions/app/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var pcInteractive templates.PageContext = templates.PageContext{
|
||||
Title: "Welcome to the demo - Interactive",
|
||||
ActivePage: "interactive",
|
||||
}
|
||||
|
||||
func Interactive(f *fiber.Ctx) error {
|
||||
Render(f, templates.Interactive(pcInteractive, data.CatBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func InteractiveSwapContent(f *fiber.Ctx) error {
|
||||
contentIndexStr := f.Query("content")
|
||||
contentIndex, err := strconv.Atoi(contentIndexStr)
|
||||
if err != nil {
|
||||
contentIndex = 0
|
||||
}
|
||||
Render(f, templates.InteractiveSwapContent(pcInteractive, contentIndex))
|
||||
return nil
|
||||
}
|
||||
|
||||
func FilterCatBreeds(f *fiber.Ctx) error {
|
||||
breedQuery := strings.ToLower(f.Query("breed"))
|
||||
countryQuery := f.Query("country")
|
||||
|
||||
var catBreeds []types.CatBreed = []types.CatBreed{}
|
||||
|
||||
for _, cb := range data.CatBreeds {
|
||||
appendToList :=
|
||||
(len(breedQuery) > 0 &&
|
||||
strings.Contains(strings.ToLower(cb.Breed), breedQuery) ||
|
||||
len(breedQuery) == 0) &&
|
||||
((len(countryQuery) > 0 &&
|
||||
cb.Country == countryQuery) ||
|
||||
len(countryQuery) == 0)
|
||||
|
||||
if appendToList {
|
||||
catBreeds = append(catBreeds, cb)
|
||||
}
|
||||
}
|
||||
|
||||
Render(f, templates.RenderCatBreedsTable(catBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateForm(f *fiber.Ctx) error {
|
||||
content := templates.ValidateFormContent{
|
||||
Validated: true,
|
||||
NumValue: f.FormValue("number-value"),
|
||||
StrValue: f.FormValue("string-value"),
|
||||
}
|
||||
|
||||
numValue, err := strconv.Atoi(content.NumValue)
|
||||
if err != nil {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "This is not valid number"
|
||||
}
|
||||
if !content.HasNumValueError {
|
||||
if numValue < 0 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is less than 0"
|
||||
}
|
||||
if numValue > 100 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is greater than 100"
|
||||
}
|
||||
}
|
||||
|
||||
if len(content.StrValue) < 5 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is less than 5"
|
||||
}
|
||||
if len(content.StrValue) > 10 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is more than 10"
|
||||
}
|
||||
|
||||
Render(f, templates.RenderInteractiveForm(content))
|
||||
return nil
|
||||
}
|
||||
16
app/handlers/render.go
Normal file
16
app/handlers/render.go
Normal file
@ -0,0 +1,16 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/a-h/templ"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
)
|
||||
|
||||
// example taken from https://github.com/a-h/templ/tree/main/examples/integration-gofiber
|
||||
func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
|
||||
componentHandler := templ.Handler(component)
|
||||
for _, o := range options {
|
||||
o(componentHandler)
|
||||
}
|
||||
return adaptor.HTTPHandler(componentHandler)(c)
|
||||
}
|
||||
Reference in New Issue
Block a user