168 lines
5.2 KiB
Go
168 lines
5.2 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/author"
|
|
"RenewCMS/internal/domain/site"
|
|
"context"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type UpdateArticleUseCase struct {
|
|
articleRepository article.Repository
|
|
bodyRenderer article.BodyRenderer
|
|
recorder audit.Recorder
|
|
sites site.Repository
|
|
authors author.Repository
|
|
}
|
|
|
|
type UpdateArticleCommand struct {
|
|
Body *string
|
|
SiteID *uint32
|
|
AuthorIDs *[]uint32
|
|
Format article.BodyFormat
|
|
}
|
|
|
|
func NewUpdateArticleUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, recorder audit.Recorder, sites site.Repository, authors author.Repository) *UpdateArticleUseCase {
|
|
return &UpdateArticleUseCase{articleRepository: articleRepository, bodyRenderer: bodyRenderer, recorder: recorder, sites: sites, authors: authors}
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) UpdateBody(ctx context.Context, id uint32, body string, format article.BodyFormat) (article.Article, error) {
|
|
return g.Update(ctx, id, UpdateArticleCommand{Body: &body, Format: format})
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) Update(ctx context.Context, id uint32, command UpdateArticleCommand) (article.Article, error) {
|
|
before, err := g.articleRepository.Get(id)
|
|
if err != nil {
|
|
return article.Article{}, err
|
|
}
|
|
destination, err := g.destinationSite(command)
|
|
if err != nil {
|
|
return article.Article{}, err
|
|
}
|
|
if err := g.ensureAuthorsExist(command.AuthorIDs); err != nil {
|
|
return article.Article{}, err
|
|
}
|
|
updated, err := g.articleRepository.Update(id, article.Update{Body: command.Body, SiteID: command.SiteID, AuthorIDs: command.AuthorIDs})
|
|
if err != nil {
|
|
g.recordFailure(ctx, audit.ActionArticleEdit, before, err)
|
|
return article.Article{}, err
|
|
}
|
|
g.recordEdit(ctx, before, updated, destination)
|
|
return renderArticle(g.bodyRenderer, updated, command.Format)
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) destinationSite(command UpdateArticleCommand) (*site.Site, error) {
|
|
if command.SiteID == nil {
|
|
return nil, nil
|
|
}
|
|
destination, err := g.sites.Get(*command.SiteID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &destination, nil
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) ensureAuthorsExist(ids *[]uint32) error {
|
|
if ids == nil {
|
|
return nil
|
|
}
|
|
for _, id := range *ids {
|
|
if _, err := g.authors.Get(id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func authorsChange(before article.Article, after article.Article) (audit.Change, bool) {
|
|
beforeIDs := bylineIDs(before.Authors)
|
|
afterIDs := bylineIDs(after.Authors)
|
|
if slices.Equal(beforeIDs, afterIDs) {
|
|
return audit.Change{}, false
|
|
}
|
|
return audit.TextChange(audit.FieldAuthors, bylineNames(before.Authors), bylineNames(after.Authors)), true
|
|
}
|
|
|
|
func bylineIDs(bylines []author.Byline) []uint32 {
|
|
ids := make([]uint32, 0, len(bylines))
|
|
for _, byline := range bylines {
|
|
ids = append(ids, byline.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func bylineNames(bylines []author.Byline) string {
|
|
names := make([]string, 0, len(bylines))
|
|
for _, byline := range bylines {
|
|
names = append(names, byline.Name)
|
|
}
|
|
return strings.Join(names, authorChangeSeparator)
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) recordEdit(ctx context.Context, before article.Article, after article.Article, destination *site.Site) {
|
|
changes := audit.ArticleChanges(before, after)
|
|
if destination != nil && before.SiteID != after.SiteID {
|
|
changes = append(changes, audit.TextChange(audit.FieldSite, g.siteName(before.SiteID), destination.Name))
|
|
}
|
|
if change, changed := authorsChange(before, after); changed {
|
|
changes = append(changes, change)
|
|
}
|
|
if len(changes) == 0 {
|
|
return
|
|
}
|
|
event := audit.NewEvent(audit.ActionArticleEdit, audit.CategoryArticles)
|
|
event.Target = articleTarget(after)
|
|
event.Changes = changes
|
|
g.recorder.Record(ctx, event)
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) siteName(id uint32) string {
|
|
found, err := g.sites.Get(id)
|
|
if err != nil {
|
|
return strconv.FormatUint(uint64(id), 10)
|
|
}
|
|
return found.Name
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) UpdateIsOnline(ctx context.Context, id uint32, isOnline bool, format article.BodyFormat) (article.Article, error) {
|
|
before, err := g.articleRepository.Get(id)
|
|
if err != nil {
|
|
return article.Article{}, err
|
|
}
|
|
updated, err := g.articleRepository.UpdateIsOnline(id, isOnline)
|
|
if err != nil {
|
|
g.recordFailure(ctx, publishAction(isOnline), before, err)
|
|
return article.Article{}, err
|
|
}
|
|
g.recordChange(ctx, publishAction(isOnline), before, updated)
|
|
return renderArticle(g.bodyRenderer, updated, format)
|
|
}
|
|
|
|
func publishAction(isOnline bool) string {
|
|
if isOnline {
|
|
return audit.ActionArticlePublish
|
|
}
|
|
return audit.ActionArticleUnpublish
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) recordChange(ctx context.Context, action string, before article.Article, after article.Article) {
|
|
changes := audit.ArticleChanges(before, after)
|
|
if len(changes) == 0 {
|
|
return
|
|
}
|
|
event := audit.NewEvent(action, audit.CategoryArticles)
|
|
event.Target = articleTarget(after)
|
|
event.Changes = changes
|
|
g.recorder.Record(ctx, event)
|
|
}
|
|
|
|
func (g *UpdateArticleUseCase) recordFailure(ctx context.Context, action string, before article.Article, cause error) {
|
|
event := audit.NewEvent(action, audit.CategoryArticles)
|
|
event.Target = articleTarget(before)
|
|
recordFailure(ctx, g.recorder, event, cause)
|
|
}
|