add article
This commit is contained in:
94
internal/domains/article/services/article_service.go
Normal file
94
internal/domains/article/services/article_service.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"tyapi-server/internal/domains/article/entities"
|
||||
)
|
||||
|
||||
// ArticleService 文章领域服务
|
||||
// 处理文章相关的业务逻辑,包括验证、状态管理等
|
||||
type ArticleService struct{}
|
||||
|
||||
// NewArticleService 创建文章领域服务
|
||||
func NewArticleService() *ArticleService {
|
||||
return &ArticleService{}
|
||||
}
|
||||
|
||||
// ValidateArticle 验证文章
|
||||
// 检查文章是否符合业务规则
|
||||
func (s *ArticleService) ValidateArticle(article *entities.Article) error {
|
||||
// 1. 基础验证
|
||||
if err := article.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 业务规则验证
|
||||
// 标题不能包含敏感词
|
||||
if s.containsSensitiveWords(article.Title) {
|
||||
return entities.NewValidationError("文章标题包含敏感词")
|
||||
}
|
||||
|
||||
// 内容不能包含敏感词
|
||||
if s.containsSensitiveWords(article.Content) {
|
||||
return entities.NewValidationError("文章内容包含敏感词")
|
||||
}
|
||||
|
||||
// 摘要长度不能超过内容长度
|
||||
if article.Summary != "" && len(article.Summary) >= len(article.Content) {
|
||||
return entities.NewValidationError("文章摘要不能超过内容长度")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CanPublish 检查是否可以发布
|
||||
func (s *ArticleService) CanPublish(article *entities.Article) error {
|
||||
if !article.CanPublish() {
|
||||
return entities.NewValidationError("文章状态不允许发布")
|
||||
}
|
||||
|
||||
// 检查必填字段
|
||||
if article.Title == "" {
|
||||
return entities.NewValidationError("文章标题不能为空")
|
||||
}
|
||||
if article.Content == "" {
|
||||
return entities.NewValidationError("文章内容不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CanEdit 检查是否可以编辑
|
||||
func (s *ArticleService) CanEdit(article *entities.Article) error {
|
||||
if !article.CanEdit() {
|
||||
return entities.NewValidationError("文章状态不允许编辑")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// containsSensitiveWords 检查是否包含敏感词
|
||||
func (s *ArticleService) containsSensitiveWords(text string) bool {
|
||||
// TODO: 实现敏感词检查逻辑
|
||||
// 这里可以集成敏感词库或调用外部服务
|
||||
sensitiveWords := []string{
|
||||
"敏感词1",
|
||||
"敏感词2",
|
||||
"敏感词3",
|
||||
}
|
||||
|
||||
for _, word := range sensitiveWords {
|
||||
if len(word) > 0 && len(text) > 0 {
|
||||
// 简单的字符串包含检查
|
||||
// 实际项目中应该使用更复杂的算法
|
||||
if len(text) >= len(word) {
|
||||
for i := 0; i <= len(text)-len(word); i++ {
|
||||
if text[i:i+len(word)] == word {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user