feat: SEO backend data (1/4)
This commit is contained in:
1 parent
cb1410f0b0
commit
0eb46b0d74
90 files changed
+6094
-350
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package useCases
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/article"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const (
|
||||
firstSlugAttempt = 1
|
||||
firstSuggestedAttempt = 2
|
||||
maxCreateSlugAttempts = 5
|
||||
maxFreeSlugAttempts = 100
|
||||
)
|
||||
|
||||
func freeSlug(articles article.Repository, siteID uint32, base string, exceptID uint32, firstAttempt int) (string, error) {
|
||||
for attempt := firstAttempt; attempt <= maxFreeSlugAttempts; attempt++ {
|
||||
candidate := article.SlugWithSuffix(base, attempt)
|
||||
taken, err := articles.SlugTaken(siteID, candidate, exceptID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !taken {
|
||||
return candidate, nil
|
||||
}
|
||||
}
|
||||
return "", article.ErrSlugTaken
|
||||
}
|
||||
|
||||
func slugConflict(articles article.Repository, siteID uint32, slug string, exceptID uint32) error {
|
||||
suggestion, err := freeSlug(articles, siteID, slug, exceptID, firstSuggestedAttempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return &article.SlugTakenError{Suggestion: suggestion}
|
||||
}
|
||||
|
||||
func isSlugTakenError(err error) bool {
|
||||
return errors.Is(err, article.ErrSlugTaken)
|
||||
}
|
||||
Reference in new issue
Block a user