adattato html, test htmx con componente svelte
This commit is contained in:
@@ -3,17 +3,13 @@ package controllers
|
||||
import (
|
||||
"html/template"
|
||||
|
||||
"trustcontact/internal/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type AdminController struct {
|
||||
usersService *services.UsersService
|
||||
}
|
||||
type AdminController struct{}
|
||||
|
||||
func NewAdminController(usersService *services.UsersService) *AdminController {
|
||||
return &AdminController{usersService: usersService}
|
||||
func NewAdminController() *AdminController {
|
||||
return &AdminController{}
|
||||
}
|
||||
|
||||
func (ac *AdminController) Dashboard(c *fiber.Ctx) error {
|
||||
@@ -36,36 +32,3 @@ func (ac *AdminController) Dashboard(c *fiber.Ctx) error {
|
||||
|
||||
return executeLayout(c, tmpl, viewData)
|
||||
}
|
||||
|
||||
func (ac *AdminController) Users(c *fiber.Ctx) error {
|
||||
pageData, err := ac.usersService.List(services.UsersQuery{
|
||||
Q: c.Query("q"),
|
||||
Sort: c.Query("sort", "id"),
|
||||
Dir: c.Query("dir", "asc"),
|
||||
Page: parseIntOrDefault(c.Query("page"), 1),
|
||||
PageSize: parseIntOrDefault(c.Query("pageSize"), 20),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
viewData := map[string]any{
|
||||
"Title": "Admin Users",
|
||||
"NavSection": "admin",
|
||||
"PageData": pageData,
|
||||
}
|
||||
for k, v := range localsTemplateData(c) {
|
||||
viewData[k] = v
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(
|
||||
"web/templates/layout.html",
|
||||
"web/templates/public/_flash.html",
|
||||
"web/templates/admin/users.html",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return executeLayout(c, tmpl, viewData)
|
||||
}
|
||||
|
||||
@@ -19,12 +19,23 @@ func NewAuthController(authService *services.AuthService) *AuthController {
|
||||
}
|
||||
|
||||
func (ac *AuthController) ShowHome(c *fiber.Ctx) error {
|
||||
if _, ok := httpmw.CurrentUserFromContext(c); ok {
|
||||
return c.Redirect("/welcome")
|
||||
}
|
||||
|
||||
return renderPublic(c, "home.html", map[string]any{
|
||||
"Title": "Home",
|
||||
"NavSection": "public",
|
||||
})
|
||||
}
|
||||
|
||||
func (ac *AuthController) ShowWelcome(c *fiber.Ctx) error {
|
||||
return renderPrivate(c, "welcome.html", map[string]any{
|
||||
"Title": "Welcome",
|
||||
"NavSection": "private",
|
||||
})
|
||||
}
|
||||
|
||||
func (ac *AuthController) ShowSignup(c *fiber.Ctx) error {
|
||||
return renderPublic(c, "signup.html", map[string]any{
|
||||
"Title": "Sign up",
|
||||
@@ -90,7 +101,7 @@ func (ac *AuthController) Login(c *fiber.Ctx) error {
|
||||
if err := httpmw.SetFlashSuccess(c, "Login effettuato"); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Redirect("/private")
|
||||
return c.Redirect("/welcome")
|
||||
}
|
||||
|
||||
func (ac *AuthController) Logout(c *fiber.Ctx) error {
|
||||
|
||||
@@ -44,6 +44,42 @@ func renderPublic(c *fiber.Ctx, page string, data map[string]any) error {
|
||||
return c.Send(out.Bytes())
|
||||
}
|
||||
|
||||
func renderPrivate(c *fiber.Ctx, page string, data map[string]any) error {
|
||||
viewData := map[string]any{}
|
||||
for k, v := range localsTemplateData(c) {
|
||||
viewData[k] = v
|
||||
}
|
||||
for k, v := range data {
|
||||
viewData[k] = v
|
||||
}
|
||||
|
||||
if _, ok := viewData["Title"]; !ok {
|
||||
viewData["Title"] = "Trustcontact"
|
||||
}
|
||||
if _, ok := viewData["NavSection"]; !ok {
|
||||
viewData["NavSection"] = "private"
|
||||
}
|
||||
|
||||
files := []string{
|
||||
"web/templates/layout.html",
|
||||
"web/templates/public/_flash.html",
|
||||
filepath.Join("web/templates/private", page),
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := tmpl.ExecuteTemplate(&out, "layout.html", viewData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Type("html", "utf-8")
|
||||
return c.Send(out.Bytes())
|
||||
}
|
||||
|
||||
func localsTemplateData(c *fiber.Ctx) map[string]any {
|
||||
data, ok := c.Locals("template_data").(map[string]any)
|
||||
if !ok || data == nil {
|
||||
|
||||
@@ -28,8 +28,8 @@ func (uc *UsersController) Index(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
viewData := map[string]any{
|
||||
"Title": "Users",
|
||||
"NavSection": "private",
|
||||
"Title": "Admin Users",
|
||||
"NavSection": "admin",
|
||||
"PageData": pageData,
|
||||
}
|
||||
for k, v := range localsTemplateData(c) {
|
||||
@@ -39,8 +39,8 @@ func (uc *UsersController) Index(c *fiber.Ctx) error {
|
||||
tmpl, err := template.ParseFiles(
|
||||
"web/templates/layout.html",
|
||||
"web/templates/public/_flash.html",
|
||||
"web/templates/private/users/index.html",
|
||||
"web/templates/private/users/_table.html",
|
||||
"web/templates/admin/users/index.html",
|
||||
"web/templates/admin/users/_table.html",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -56,7 +56,7 @@ func (uc *UsersController) Table(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
viewData := map[string]any{"PageData": pageData}
|
||||
tmpl, err := template.ParseFiles("web/templates/private/users/_table.html")
|
||||
tmpl, err := template.ParseFiles("web/templates/admin/users/_table.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (uc *UsersController) Modal(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
viewData := map[string]any{"User": user}
|
||||
tmpl, err := template.ParseFiles("web/templates/private/users/_modal.html")
|
||||
tmpl, err := template.ParseFiles("web/templates/admin/users/_modal.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
)
|
||||
|
||||
func RegisterRoutes(app *fiber.App, store *session.Store, database *gorm.DB, cfg *config.Config) error {
|
||||
app.Static("/static", "web/static")
|
||||
|
||||
app.Use(httpmw.SessionStoreMiddleware(store))
|
||||
app.Use(httpmw.CurrentUserMiddleware(store, database))
|
||||
app.Use(httpmw.ConsumeFlash())
|
||||
@@ -28,7 +30,7 @@ func RegisterRoutes(app *fiber.App, store *session.Store, database *gorm.DB, cfg
|
||||
authController := controllers.NewAuthController(authService)
|
||||
usersService := services.NewUsersService(database)
|
||||
usersController := controllers.NewUsersController(usersService)
|
||||
adminController := controllers.NewAdminController(usersService)
|
||||
adminController := controllers.NewAdminController()
|
||||
|
||||
app.Get("/healthz", func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
@@ -46,18 +48,18 @@ func RegisterRoutes(app *fiber.App, store *session.Store, database *gorm.DB, cfg
|
||||
app.Post("/forgot-password", authController.ForgotPassword)
|
||||
app.Get("/reset-password", authController.ShowResetPassword)
|
||||
app.Post("/reset-password", authController.ResetPassword)
|
||||
app.Get("/users", httpmw.RequireAuth(), usersController.Index)
|
||||
app.Get("/users/table", httpmw.RequireAuth(), usersController.Table)
|
||||
app.Get("/users/:id/modal", httpmw.RequireAuth(), usersController.Modal)
|
||||
app.Get("/welcome", httpmw.RequireAuth(), authController.ShowWelcome)
|
||||
|
||||
private := app.Group("/private", httpmw.RequireAuth())
|
||||
private := app.Group("/private", httpmw.RequireAuth(), httpmw.RequireAdmin())
|
||||
private.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.Redirect("/users")
|
||||
return c.Redirect("/admin/users")
|
||||
})
|
||||
|
||||
admin := app.Group("/admin", httpmw.RequireAuth(), httpmw.RequireAdmin())
|
||||
admin.Get("/", adminController.Dashboard)
|
||||
admin.Get("/users", adminController.Users)
|
||||
admin.Get("/users", usersController.Index)
|
||||
admin.Get("/users/table", usersController.Table)
|
||||
admin.Get("/users/:id/modal", usersController.Modal)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user