v2 #43

Merged
h4kor merged 41 commits from v2 into main 2023-07-19 19:09:19 +00:00
6 changed files with 44 additions and 68 deletions
Showing only changes of commit eaacf70140 - Show all commits

View File

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

View File

@ -1,10 +1,7 @@
package model
import "time"
type PostEntry struct {
id string
publishedAt *time.Time
EntryBase
meta PostEntryMetaData
}
@ -13,25 +10,14 @@ type PostEntryMetaData struct {
Content string `owl:"inputType=text widget=textarea"`
}
func (e *PostEntry) ID() string {
return e.id
}
func (e *PostEntry) Content() EntryContent {
return EntryContent(e.meta.Content)
}
func (e *PostEntry) PublishedAt() *time.Time {
return e.publishedAt
}
func (e *PostEntry) MetaData() interface{} {
return &e.meta
}
func (e *PostEntry) Create(id string, publishedAt *time.Time, metaData EntryMetaData) error {
e.id = id
e.publishedAt = publishedAt
func (e *PostEntry) SetMetaData(metaData interface{}) {
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()
_, 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
}
@ -146,6 +148,8 @@ func (r *DefaultEntryRepo) sqlEntryToEntry(entry sqlEntry) (model.Entry, error)
}
metaData := reflect.New(reflect.TypeOf(e.MetaData()).Elem()).Interface()
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
}

View File

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

View File

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