listing entries

This commit is contained in:
Niko Abeler 2023-07-09 19:51:49 +02:00
parent 408bb88cb8
commit f513205cc3
17 changed files with 138 additions and 84 deletions

View File

@ -1,5 +1,10 @@
package model
import (
"fmt"
"owl-blogs/render"
)
type Article struct {
EntryBase
meta ArticleMetaData
@ -10,8 +15,16 @@ type ArticleMetaData struct {
Content string `owl:"inputType=text widget=textarea"`
}
func (e *Article) Title() string {
return e.meta.Title
}
func (e *Article) Content() EntryContent {
return EntryContent(e.meta.Content)
str, err := render.RenderTemplateToString("entry/Article", e)
if err != nil {
fmt.Println(err)
}
return EntryContent(str)
}
func (e *Article) MetaData() interface{} {

View File

@ -9,7 +9,9 @@ type Entry interface {
Content() EntryContent
PublishedAt() *time.Time
MetaData() interface{}
// Create(id string, publishedAt *time.Time, metaData EntryMetaData) error
// Optional: can return empty string
Title() string
SetID(id string)
SetPublishedAt(publishedAt *time.Time)

View File

@ -1,5 +1,10 @@
package model
import (
"fmt"
"owl-blogs/render"
)
type Image struct {
EntryBase
meta ImageMetaData
@ -10,8 +15,16 @@ type ImageMetaData struct {
Content string `owl:"inputType=text widget=textarea"`
}
func (e *Image) Title() string {
return ""
}
func (e *Image) Content() EntryContent {
return EntryContent(e.meta.Content)
str, err := render.RenderTemplateToString("entry/Image", e)
if err != nil {
fmt.Println(err)
}
return EntryContent(str)
}
func (e *Image) MetaData() interface{} {

52
render/templates.go Normal file
View File

@ -0,0 +1,52 @@
package render
import (
"bytes"
"embed"
"io"
"text/template"
)
//go:embed templates
var templates embed.FS
func CreateTemplateWithBase(templateName string) (*template.Template, error) {
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/"+templateName+".tmpl",
)
}
func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error {
t, err := CreateTemplateWithBase(templateName)
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", data)
return err
}
func RenderTemplateToString(templateName string, data interface{}) (string, error) {
t, err := template.ParseFS(
templates,
"templates/"+templateName+".tmpl",
)
if err != nil {
return "", err
}
var output bytes.Buffer
err = t.Execute(&output, data)
return output.String(), err
}

View File

@ -0,0 +1 @@
{{.MetaData.Content}}

View File

@ -0,0 +1,8 @@
<h1>
{{.MetaData.Title}}
</h1>
<img src="/media/{{.MetaData.ImageId}}">
{{.MetaData.Content}}

View File

@ -1,17 +1,18 @@
{{define "title"}}{{.MetaData.Title}}{{end}}
{{define "title"}}{{.Title}}{{end}}
{{define "main"}}
<h1>{{.MetaData.Title}}</h1>
<p>
{{.Content}}
</p>
{{if .Title}}
<h1>{{.Title}}</h1>
{{end}}
<p>
Published: {{.PublishedAt}}
</p>
{{.Content}}
{{end}}

View File

@ -0,0 +1,22 @@
{{define "title"}}Index{{end}}
{{define "main"}}
{{ range . }}
<div>
<h2>
<a href="/posts/{{ .ID }}">
{{if .Title}}
{{ .Title }}
{{else}}
#
{{end}}
</a>
</h2>
<p>{{ .PublishedAt }}</p>
{{ .Content }}
</div>
<hr>
{{ end }}
{{end}}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
@ -32,5 +33,5 @@ func (h *EditorListHandler) Handle(c *fiber.Ctx) error {
typeNames = append(typeNames, name)
}
return RenderTemplate(c, "views/editor_list", &EditorListContext{Types: typeNames})
return render.RenderTemplateWithBase(c, "views/editor_list", &EditorListContext{Types: typeNames})
}

View File

@ -2,8 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/domain/model"
"text/template"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
@ -17,18 +16,6 @@ func NewEntryHandler(entryService *app.EntryService, registry *app.EntryTypeRegi
return &EntryHandler{entrySvc: entryService, registry: registry}
}
func (h *EntryHandler) getTemplate(entry model.Entry) (*template.Template, error) {
name, err := h.registry.TypeName(entry)
if err != nil {
return nil, err
}
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/views/entry/"+name+".tmpl",
)
}
func (h *EntryHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
@ -38,10 +25,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
return err
}
template, err := h.getTemplate(entry)
if err != nil {
return err
}
return template.ExecuteTemplate(c, "base", entry)
return render.RenderTemplateWithBase(c, "views/entry", entry)
}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
@ -15,5 +16,13 @@ func NewIndexHandler(entryService *app.EntryService) *IndexHandler {
}
func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
entries, err := h.entrySvc.FindAll()
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, "views/index", entries)
}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/render"
"time"
"github.com/gofiber/fiber/v2"
@ -17,7 +18,7 @@ func NewLoginHandler(authorService *app.AuthorService) *LoginHandler {
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return RenderTemplate(c, "views/login", nil)
return render.RenderTemplateWithBase(c, "views/login", nil)
}
func (h *LoginHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -1,34 +0,0 @@
package web
import (
"embed"
"io"
"text/template"
)
//go:embed templates
var templates embed.FS
func CreateTemplate(templateName string) (*template.Template, error) {
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/"+templateName+".tmpl",
)
}
func RenderTemplate(w io.Writer, templateName string, data interface{}) error {
t, err := CreateTemplate(templateName)
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", data)
return err
}

View File

@ -1,17 +0,0 @@
{{define "title"}}Image Entry{{end}}
{{define "main"}}
<img src="/media/{{.MetaData.ImageId}}">
<p>
{{.Content}}
</p>
<p>
Published: {{.PublishedAt}}
</p>
{{end}}