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"
type AppConfig interface {
Form(binSvc model.BinaryStorageInterface) string
ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (AppConfig, error)
model.Formable
}
type ConfigRegister struct {

View File

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

View File

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

View File

@ -23,11 +23,10 @@ func (meta *ArticleMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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 (meta *ArticleMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
meta.Title = data.FormValue("title")
meta.Content = data.FormValue("content")
return nil
}
func (e *Article) Title() string {

View File

@ -24,12 +24,11 @@ func (meta *BookmarkMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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 (meta *BookmarkMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
meta.Title = data.FormValue("title")
meta.Url = data.FormValue("url")
meta.Content = data.FormValue("content")
return nil
}
func (e *Bookmark) Title() string {

View File

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

View File

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

View File

@ -23,11 +23,10 @@ func (meta *PageMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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 (meta *PageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
meta.Title = data.FormValue("title")
meta.Content = data.FormValue("content")
return nil
}
func (e *Page) Title() string {

View File

@ -27,7 +27,7 @@ func (meta *RecipeMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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")
clean := make([]string, 0)
for _, ing := range ings {
@ -35,13 +35,12 @@ func (*RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.Binar
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
meta.Title = data.FormValue("title")
meta.Yield = data.FormValue("yield")
meta.Duration = data.FormValue("duration")
meta.Ingredients = clean
meta.Content = data.FormValue("content")
return nil
}
func (e *Recipe) Title() string {

View File

@ -24,12 +24,11 @@ func (meta *ReplyMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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 (meta *ReplyMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) error {
meta.Title = data.FormValue("title")
meta.Url = data.FormValue("url")
meta.Content = data.FormValue("content")
return nil
}
func (e *Reply) Title() string {

View File

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

View File

@ -18,7 +18,7 @@ func (*MockEntryMetaData) Form(binSvc model.BinaryStorageInterface) string {
}
// 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")
}

View File

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

View File

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

View File

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