entry base

This commit is contained in:
Niko Abeler 2023-07-08 15:56:42 +02:00
parent 1514e7533c
commit eaacf70140
6 changed files with 44 additions and 68 deletions

View File

@ -9,8 +9,33 @@ type Entry interface {
Content() EntryContent Content() EntryContent
PublishedAt() *time.Time PublishedAt() *time.Time
MetaData() interface{} MetaData() interface{}
Create(id string, publishedAt *time.Time, metaData EntryMetaData) error // Create(id string, publishedAt *time.Time, metaData EntryMetaData) error
SetID(id string)
SetPublishedAt(publishedAt *time.Time)
SetMetaData(metaData interface{})
} }
type EntryMetaData interface { type EntryMetaData interface {
} }
type EntryBase struct {
id string
publishedAt *time.Time
}
func (e *EntryBase) ID() string {
return e.id
}
func (e *EntryBase) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *EntryBase) SetID(id string) {
e.id = id
}
func (e *EntryBase) SetPublishedAt(publishedAt *time.Time) {
e.publishedAt = publishedAt
}

View File

@ -1,11 +1,8 @@
package model package model
import "time"
type ImageEntry struct { type ImageEntry struct {
id string EntryBase
publishedAt *time.Time meta ImageEntryMetaData
meta ImageEntryMetaData
} }
type ImageEntryMetaData struct { type ImageEntryMetaData struct {
@ -13,25 +10,14 @@ type ImageEntryMetaData struct {
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
func (e *ImageEntry) ID() string {
return e.id
}
func (e *ImageEntry) Content() EntryContent { func (e *ImageEntry) Content() EntryContent {
return EntryContent(e.meta.Content) return EntryContent(e.meta.Content)
} }
func (e *ImageEntry) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *ImageEntry) MetaData() interface{} { func (e *ImageEntry) MetaData() interface{} {
return &e.meta return &e.meta
} }
func (e *ImageEntry) Create(id string, publishedAt *time.Time, metaData EntryMetaData) error { func (e *ImageEntry) SetMetaData(metaData interface{}) {
e.id = id
e.publishedAt = publishedAt
e.meta = *metaData.(*ImageEntryMetaData) e.meta = *metaData.(*ImageEntryMetaData)
return nil
} }

View File

@ -1,11 +1,8 @@
package model package model
import "time"
type PostEntry struct { type PostEntry struct {
id string EntryBase
publishedAt *time.Time meta PostEntryMetaData
meta PostEntryMetaData
} }
type PostEntryMetaData struct { type PostEntryMetaData struct {
@ -13,25 +10,14 @@ type PostEntryMetaData struct {
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
func (e *PostEntry) ID() string {
return e.id
}
func (e *PostEntry) Content() EntryContent { func (e *PostEntry) Content() EntryContent {
return EntryContent(e.meta.Content) return EntryContent(e.meta.Content)
} }
func (e *PostEntry) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *PostEntry) MetaData() interface{} { func (e *PostEntry) MetaData() interface{} {
return &e.meta return &e.meta
} }
func (e *PostEntry) Create(id string, publishedAt *time.Time, metaData EntryMetaData) error { func (e *PostEntry) SetMetaData(metaData interface{}) {
e.id = id
e.publishedAt = publishedAt
e.meta = *metaData.(*PostEntryMetaData) e.meta = *metaData.(*PostEntryMetaData)
return nil
} }

View File

@ -41,7 +41,9 @@ func (r *DefaultEntryRepo) Create(entry model.Entry, publishedAt *time.Time, met
id := uuid.New().String() id := uuid.New().String()
_, err = r.db.Exec("INSERT INTO entries (id, type, published_at, meta_data) VALUES (?, ?, ?, ?)", id, t, publishedAt, metaDataJson) _, err = r.db.Exec("INSERT INTO entries (id, type, published_at, meta_data) VALUES (?, ?, ?, ?)", id, t, publishedAt, metaDataJson)
entry.Create(id, publishedAt, metaData) entry.SetID(id)
entry.SetPublishedAt(publishedAt)
entry.SetMetaData(metaData)
return err return err
} }
@ -146,6 +148,8 @@ func (r *DefaultEntryRepo) sqlEntryToEntry(entry sqlEntry) (model.Entry, error)
} }
metaData := reflect.New(reflect.TypeOf(e.MetaData()).Elem()).Interface() metaData := reflect.New(reflect.TypeOf(e.MetaData()).Elem()).Interface()
json.Unmarshal([]byte(*entry.MetaData), metaData) json.Unmarshal([]byte(*entry.MetaData), metaData)
e.Create(entry.Id, entry.PublishedAt, metaData) e.SetID(entry.Id)
e.SetPublishedAt(entry.PublishedAt)
e.SetMetaData(metaData)
return e, nil return e, nil
} }

View File

@ -12,30 +12,18 @@ type MockEntryMetaData struct {
} }
type MockEntry struct { type MockEntry struct {
id string model.EntryBase
publishedAt *time.Time metaData *MockEntryMetaData
metaData *MockEntryMetaData
}
func (e *MockEntry) ID() string {
return e.id
} }
func (e *MockEntry) Content() model.EntryContent { func (e *MockEntry) Content() model.EntryContent {
return model.EntryContent(e.metaData.Str) return model.EntryContent(e.metaData.Str)
} }
func (e *MockEntry) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *MockEntry) MetaData() interface{} { func (e *MockEntry) MetaData() interface{} {
return e.metaData return e.metaData
} }
func (e *MockEntry) Create(id string, publishedAt *time.Time, metaData model.EntryMetaData) error { func (e *MockEntry) SetMetaData(metaData interface{}) {
e.id = id
e.publishedAt = publishedAt
e.metaData = metaData.(*MockEntryMetaData) e.metaData = metaData.(*MockEntryMetaData)
return nil
} }

View File

@ -14,7 +14,6 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -67,32 +66,20 @@ func (f *MockFormData) FormValue(key string, defaultValue ...string) string {
} }
type MockEntry struct { type MockEntry struct {
id string model.EntryBase
publishedAt *time.Time metaData MockEntryMetaData
metaData MockEntryMetaData
}
func (e *MockEntry) ID() string {
return e.id
} }
func (e *MockEntry) Content() model.EntryContent { func (e *MockEntry) Content() model.EntryContent {
return model.EntryContent(e.metaData.Content) return model.EntryContent(e.metaData.Content)
} }
func (e *MockEntry) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *MockEntry) MetaData() interface{} { func (e *MockEntry) MetaData() interface{} {
return &e.metaData return &e.metaData
} }
func (e *MockEntry) Create(id string, publishedAt *time.Time, metaData model.EntryMetaData) error { func (e *MockEntry) SetMetaData(metaData interface{}) {
e.id = id
e.publishedAt = publishedAt
e.metaData = *metaData.(*MockEntryMetaData) e.metaData = *metaData.(*MockEntryMetaData)
return nil
} }
func TestFieldToFormField(t *testing.T) { func TestFieldToFormField(t *testing.T) {