cup/controllers/sample.go
2024-10-29 12:44:51 -05:00

149 lines
3 KiB
Go

// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.
package controllers
import (
"fmt"
"git.smarteching.com/goffee/core"
"git.smarteching.com/goffee/core/template/components"
"git.smarteching.com/goffee/cup/utils"
)
// Show basic template
func Sample(c *core.Context) *core.Response {
// first, include all compoments
type templateData struct {
PageCard components.PageCard
}
// now fill data of the components
tmplData := templateData{
PageCard: components.PageCard{
CardTitle: "Framework Goffee",
CardBody: "Powered by Golang",
},
}
return c.Response.Template("basic.html", tmplData)
}
// Show basic app login
func AppLogin(c *core.Context) *core.Response {
// first, include all compoments
// first, include all compoments
type templateData struct {
PageCard components.PageCard
}
// now fill data of the components
tmplData := templateData{
PageCard: components.PageCard{
CardTitle: "Card title",
CardBody: "Loerm ipsum at deim",
},
}
return c.Response.Template("login.html", tmplData)
}
// Show basic app login
func AppSession(c *core.Context) *core.Response {
var session = new(utils.SessionUser)
// true if session is active
hassession := session.Init(c)
//session.Set("numberdos", 66)
type templateData struct {
PageCard components.PageCard
}
// now fill data of the components
tmplData := templateData{}
if hassession {
sesiondata := ""
cardtitle := fmt.Sprintf("Session user id: %v", session.GetUserID())
numberdos, ok := session.Get("numberdos")
if numberdos != nil {
numberdos = numberdos.(float64) + 10
} else {
numberdos = 10
}
session.Set("numberdos", numberdos)
if ok {
sesiondata = fmt.Sprintf("OK, Session numberdos has %v", numberdos)
} else {
sesiondata = fmt.Sprintf("No ok, session numberdos has %v", numberdos)
}
// delete single
//session.Delete("numberdos")
// delete all data
//session.Flush()
tmplData = templateData{
PageCard: components.PageCard{
CardTitle: cardtitle,
CardBody: sesiondata,
},
}
return c.Response.Template("appsession.html", tmplData)
} else {
return c.Response.Template("login.html", tmplData)
}
}
// Show basic app sample
func AppSample(c *core.Context) *core.Response {
// first, include all compoments
type templateData struct {
PageCard components.PageCard
ContentDropdown components.ContentDropdown
}
// now fill data of the components
tmplData := templateData{
PageCard: components.PageCard{
CardTitle: "Protected page",
CardBody: "If you can see this page, your are loggedin",
},
ContentDropdown: components.ContentDropdown{
Label: "dropdown",
Items: []components.ContentDropdownItem{
{
Text: "Signout",
Link: "#",
ID: "signout",
},
{
Text: "item disabled",
Link: "#",
IsDisabled: true,
},
},
},
}
//fmt.Printf("Outside cookie user is: %s", user.Email)
return c.Response.Template("app.html", tmplData)
}