Compare commits

...

2 Commits

Author SHA1 Message Date
Niko Abeler eee2131a76 more custom forms 2024-02-14 20:43:43 +01:00
Niko Abeler fcc0132758 first custom form 2024-02-14 19:53:49 +01:00
9 changed files with 100 additions and 28 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
"owl-blogs/domain/model"
"owl-blogs/render"
"owl-blogs/web/forms"
)
type Article struct {
@ -13,11 +12,24 @@ type Article struct {
}
type ArticleMetaData struct {
forms.DefaultForm
Title string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"`
}
// Form implements model.EntryMetaData.
func (meta *ArticleMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Article", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*ArticleMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
return &ArticleMetaData{
Title: data.FormValue("title"),
Content: data.FormValue("content"),
}, nil
}
func (e *Article) Title() string {
return e.meta.Title
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"owl-blogs/domain/model"
"owl-blogs/render"
"owl-blogs/web/forms"
)
type Bookmark struct {
@ -13,13 +12,26 @@ type Bookmark struct {
}
type BookmarkMetaData struct {
forms.DefaultForm
Title string `owl:"inputType=text"`
Url string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"`
}
// Form implements model.EntryMetaData.
func (meta *BookmarkMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Bookmark", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*BookmarkMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
return &BookmarkMetaData{
Title: data.FormValue("title"),
Url: data.FormValue("url"),
Content: data.FormValue("content"),
}, nil
}
func (e *Bookmark) Title() string {
return e.meta.Title
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"owl-blogs/domain/model"
"owl-blogs/render"
"owl-blogs/web/forms"
)
type Image struct {
@ -13,13 +12,49 @@ type Image struct {
}
type ImageMetaData struct {
forms.DefaultForm
ImageId string `owl:"inputType=file"`
Title string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"`
}
// Form implements model.EntryMetaData.
func (meta *ImageMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Image", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (meta *ImageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
file, err := data.FormFile("image")
var imgId = meta.ImageId
if err != nil && imgId == "" {
return nil, err
} else if err == nil {
fileData, err := file.Open()
if err != nil {
return nil, err
}
defer fileData.Close()
fileBytes := make([]byte, file.Size)
_, err = fileData.Read(fileBytes)
if err != nil {
return nil, err
}
bin, err := binSvc.Create(file.Filename, fileBytes)
if err != nil {
return nil, err
}
imgId = bin.Id
}
return &ImageMetaData{
ImageId: imgId,
Title: data.FormValue("title"),
Content: data.FormValue("content"),
}, nil
}
func (e *Image) Title() string {
return e.meta.Title
}

View File

@ -0,0 +1,7 @@
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="content">Content</label>
<textarea name="content" rows="16">
{{.Content}}
</textarea>

View File

@ -0,0 +1,10 @@
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="url">URL</label>
<input type="text" name="url" value="{{.Url}}" placeholder="https://..." />
<label for="content">Content</label>
<textarea name="content" rows="16">
{{.Content}}
</textarea>

View File

@ -0,0 +1,10 @@
<label for="image">Image</label>
<input type="file" name="image" />
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="content">Content</label>
<textarea name="content" rows="16">
{{.Content}}
</textarea>

View File

@ -1 +0,0 @@
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1

View File

@ -5,7 +5,6 @@ import (
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/render"
"owl-blogs/web/forms"
"time"
"github.com/gofiber/fiber/v2"
@ -48,12 +47,7 @@ func (h *EditorHandler) HandleGetNew(c *fiber.Ctx) error {
if err != nil {
return err
}
form := forms.NewForm(entryType.MetaData(), h.binSvc)
htmlForm, err := form.HtmlForm()
if err != nil {
return err
}
htmlForm := entryType.MetaData().Form(h.binSvc)
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm)
}
@ -65,9 +59,7 @@ func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error {
return err
}
form := forms.NewForm(entry.MetaData(), h.binSvc)
// get form data
entryMeta, err := form.Parse(c)
entryMeta, err := entry.MetaData().ParseFormData(c, h.binSvc)
if err != nil {
return err
}
@ -100,11 +92,7 @@ func (h *EditorHandler) HandleGetEdit(c *fiber.Ctx) error {
return err
}
form := forms.NewForm(entry.MetaData(), h.binSvc)
htmlForm, err := form.HtmlForm()
if err != nil {
return err
}
htmlForm := entry.MetaData().Form(h.binSvc)
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm)
}
@ -117,9 +105,8 @@ func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error {
return err
}
form := forms.NewForm(entry.MetaData(), h.binSvc)
// get form data
meta, err := form.Parse(c)
meta, err := entry.MetaData().ParseFormData(c, h.binSvc)
if err != nil {
return err
}

View File

@ -103,6 +103,6 @@ func TestFormParse(t *testing.T) {
form := forms.NewForm(&MockData{}, binService)
data, err := form.Parse(NewMockFormData())
require.NoError(t, err)
require.NotZero(t, data.(*MockData).Image)
require.Equal(t, "Content", data.(*MockData).Content)
require.NotZero(t, data.Image)
require.Equal(t, "Content", data.Content)
}