feat: storage, language rules and migration

This commit is contained in:
Florian Sylvain committed 2026-11-01 17:50:28 +01:00
1 parent 8c4a1825ab
commit 0806733d5c
70 files changed
+1771 -132

No files matched your search

@@ -7,16 +7,17 @@ import (
"RenewCMS/internal/domain/builtintype"
"RenewCMS/internal/domain/role"
"RenewCMS/internal/domain/site"
"RenewCMS/internal/domain/transaction"
"context"
"errors"
)
type CreateArticleUseCase struct {
articleRepository article.Repository
bodyRenderer article.BodyRenderer
recorder audit.Recorder
sites site.Repository
authors author.Repository
transactor transaction.Transactor
bodyRenderer article.BodyRenderer
recorder audit.Recorder
sites site.Repository
authors author.Repository
}
type CreateArticleCommand struct {
@@ -26,8 +27,8 @@ type CreateArticleCommand struct {
Caller ContentCaller
}
func NewCreateArticleUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, recorder audit.Recorder, sites site.Repository, authors author.Repository) *CreateArticleUseCase {
return &CreateArticleUseCase{articleRepository: articleRepository, bodyRenderer: bodyRenderer, recorder: recorder, sites: sites, authors: authors}
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) {
@@ -71,7 +72,7 @@ func (g *CreateArticleUseCase) CreateArticle(ctx context.Context, createArticle
return article.Article{}, err
}
created, err := g.createWithFreeSlug(article.Article{
created, err := g.createWithFreeSlug(ctx, article.Article{
Title: createArticle.Title,
Body: createArticle.Body,
IsOnline: false,
@@ -90,15 +91,9 @@ func (g *CreateArticleUseCase) CreateArticle(ctx context.Context, createArticle
return renderArticle(g.bodyRenderer, created, createArticle.Format)
}
func (g *CreateArticleUseCase) createWithFreeSlug(draft article.Article) (article.Article, error) {
base := article.GenerateSlug(draft.Title)
func (g *CreateArticleUseCase) createWithFreeSlug(ctx context.Context, draft article.Article) (article.Article, error) {
for attempt := 0; attempt < maxCreateSlugAttempts; attempt++ {
slug, err := freeSlug(g.articleRepository, draft.SiteID, base, 0, firstSlugAttempt)
if err != nil {
return article.Article{}, err
}
draft.Slug = slug
created, err := g.articleRepository.Create(draft)
created, err := g.createInTransaction(ctx, draft)
if !isSlugTakenError(err) {
return created, err
}
@@ -106,6 +101,24 @@ func (g *CreateArticleUseCase) createWithFreeSlug(draft article.Article) (articl
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)
}