unified interface Formable

This commit is contained in:
Niko Abeler 2024-02-21 20:24:18 +01:00
parent c36b9abbcf
commit 2f81bf8678
15 changed files with 60 additions and 64 deletions

View File

@ -3,8 +3,7 @@ package app
import "owl-blogs/domain/model" import "owl-blogs/domain/model"
type AppConfig interface { type AppConfig interface {
Form(binSvc model.BinaryStorageInterface) string model.Formable
ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (AppConfig, error)
} }
type ConfigRegister struct { type ConfigRegister struct {

View File

@ -23,8 +23,7 @@ type Entry interface {
} }
type EntryMetaData interface { type EntryMetaData interface {
Form(binSvc BinaryStorageInterface) string Formable
ParseFormData(data HttpFormData, binSvc BinaryStorageInterface) (EntryMetaData, error)
} }
type EntryBase struct { type EntryBase struct {

View File

@ -2,6 +2,11 @@ package model
import "mime/multipart" import "mime/multipart"
type Formable interface {
Form(binSvc BinaryStorageInterface) string
ParseFormData(data HttpFormData, binSvc BinaryStorageInterface) error
}
type HttpFormData interface { type HttpFormData interface {
// FormFile returns the first file by key from a MultipartForm. // FormFile returns the first file by key from a MultipartForm.
FormFile(key string) (*multipart.FileHeader, error) FormFile(key string) (*multipart.FileHeader, error)

View File

@ -23,11 +23,10 @@ func (meta *ArticleMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*ArticleMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *ArticleMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &ArticleMetaData{ meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Article) Title() string { func (e *Article) Title() string {

View File

@ -24,12 +24,11 @@ func (meta *BookmarkMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*BookmarkMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *BookmarkMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &BookmarkMetaData{ meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Url = data.FormValue("url")
Url: data.FormValue("url"), meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Bookmark) Title() string { func (e *Bookmark) Title() string {

View File

@ -24,35 +24,34 @@ func (meta *ImageMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (meta *ImageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *ImageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
file, err := data.FormFile("image") file, err := data.FormFile("image")
var imgId = meta.ImageId var imgId = meta.ImageId
if err != nil && imgId == "" { if err != nil && imgId == "" {
return nil, err return err
} else if err == nil { } else if err == nil {
fileData, err := file.Open() fileData, err := file.Open()
if err != nil { if err != nil {
return nil, err return err
} }
defer fileData.Close() defer fileData.Close()
fileBytes := make([]byte, file.Size) fileBytes := make([]byte, file.Size)
_, err = fileData.Read(fileBytes) _, err = fileData.Read(fileBytes)
if err != nil { if err != nil {
return nil, err return err
} }
bin, err := binSvc.Create(file.Filename, fileBytes) bin, err := binSvc.Create(file.Filename, fileBytes)
if err != nil { if err != nil {
return nil, err return err
} }
imgId = bin.Id imgId = bin.Id
} }
return &ImageMetaData{ meta.ImageId = imgId
ImageId: imgId, meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Image) Title() string { func (e *Image) Title() string {

View File

@ -22,10 +22,9 @@ func (meta *NoteMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*NoteMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *NoteMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &NoteMetaData{ meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Note) Title() string { func (e *Note) Title() string {

View File

@ -23,11 +23,10 @@ func (meta *PageMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*PageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *PageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &PageMetaData{ meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Page) Title() string { func (e *Page) Title() string {

View File

@ -27,7 +27,7 @@ func (meta *RecipeMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
ings := strings.Split(data.FormValue("ingredients"), "\n") ings := strings.Split(data.FormValue("ingredients"), "\n")
clean := make([]string, 0) clean := make([]string, 0)
for _, ing := range ings { for _, ing := range ings {
@ -35,13 +35,12 @@ func (*RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.Binar
clean = append(clean, strings.TrimSpace(ing)) clean = append(clean, strings.TrimSpace(ing))
} }
} }
return &RecipeMetaData{ meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Yield = data.FormValue("yield")
Yield: data.FormValue("yield"), meta.Duration = data.FormValue("duration")
Duration: data.FormValue("duration"), meta.Ingredients = clean
Ingredients: clean, meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Recipe) Title() string { func (e *Recipe) Title() string {

View File

@ -24,12 +24,11 @@ func (meta *ReplyMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*ReplyMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (meta *ReplyMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &ReplyMetaData{ meta.Title = data.FormValue("title")
Title: data.FormValue("title"), meta.Url = data.FormValue("url")
Url: data.FormValue("url"), meta.Content = data.FormValue("content")
Content: data.FormValue("content"), return nil
}, nil
} }
func (e *Reply) Title() string { func (e *Reply) Title() string {

View File

@ -28,11 +28,10 @@ func (cfg *InstagramConfig) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements app.AppConfig. // ParseFormData implements app.AppConfig.
func (*InstagramConfig) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (app.AppConfig, error) { func (cfg *InstagramConfig) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &InstagramConfig{ cfg.User = data.FormValue("User")
User: data.FormValue("User"), cfg.Password = data.FormValue("Password")
Password: data.FormValue("Password"), return nil
}, nil
} }
func RegisterInstagram( func RegisterInstagram(

View File

@ -18,7 +18,7 @@ func (*MockEntryMetaData) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements model.EntryMetaData. // ParseFormData implements model.EntryMetaData.
func (*MockEntryMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) { func (*MockEntryMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
panic("unimplemented") panic("unimplemented")
} }

View File

@ -33,12 +33,11 @@ func (cfg *ActivityPubConfig) Form(binSvc model.BinaryStorageInterface) string {
} }
// ParseFormData implements app.AppConfig. // ParseFormData implements app.AppConfig.
func (*ActivityPubConfig) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (app.AppConfig, error) { func (cfg *ActivityPubConfig) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
return &ActivityPubConfig{ cfg.PreferredUsername = data.FormValue("PreferredUsername")
PreferredUsername: data.FormValue("PreferredUsername"), cfg.PublicKeyPem = data.FormValue("PublicKeyPem")
PublicKeyPem: data.FormValue("PublicKeyPem"), cfg.PrivateKeyPem = data.FormValue("PrivateKeyPem")
PrivateKeyPem: data.FormValue("PrivateKeyPem"), return nil
}, nil
} }
type WebfingerResponse struct { type WebfingerResponse struct {

View File

@ -92,12 +92,12 @@ func (h *adminHandler) HandleConfigPost(c *fiber.Ctx) error {
return c.SendStatus(404) return c.SendStatus(404)
} }
newConfig, err := config.ParseFormData(c, h.binSvc) err := config.ParseFormData(c, h.binSvc)
if err != nil { if err != nil {
return err return err
} }
h.configRepo.Update(configName, newConfig) h.configRepo.Update(configName, config)
return c.Redirect("") return c.Redirect("")

View File

@ -59,7 +59,8 @@ func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error {
return err return err
} }
entryMeta, err := entry.MetaData().ParseFormData(c, h.binSvc) entryMeta := entry.MetaData()
err = entryMeta.ParseFormData(c, h.binSvc)
if err != nil { if err != nil {
return err return err
} }
@ -106,7 +107,8 @@ func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error {
} }
// get form data // get form data
meta, err := entry.MetaData().ParseFormData(c, h.binSvc) meta := entry.MetaData()
err = meta.ParseFormData(c, h.binSvc)
if err != nil { if err != nil {
return err return err
} }