125 lines
3.7 KiB
Go
125 lines
3.7 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/author"
|
|
"RenewCMS/internal/domain/builtintype"
|
|
"RenewCMS/internal/domain/role"
|
|
"RenewCMS/internal/domain/site"
|
|
"RenewCMS/internal/domain/transaction"
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
type CreateArticleUseCase struct {
|
|
transactor transaction.Transactor
|
|
bodyRenderer article.BodyRenderer
|
|
recorder audit.Recorder
|
|
sites site.Repository
|
|
authors author.Repository
|
|
}
|
|
|
|
type CreateArticleCommand struct {
|
|
Title string
|
|
Body string
|
|
Format article.BodyFormat
|
|
Caller ContentCaller
|
|
}
|
|
|
|
func NewCreateArticleUseCase(transactor transaction.Transactor, bodyRenderer article.BodyRenderer, recorder audit.Recorder, sites site.Repository, authors author.Repository) *CreateArticleUseCase {
|
|
return &CreateArticleUseCase{transactor: transactor, bodyRenderer: bodyRenderer, recorder: recorder, sites: sites, authors: authors}
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) defaultSiteID() (uint32, error) {
|
|
defaultSite, err := g.sites.GetDefault()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return defaultSite.ID, nil
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) creatorBylines(userID uint32) ([]author.Byline, error) {
|
|
found, err := g.authors.GetByUserID(userID)
|
|
if errors.Is(err, author.ErrAuthorNotFound) {
|
|
return []author.Byline{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []author.Byline{found.Byline()}, nil
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) CreateArticle(ctx context.Context, createArticle CreateArticleCommand) (article.Article, error) {
|
|
if err := createArticle.Caller.requireHolding(role.KeyContentCreate, builtintype.KeyArticle); err != nil {
|
|
return article.Article{}, err
|
|
}
|
|
event := audit.NewEvent(audit.ActionArticleCreate, audit.CategoryArticles)
|
|
if err := article.ValidateTitle(createArticle.Title); err != nil {
|
|
recordFailure(ctx, g.recorder, event, err)
|
|
return article.Article{}, err
|
|
}
|
|
|
|
siteID, err := g.defaultSiteID()
|
|
if err != nil {
|
|
recordFailure(ctx, g.recorder, event, err)
|
|
return article.Article{}, err
|
|
}
|
|
|
|
creditedAuthors, err := g.creatorBylines(createArticle.Caller.UserID)
|
|
if err != nil {
|
|
recordFailure(ctx, g.recorder, event, err)
|
|
return article.Article{}, err
|
|
}
|
|
|
|
created, err := g.createWithFreeSlug(ctx, article.Article{
|
|
Title: createArticle.Title,
|
|
Body: createArticle.Body,
|
|
IsOnline: false,
|
|
SiteID: siteID,
|
|
CreatedBy: createArticle.Caller.UserID,
|
|
Authors: creditedAuthors,
|
|
SEODismissed: []string{},
|
|
})
|
|
if err != nil {
|
|
recordFailure(ctx, g.recorder, event, err)
|
|
return article.Article{}, err
|
|
}
|
|
|
|
event.Target = articleTarget(created)
|
|
g.recorder.Record(ctx, event)
|
|
return renderArticle(g.bodyRenderer, created, createArticle.Format)
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) createWithFreeSlug(ctx context.Context, draft article.Article) (article.Article, error) {
|
|
for attempt := 0; attempt < maxCreateSlugAttempts; attempt++ {
|
|
created, err := g.createInTransaction(ctx, draft)
|
|
if !isSlugTakenError(err) {
|
|
return created, err
|
|
}
|
|
}
|
|
return article.Article{}, article.ErrSlugTaken
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) createInTransaction(ctx context.Context, draft article.Article) (article.Article, error) {
|
|
var created article.Article
|
|
err := g.transactor.Run(ctx, func(tx transaction.Repositories) error {
|
|
lang, err := tx.Languages.LockStartingLanguage(draft.SiteID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
draft.Lang = lang
|
|
draft.Slug, err = freeSlug(tx.Articles, draft.SiteID, draft.Lang, article.GenerateSlug(draft.Title), 0, firstSlugAttempt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
created, err = tx.Articles.Create(draft)
|
|
return err
|
|
})
|
|
return created, err
|
|
}
|
|
|
|
func articleTarget(a article.Article) audit.Target {
|
|
return audit.NewTarget(audit.TargetArticle, a.ID, a.Title)
|
|
}
|