feat: Authors feature backend (and upgraded deprecated mods)
This commit is contained in:
1 parent
a11338094e
commit
b34a173d83
103 files changed
+6454
-240
No files matched your search
+45
-7
@@ -2,6 +2,7 @@ package test
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/article"
|
||||
"RenewCMS/internal/domain/author"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"errors"
|
||||
"sort"
|
||||
@@ -20,14 +21,36 @@ type mockArticleRepo struct {
|
||||
nextID uint32
|
||||
fail failSwitch
|
||||
createCalled bool
|
||||
credits map[uint32][]uint32
|
||||
authors *mockAuthorRepo
|
||||
}
|
||||
|
||||
func newMockArticleRepo() *mockArticleRepo {
|
||||
return &mockArticleRepo{
|
||||
articles: make(map[uint32]article.Article),
|
||||
credits: make(map[uint32][]uint32),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) withAuthors(a article.Article) article.Article {
|
||||
ids := m.credits[a.ID]
|
||||
if len(ids) == 0 || m.authors == nil {
|
||||
return a
|
||||
}
|
||||
bylines := make([]author.Byline, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if found, ok := m.authors.authors[id]; ok {
|
||||
bylines = append(bylines, found.Byline())
|
||||
}
|
||||
}
|
||||
a.Authors = bylines
|
||||
return a
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) credit(articleID uint32, authorIDs ...uint32) {
|
||||
m.credits[articleID] = append([]uint32{}, authorIDs...)
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) sortedIds() []uint32 {
|
||||
ids := make([]uint32, 0, len(m.articles))
|
||||
for id := range m.articles {
|
||||
@@ -78,7 +101,11 @@ func (m *mockArticleRepo) FindPage(filters article.Filters) (article.PageResult,
|
||||
sort.Slice(matching, func(i, j int) bool { return articleSortsBefore(matching[i], matching[j], filters.Sort) })
|
||||
start := min((filters.Page-1)*filters.Size, len(matching))
|
||||
end := min(start+filters.Size, len(matching))
|
||||
return article.PageResult{Items: matching[start:end], Total: len(matching)}, nil
|
||||
items := make([]article.Article, 0)
|
||||
for _, a := range matching[start:end] {
|
||||
items = append(items, m.withAuthors(a))
|
||||
}
|
||||
return article.PageResult{Items: items, Total: len(matching)}, nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) CountByVisibility(onlineOnly bool) (article.Counts, error) {
|
||||
@@ -142,7 +169,7 @@ func (m *mockArticleRepo) Get(id uint32) (article.Article, error) {
|
||||
if !ok {
|
||||
return article.Article{}, article.ErrArticleNotFound
|
||||
}
|
||||
return a, nil
|
||||
return m.withAuthors(a), nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) GetByName(name string) (article.Article, error) {
|
||||
@@ -174,7 +201,14 @@ func (m *mockArticleRepo) Create(a article.Article) (article.Article, error) {
|
||||
m.nextID++
|
||||
a.ID = m.nextID
|
||||
m.articles[a.ID] = a
|
||||
return a, nil
|
||||
if len(a.Authors) > 0 {
|
||||
ids := make([]uint32, 0, len(a.Authors))
|
||||
for _, byline := range a.Authors {
|
||||
ids = append(ids, byline.ID)
|
||||
}
|
||||
m.credits[a.ID] = ids
|
||||
}
|
||||
return m.withAuthors(a), nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) UpdateBody(id uint32, body string) (article.Article, error) {
|
||||
@@ -190,7 +224,7 @@ func (m *mockArticleRepo) UpdateBody(id uint32, body string) (article.Article, e
|
||||
}
|
||||
a.Body = body
|
||||
m.articles[id] = a
|
||||
return a, nil
|
||||
return m.withAuthors(a), nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) Update(id uint32, update article.Update) (article.Article, error) {
|
||||
@@ -210,8 +244,11 @@ func (m *mockArticleRepo) Update(id uint32, update article.Update) (article.Arti
|
||||
if update.SiteID != nil {
|
||||
a.SiteID = *update.SiteID
|
||||
}
|
||||
if update.AuthorIDs != nil {
|
||||
m.credits[id] = append([]uint32{}, *update.AuthorIDs...)
|
||||
}
|
||||
m.articles[id] = a
|
||||
return a, nil
|
||||
return m.withAuthors(a), nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) UpdateIsOnline(id uint32, isOnline bool) (article.Article, error) {
|
||||
@@ -224,7 +261,7 @@ func (m *mockArticleRepo) UpdateIsOnline(id uint32, isOnline bool) (article.Arti
|
||||
}
|
||||
a.IsOnline = isOnline
|
||||
m.articles[id] = a
|
||||
return a, nil
|
||||
return m.withAuthors(a), nil
|
||||
}
|
||||
|
||||
func (m *mockArticleRepo) Delete(id uint32) error {
|
||||
@@ -235,6 +272,7 @@ func (m *mockArticleRepo) Delete(id uint32) error {
|
||||
return article.ErrArticleNotFound
|
||||
}
|
||||
delete(m.articles, id)
|
||||
delete(m.credits, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -256,7 +294,7 @@ var _ article.Repository = &mockArticleRepo{}
|
||||
|
||||
func TestCreateArticle(t *testing.T) {
|
||||
repo := newMockArticleRepo()
|
||||
uc := useCases.NewCreateArticleUseCase(repo, &fakeBodyRenderer{}, discardRecorder, plainSites())
|
||||
uc := useCases.NewCreateArticleUseCase(repo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors())
|
||||
|
||||
t.Run("Valid Title", func(t *testing.T) {
|
||||
cmd := useCases.CreateArticleCommand{Title: "Valid Title", Body: "Content"}
|
||||
|
||||
Reference in new issue
Block a user