v2 #43
|
@ -2,7 +2,15 @@
|
|||
|
||||
{{define "main"}}
|
||||
|
||||
<form method="post" enctyep="multipart/form-data">
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="me">Me Links</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<h2>Site Settings</h2>
|
||||
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<label for="Title">Title</label>
|
||||
<input type="text" name="Title" id="Title" value="{{.Title}}"/>
|
||||
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
{{define "title"}}Editor{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
|
||||
<h2>Create a List</h2>
|
||||
<form action="/site-config/lists/create" method="post" enctype="multipart/form-data">
|
||||
<label for="Id">Id</label>
|
||||
<input type="text" name="Id" id="Id"/>
|
||||
|
||||
<label for="Title">Title</label>
|
||||
<input type="text" name="Title" id="Title"/>
|
||||
|
||||
<label for="Include">Include</label>
|
||||
{{ range .Types}}
|
||||
<input type="checkbox" name="Include" value="{{.}}" id="{{.}}"/>
|
||||
<label for="{{.}}">{{.}}</label>
|
||||
{{end}}
|
||||
|
||||
<br>
|
||||
<br>
|
||||
<label for="ListType">ListType</label>
|
||||
<input type="text" name="ListType" id="ListType"/>
|
||||
|
||||
<input type="submit" value="Create" />
|
||||
</form>
|
||||
|
||||
<h2>Me Links</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Title</th>
|
||||
<th>Include</th>
|
||||
<th>ListType</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $i, $l := .Lists}}
|
||||
<tr>
|
||||
<td>{{$l.Id}}</td>
|
||||
<td>{{$l.Title}}</td>
|
||||
<td>{{$l.Include}}</td>
|
||||
<td>{{$l.ListType}}</td>
|
||||
<td>
|
||||
<form action="/site-config/lists/delete" method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="idx" value="{{$i}}"/>
|
||||
<input type="submit" value="Delete" />
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
{{end}}
|
|
@ -0,0 +1,43 @@
|
|||
{{define "title"}}Editor{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
|
||||
<h2>Create a Me Link</h2>
|
||||
<form action="/site-config/me/create" method="post" enctype="multipart/form-data">
|
||||
<label for="Name">Name</label>
|
||||
<input type="text" name="Name" id="Name"/>
|
||||
|
||||
<label for="Url">URL</label>
|
||||
<input type="text" name="Url" id="Url"/>
|
||||
|
||||
<input type="submit" value="Create" />
|
||||
</form>
|
||||
|
||||
<h2>Me Links</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>URL</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $i, $a := .}}
|
||||
<tr>
|
||||
<td>{{.Name}}</td>
|
||||
<td>{{.Url}}</td>
|
||||
<td>
|
||||
<form action="/site-config/me/delete" method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="idx" value="{{$i}}"/>
|
||||
<input type="submit" value="Delete" />
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
{{end}}
|
16
web/app.go
16
web/app.go
|
@ -55,8 +55,20 @@ func NewWebApp(
|
|||
// SiteConfig
|
||||
siteConfig := app.Group("/site-config")
|
||||
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
|
||||
siteConfig.Get("/", NewSiteConfigHandler(siteConfigRepo).HandleGet)
|
||||
siteConfig.Post("/", NewSiteConfigHandler(siteConfigRepo).HandlePost)
|
||||
|
||||
siteConfigHandler := NewSiteConfigHandler(siteConfigRepo)
|
||||
siteConfig.Get("/", siteConfigHandler.HandleGet)
|
||||
siteConfig.Post("/", siteConfigHandler.HandlePost)
|
||||
|
||||
siteConfigMeHandler := NewSiteConfigMeHandler(siteConfigRepo)
|
||||
siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
|
||||
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
|
||||
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
|
||||
|
||||
siteConfigListHandler := NewSiteConfigListHandler(siteConfigRepo, typeRegistry)
|
||||
siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
|
||||
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
|
||||
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
|
||||
|
||||
// app.Static("/static/*filepath", http.Dir(repo.StaticDir()))
|
||||
app.Use("/static", filesystem.New(filesystem.Config{
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"owl-blogs/app"
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/domain/model"
|
||||
"owl-blogs/render"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type SiteConfigListHandler struct {
|
||||
siteConfigRepo repository.SiteConfigRepository
|
||||
typeRegistry *app.EntryTypeRegistry
|
||||
}
|
||||
|
||||
type siteConfigListTemplateData struct {
|
||||
Lists []model.EntryList
|
||||
Types []string
|
||||
}
|
||||
|
||||
func NewSiteConfigListHandler(
|
||||
siteConfigRepo repository.SiteConfigRepository,
|
||||
typeRegistry *app.EntryTypeRegistry,
|
||||
) *SiteConfigListHandler {
|
||||
return &SiteConfigListHandler{
|
||||
siteConfigRepo: siteConfigRepo,
|
||||
typeRegistry: typeRegistry,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
types := make([]string, 0)
|
||||
for _, t := range h.typeRegistry.Types() {
|
||||
typeName, err := h.typeRegistry.TypeName(t)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
types = append(types, typeName)
|
||||
}
|
||||
|
||||
return render.RenderTemplateWithBase(
|
||||
c, getConfig(h.siteConfigRepo), "views/site_config_list", siteConfigListTemplateData{
|
||||
Lists: config.Lists,
|
||||
Types: types,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.Lists = append(config.Lists, model.EntryList{
|
||||
Id: c.FormValue("Id"),
|
||||
Title: c.FormValue("Title"),
|
||||
Include: form.Value["Include"],
|
||||
ListType: c.FormValue("ListType"),
|
||||
})
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/lists")
|
||||
}
|
||||
|
||||
func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(c.FormValue("idx"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.Lists = append(config.Lists[:id], config.Lists[id+1:]...)
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/lists")
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/domain/model"
|
||||
"owl-blogs/render"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type SiteConfigMeHandler struct {
|
||||
siteConfigRepo repository.SiteConfigRepository
|
||||
}
|
||||
|
||||
func NewSiteConfigMeHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigMeHandler {
|
||||
return &SiteConfigMeHandler{
|
||||
siteConfigRepo: siteConfigRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SiteConfigMeHandler) 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_me", config.Me)
|
||||
}
|
||||
|
||||
func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config.Me = append(config.Me, model.MeLinks{
|
||||
Name: c.FormValue("Name"),
|
||||
Url: c.FormValue("Url"),
|
||||
})
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/me")
|
||||
}
|
||||
|
||||
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
||||
|
||||
config, err := h.siteConfigRepo.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
idx, err := strconv.Atoi(c.FormValue("idx"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.Me = append(config.Me[:idx], config.Me[idx+1:]...)
|
||||
|
||||
err = h.siteConfigRepo.Update(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Redirect("/site-config/me")
|
||||
}
|
Loading…
Reference in New Issue