v2 #43
|
@ -0,0 +1,95 @@
|
|||
{{define "title"}}Editor{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
|
||||
<h2>Create a List</h2>
|
||||
<form action="/site-config/menus/create" method="post" enctype="multipart/form-data">
|
||||
|
||||
<select name="menu" id="menu">
|
||||
<option value="header">Header</option>
|
||||
<option value="footer">Footer</option>
|
||||
</select>
|
||||
|
||||
<label for="Title">Title</label>
|
||||
<input type="text" name="Title" id="Title"/>
|
||||
|
||||
<label for="Url">Url</label>
|
||||
<input type="text" name="Url" id="Url"/>
|
||||
|
||||
<label for="List">List</label>
|
||||
<input type="text" name="List" id="List"/>
|
||||
|
||||
<label for="Post">Post</label>
|
||||
<input type="text" name="Post" id="Post"/>
|
||||
|
||||
<label for="ListType">ListType</label>
|
||||
<input type="text" name="ListType" id="ListType"/>
|
||||
|
||||
<input type="submit" value="Create" />
|
||||
</form>
|
||||
|
||||
<h2>Header Menu</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>List</th>
|
||||
<th>Url</th>
|
||||
<th>Post</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $i, $l := .HeaderMenu}}
|
||||
<tr>
|
||||
<td>{{$l.Title}}</td>
|
||||
<td>{{$l.List}}</td>
|
||||
<td>{{$l.Url}}</td>
|
||||
<td>{{$l.Post}}</td>
|
||||
<td>
|
||||
<form action="/site-config/menus/delete" method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="idx" value="{{$i}}"/>
|
||||
<input type="hidden" name="menu" value="header"/>
|
||||
<input type="submit" value="Delete" />
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
<h2>Footer Menu</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Title</th>
|
||||
<th>List</th>
|
||||
<th>Url</th>
|
||||
<th>Post</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $i, $l := .FooterMenu}}
|
||||
<tr>
|
||||
<td>{{$l.Title}}</td>
|
||||
<td>{{$l.List}}</td>
|
||||
<td>{{$l.Url}}</td>
|
||||
<td>{{$l.Post}}</td>
|
||||
<td>
|
||||
<form action="/site-config/menus/delete" method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="idx" value="{{$i}}"/>
|
||||
<input type="hidden" name="menu" value="footer"/>
|
||||
<input type="submit" value="Delete" />
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
{{end}}
|
|
@ -70,6 +70,11 @@ func NewWebApp(
|
|||
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
|
||||
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
|
||||
|
||||
siteConfigMenusHandler := NewSiteConfigMenusHandler(siteConfigRepo)
|
||||
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
|
||||
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
|
||||
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
|
||||
|
||||
// app.Static("/static/*filepath", http.Dir(repo.StaticDir()))
|
||||
app.Use("/static", filesystem.New(filesystem.Config{
|
||||
Root: http.FS(embedDirStatic),
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/domain/model"
|
||||
"owl-blogs/render"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type SiteConfigMenusHandler struct {
|
||||
siteConfigRepo repository.SiteConfigRepository
|
||||
}
|
||||
|
||||
type siteConfigMenusTemplateData struct {
|
||||
HeaderMenu []model.MenuItem
|
||||
FooterMenu []model.MenuItem
|
||||
}
|
||||
|
||||
func NewSiteConfigMenusHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigMenusHandler {
|
||||
return &SiteConfigMenusHandler{
|
||||
siteConfigRepo: siteConfigRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.RenderTemplateWithBase(
|
||||
c, getConfig(h.siteConfigRepo), "views/site_config_menus", siteConfigMenusTemplateData{
|
||||
HeaderMenu: config.HeaderMenu,
|
||||
FooterMenu: config.FooterMenu,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
menuItem := model.MenuItem{
|
||||
Title: c.FormValue("Title"),
|
||||
List: c.FormValue("List"),
|
||||
Url: c.FormValue("Url"),
|
||||
Post: c.FormValue("Post"),
|
||||
}
|
||||
|
||||
if c.FormValue("menu") == "header" {
|
||||
config.HeaderMenu = append(config.HeaderMenu, menuItem)
|
||||
} else if c.FormValue("menu") == "footer" {
|
||||
config.FooterMenu = append(config.FooterMenu, menuItem)
|
||||
}
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/menus")
|
||||
}
|
||||
|
||||
func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
menu := c.FormValue("menu")
|
||||
idx, err := strconv.Atoi(c.FormValue("idx"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if menu == "header" {
|
||||
config.HeaderMenu = append(config.HeaderMenu[:idx], config.HeaderMenu[idx+1:]...)
|
||||
} else if menu == "footer" {
|
||||
config.FooterMenu = append(config.FooterMenu[:idx], config.FooterMenu[idx+1:]...)
|
||||
}
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/menus")
|
||||
}
|
Loading…
Reference in New Issue