better entry ids

This commit is contained in:
Niko Abeler 2023-08-08 19:41:07 +02:00
parent 304db7ec03
commit 5939bbd09d
5 changed files with 68 additions and 6 deletions

View File

@ -1,8 +1,11 @@
package app package app
import ( import (
"fmt"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"regexp"
"strings"
) )
type EntryService struct { type EntryService struct {
@ -14,6 +17,22 @@ func NewEntryService(entryRepository repository.EntryRepository) *EntryService {
} }
func (s *EntryService) Create(entry model.Entry) error { func (s *EntryService) Create(entry model.Entry) error {
// try to find a good ID
m := regexp.MustCompile(`[^a-z0-9-]`)
prefix := m.ReplaceAllString(strings.ToLower(entry.Title()), "-")
title := prefix
counter := 0
for {
_, err := s.EntryRepository.FindById(title)
if err == nil {
counter += 1
title = prefix + "-" + fmt.Sprintf("%s-%d", prefix, counter)
} else {
break
}
}
entry.SetID(title)
return s.EntryRepository.Create(entry) return s.EntryRepository.Create(entry)
} }

45
app/entry_service_test.go Normal file
View File

@ -0,0 +1,45 @@
package app_test
import (
"owl-blogs/app"
"owl-blogs/infra"
"owl-blogs/test"
"testing"
"github.com/stretchr/testify/require"
)
func setupService() *app.EntryService {
db := test.NewMockDb()
register := app.NewEntryTypeRegistry()
register.Register(&test.MockEntry{})
repo := infra.NewEntryRepository(db, register)
service := app.NewEntryService(repo)
return service
}
func TestNiceEntryId(t *testing.T) {
service := setupService()
entry := &test.MockEntry{}
meta := test.MockEntryMetaData{
Title: "Hello World",
}
entry.SetMetaData(&meta)
err := service.Create(entry)
require.NoError(t, err)
require.Equal(t, "hello-world", entry.ID())
}
func TestNoTitleCreation(t *testing.T) {
service := setupService()
entry := &test.MockEntry{}
meta := test.MockEntryMetaData{
Title: "",
}
entry.SetMetaData(&meta)
err := service.Create(entry)
require.NoError(t, err)
require.NotEqual(t, "", entry.ID())
}

View File

@ -97,7 +97,7 @@ func (r *DefaultEntryRepo) FindById(id string) (model.Entry, error) {
return nil, err return nil, err
} }
if data.Id == "" { if data.Id == "" {
return nil, nil return nil, errors.New("entry not found")
} }
return r.sqlEntryToEntry(data) return r.sqlEntryToEntry(data)
} }

View File

@ -9,6 +9,7 @@ type MockEntryMetaData struct {
Str string Str string
Number int Number int
Date time.Time Date time.Time
Title string
} }
type MockEntry struct { type MockEntry struct {
@ -29,5 +30,5 @@ func (e *MockEntry) SetMetaData(metaData interface{}) {
} }
func (e *MockEntry) Title() string { func (e *MockEntry) Title() string {
return "" return e.metaData.Title
} }

View File

@ -66,7 +66,7 @@ func (f *MockFormData) FormValue(key string, defaultValue ...string) string {
func TestFieldToFormField(t *testing.T) { func TestFieldToFormField(t *testing.T) {
field := reflect.TypeOf(&MockData{}).Elem().Field(0) field := reflect.TypeOf(&MockData{}).Elem().Field(0)
formField, err := forms.FieldToFormField(field, "") formField, err := forms.FieldToFormField(field, reflect.Value{})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "Image", formField.Name) require.Equal(t, "Image", formField.Name)
require.Equal(t, "file", formField.Params.InputType) require.Equal(t, "file", formField.Params.InputType)
@ -86,11 +86,8 @@ func TestForm_HtmlForm(t *testing.T) {
form := forms.NewForm(&MockData{}, nil) form := forms.NewForm(&MockData{}, nil)
html, err := form.HtmlForm() html, err := form.HtmlForm()
require.NoError(t, err) require.NoError(t, err)
require.Contains(t, html, "<form")
require.Contains(t, html, "method=\"POST\"")
require.Contains(t, html, "<input type=\"file\" name=\"Image\"") require.Contains(t, html, "<input type=\"file\" name=\"Image\"")
require.Contains(t, html, "<input type=\"text\" name=\"Content\"") require.Contains(t, html, "<input type=\"text\" name=\"Content\"")
require.Contains(t, html, "<input type=\"submit\" value=\"Submit\"")
} }