120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/site"
|
|
"context"
|
|
"strconv"
|
|
)
|
|
|
|
type UpdateArticleUseCase struct {
|
|
articleRepository article.Repository
|
|
bodyRenderer article.BodyRenderer
|
|
recorder audit.Recorder
|
|
sites site.Repository
|
|
}
|
|
|
|
type UpdateArticleCommand struct {
|
|
Body *string
|
|
SiteID *uint32
|
|
Format article.BodyFormat
|
|
}
|
|
|
|
func NewUpdateArticleUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, recorder audit.Recorder, sites site.Repository) *UpdateArticleUseCase {
|
|
return &UpdateArticleUseCase{articleRepository: articleRepository, bodyRenderer: bodyRenderer, recorder: recorder, sites: sites}
|
|
}
|
|
|
|
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
|
|
}
|
|
updated, err := g.articleRepository.Update(id, article.Update{Body: command.Body, SiteID: command.SiteID})
|
|
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) 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 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)
|
|
}
|