Files
RenewCMS/internal/infrastructure/useCases/CreateArticle.go
T

37 lines
953 B
Go

package useCases
import (
"RenewCMS/internal/domain/article"
)
type CreateArticleUseCase struct {
articleRepository article.Repository
bodyRenderer article.BodyRenderer
}
type CreateArticleCommand struct {
Title string
Body string
Format article.BodyFormat
}
func NewCreateArticleUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer) *CreateArticleUseCase {
return &CreateArticleUseCase{articleRepository, bodyRenderer}
}
func (g *CreateArticleUseCase) CreateArticle(createArticle CreateArticleCommand) (article.Article, error) {
if err := article.ValidateTitle(createArticle.Title); err != nil {
return article.Article{}, err
}
created, err := g.articleRepository.Create(article.Article{
Title: createArticle.Title,
Body: createArticle.Body,
IsOnline: false,
})
if err != nil {
return article.Article{}, err
}
return renderArticle(g.bodyRenderer, created, createArticle.Format)
}