50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/audit"
|
|
"context"
|
|
)
|
|
|
|
type CreateArticleUseCase struct {
|
|
articleRepository article.Repository
|
|
bodyRenderer article.BodyRenderer
|
|
recorder audit.Recorder
|
|
}
|
|
|
|
type CreateArticleCommand struct {
|
|
Title string
|
|
Body string
|
|
Format article.BodyFormat
|
|
}
|
|
|
|
func NewCreateArticleUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, recorder audit.Recorder) *CreateArticleUseCase {
|
|
return &CreateArticleUseCase{articleRepository, bodyRenderer, recorder}
|
|
}
|
|
|
|
func (g *CreateArticleUseCase) CreateArticle(ctx context.Context, createArticle CreateArticleCommand) (article.Article, error) {
|
|
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
|
|
}
|
|
|
|
created, err := g.articleRepository.Create(article.Article{
|
|
Title: createArticle.Title,
|
|
Body: createArticle.Body,
|
|
IsOnline: false,
|
|
})
|
|
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 articleTarget(a article.Article) audit.Target {
|
|
return audit.NewTarget(audit.TargetArticle, a.ID, a.Title)
|
|
}
|