From 1fdcdff41d8c117ef9758f4813a84dd5e0798246 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Wed, 14 Feb 2024 21:05:50 +0100 Subject: [PATCH] all entry types with custom forms --- entry_types/note.go | 16 +++++++++++++--- entry_types/page.go | 17 ++++++++++++++--- entry_types/recipe.go | 28 +++++++++++++++++++++++++--- entry_types/reply.go | 18 +++++++++++++++--- render/templates/forms/Article.tmpl | 4 +--- render/templates/forms/Bookmark.tmpl | 4 +--- render/templates/forms/Image.tmpl | 4 +--- render/templates/forms/Note.tmpl | 2 ++ render/templates/forms/Page.tmpl | 5 +++++ render/templates/forms/Recipe.tmpl | 15 +++++++++++++++ render/templates/forms/Reply.tmpl | 8 ++++++++ test/mock_entry.go | 13 ++++++++++--- web/forms/form.go | 13 ------------- 13 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 render/templates/forms/Note.tmpl create mode 100644 render/templates/forms/Page.tmpl create mode 100644 render/templates/forms/Recipe.tmpl create mode 100644 render/templates/forms/Reply.tmpl diff --git a/entry_types/note.go b/entry_types/note.go index 7dd4157..7e1bcf6 100644 --- a/entry_types/note.go +++ b/entry_types/note.go @@ -4,7 +4,6 @@ import ( "fmt" "owl-blogs/domain/model" "owl-blogs/render" - "owl-blogs/web/forms" ) type Note struct { @@ -13,11 +12,22 @@ type Note struct { } type NoteMetaData struct { - forms.DefaultForm - Content string `owl:"inputType=text widget=textarea"` } +// Form implements model.EntryMetaData. +func (meta *NoteMetaData) Form(binSvc model.BinaryStorageInterface) string { + f, _ := render.RenderTemplateToString("forms/Note", meta) + return f +} + +// ParseFormData implements model.EntryMetaData. +func (*NoteMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { + return &NoteMetaData{ + Content: data.FormValue("content"), + }, nil +} + func (e *Note) Title() string { return "" } diff --git a/entry_types/page.go b/entry_types/page.go index 6952de4..9e088bf 100644 --- a/entry_types/page.go +++ b/entry_types/page.go @@ -4,7 +4,6 @@ import ( "fmt" "owl-blogs/domain/model" "owl-blogs/render" - "owl-blogs/web/forms" ) type Page struct { @@ -13,12 +12,24 @@ type Page struct { } type PageMetaData struct { - forms.DefaultForm - Title string `owl:"inputType=text"` Content string `owl:"inputType=text widget=textarea"` } +// Form implements model.EntryMetaData. +func (meta *PageMetaData) Form(binSvc model.BinaryStorageInterface) string { + f, _ := render.RenderTemplateToString("forms/Page", meta) + return f +} + +// ParseFormData implements model.EntryMetaData. +func (*PageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { + return &PageMetaData{ + Title: data.FormValue("title"), + Content: data.FormValue("content"), + }, nil +} + func (e *Page) Title() string { return e.meta.Title } diff --git a/entry_types/recipe.go b/entry_types/recipe.go index 7442592..daa8a57 100644 --- a/entry_types/recipe.go +++ b/entry_types/recipe.go @@ -4,7 +4,7 @@ import ( "fmt" "owl-blogs/domain/model" "owl-blogs/render" - "owl-blogs/web/forms" + "strings" ) type Recipe struct { @@ -13,8 +13,6 @@ type Recipe struct { } type RecipeMetaData struct { - forms.DefaultForm - Title string `owl:"inputType=text"` Yield string `owl:"inputType=text"` Duration string `owl:"inputType=text"` @@ -22,6 +20,30 @@ type RecipeMetaData struct { Content string `owl:"inputType=text widget=textarea"` } +// Form implements model.EntryMetaData. +func (meta *RecipeMetaData) Form(binSvc model.BinaryStorageInterface) string { + f, _ := render.RenderTemplateToString("forms/Recipe", meta) + return f +} + +// ParseFormData implements model.EntryMetaData. +func (*RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { + ings := strings.Split(data.FormValue("ingredients"), "\n") + clean := make([]string, 0) + for _, ing := range ings { + if strings.TrimSpace(ing) != "" { + clean = append(clean, strings.TrimSpace(ing)) + } + } + return &RecipeMetaData{ + Title: data.FormValue("title"), + Yield: data.FormValue("yield"), + Duration: data.FormValue("duration"), + Ingredients: clean, + Content: data.FormValue("content"), + }, nil +} + func (e *Recipe) Title() string { return e.meta.Title } diff --git a/entry_types/reply.go b/entry_types/reply.go index ef23467..e82d0da 100644 --- a/entry_types/reply.go +++ b/entry_types/reply.go @@ -4,7 +4,6 @@ import ( "fmt" "owl-blogs/domain/model" "owl-blogs/render" - "owl-blogs/web/forms" ) type Reply struct { @@ -13,13 +12,26 @@ type Reply struct { } type ReplyMetaData 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 *ReplyMetaData) Form(binSvc model.BinaryStorageInterface) string { + f, _ := render.RenderTemplateToString("forms/Reply", meta) + return f +} + +// ParseFormData implements model.EntryMetaData. +func (*ReplyMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { + return &ReplyMetaData{ + Title: data.FormValue("title"), + Url: data.FormValue("url"), + Content: data.FormValue("content"), + }, nil +} + func (e *Reply) Title() string { return "Re: " + e.meta.Title } diff --git a/render/templates/forms/Article.tmpl b/render/templates/forms/Article.tmpl index 65aaedd..3b5e9ce 100644 --- a/render/templates/forms/Article.tmpl +++ b/render/templates/forms/Article.tmpl @@ -2,6 +2,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/render/templates/forms/Bookmark.tmpl b/render/templates/forms/Bookmark.tmpl index e482759..6e69609 100644 --- a/render/templates/forms/Bookmark.tmpl +++ b/render/templates/forms/Bookmark.tmpl @@ -5,6 +5,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/render/templates/forms/Image.tmpl b/render/templates/forms/Image.tmpl index 1c78f6c..9286c42 100644 --- a/render/templates/forms/Image.tmpl +++ b/render/templates/forms/Image.tmpl @@ -5,6 +5,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/render/templates/forms/Note.tmpl b/render/templates/forms/Note.tmpl new file mode 100644 index 0000000..63cb09d --- /dev/null +++ b/render/templates/forms/Note.tmpl @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/render/templates/forms/Page.tmpl b/render/templates/forms/Page.tmpl new file mode 100644 index 0000000..3b5e9ce --- /dev/null +++ b/render/templates/forms/Page.tmpl @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/render/templates/forms/Recipe.tmpl b/render/templates/forms/Recipe.tmpl new file mode 100644 index 0000000..bcf27bc --- /dev/null +++ b/render/templates/forms/Recipe.tmpl @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/render/templates/forms/Reply.tmpl b/render/templates/forms/Reply.tmpl new file mode 100644 index 0000000..aaca489 --- /dev/null +++ b/render/templates/forms/Reply.tmpl @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/test/mock_entry.go b/test/mock_entry.go index e9f9301..a21754f 100644 --- a/test/mock_entry.go +++ b/test/mock_entry.go @@ -2,19 +2,26 @@ package test import ( "owl-blogs/domain/model" - "owl-blogs/web/forms" "time" ) type MockEntryMetaData struct { - forms.DefaultForm - Str string Number int Date time.Time Title string } +// Form implements model.EntryMetaData. +func (*MockEntryMetaData) Form(binSvc model.BinaryStorageInterface) string { + panic("unimplemented") +} + +// ParseFormData implements model.EntryMetaData. +func (*MockEntryMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { + panic("unimplemented") +} + type MockEntry struct { model.EntryBase metaData *MockEntryMetaData diff --git a/web/forms/form.go b/web/forms/form.go index 3737bfd..2d2f5f3 100644 --- a/web/forms/form.go +++ b/web/forms/form.go @@ -7,19 +7,6 @@ import ( "strings" ) -type DefaultForm struct{} - -func (meta *DefaultForm) Form(binSvc model.BinaryStorageInterface) string { - form := NewForm(meta, nil) - htmlForm, _ := form.HtmlForm() - return htmlForm -} - -func (meta *DefaultForm) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { - form := NewForm(meta, binSvc) - return form.Parse(data) -} - type Form[T interface{}] struct { data T binSvc model.BinaryStorageInterface