fix
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -9,20 +12,20 @@ import (
|
||||
|
||||
// ProductDocumentation 产品文档实体
|
||||
type ProductDocumentation struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"文档ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;uniqueIndex" comment:"产品ID"`
|
||||
Title string `gorm:"type:varchar(200);not null" comment:"文档标题"`
|
||||
Content string `gorm:"type:text;not null" comment:"文档内容"`
|
||||
UsageGuide string `gorm:"type:text" comment:"使用指南"`
|
||||
APIDocs string `gorm:"type:text" comment:"API文档"`
|
||||
Examples string `gorm:"type:text" comment:"使用示例"`
|
||||
FAQ string `gorm:"type:text" comment:"常见问题"`
|
||||
Version string `gorm:"type:varchar(20);default:'1.0'" comment:"文档版本"`
|
||||
Published bool `gorm:"default:false" comment:"是否已发布"`
|
||||
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" comment:"文档ID"`
|
||||
ProductID string `gorm:"type:varchar(36);not null;uniqueIndex" comment:"产品ID"`
|
||||
RequestURL string `gorm:"type:varchar(500);not null" comment:"请求链接"`
|
||||
RequestMethod string `gorm:"type:varchar(20);not null" comment:"请求方法"`
|
||||
BasicInfo string `gorm:"type:text" comment:"基础说明(请求头配置、参数加密等)"`
|
||||
RequestParams string `gorm:"type:text" comment:"请求参数"`
|
||||
ResponseFields string `gorm:"type:text" comment:"返回字段说明"`
|
||||
ResponseExample string `gorm:"type:text" comment:"响应示例"`
|
||||
ErrorCodes string `gorm:"type:text" comment:"错误代码"`
|
||||
Version string `gorm:"type:varchar(20);default:'1.0'" comment:"文档版本"`
|
||||
|
||||
// 关联关系
|
||||
Product *Product `gorm:"foreignKey:ProductID" comment:"产品"`
|
||||
|
||||
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||||
@@ -41,29 +44,16 @@ func (pd *ProductDocumentation) IsValid() bool {
|
||||
return pd.DeletedAt.Time.IsZero()
|
||||
}
|
||||
|
||||
// IsPublished 检查文档是否已发布
|
||||
func (pd *ProductDocumentation) IsPublished() bool {
|
||||
return pd.Published
|
||||
}
|
||||
|
||||
// Publish 发布文档
|
||||
func (pd *ProductDocumentation) Publish() {
|
||||
pd.Published = true
|
||||
}
|
||||
|
||||
// Unpublish 取消发布文档
|
||||
func (pd *ProductDocumentation) Unpublish() {
|
||||
pd.Published = false
|
||||
}
|
||||
|
||||
// UpdateContent 更新文档内容
|
||||
func (pd *ProductDocumentation) UpdateContent(title, content, usageGuide, apiDocs, examples, faq string) {
|
||||
pd.Title = title
|
||||
pd.Content = content
|
||||
pd.UsageGuide = usageGuide
|
||||
pd.APIDocs = apiDocs
|
||||
pd.Examples = examples
|
||||
pd.FAQ = faq
|
||||
func (pd *ProductDocumentation) UpdateContent(requestURL, requestMethod, basicInfo, requestParams, responseFields, responseExample, errorCodes string) {
|
||||
pd.RequestURL = requestURL
|
||||
pd.RequestMethod = requestMethod
|
||||
pd.BasicInfo = basicInfo
|
||||
pd.RequestParams = requestParams
|
||||
pd.ResponseFields = responseFields
|
||||
pd.ResponseExample = responseExample
|
||||
pd.ErrorCodes = errorCodes
|
||||
}
|
||||
|
||||
// IncrementVersion 增加版本号
|
||||
@@ -75,4 +65,157 @@ func (pd *ProductDocumentation) IncrementVersion() {
|
||||
// 这里可以实现更复杂的版本号递增逻辑
|
||||
pd.Version = pd.Version + ".1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate 验证文档完整性
|
||||
func (pd *ProductDocumentation) Validate() error {
|
||||
if pd.RequestURL == "" {
|
||||
return errors.New("请求链接不能为空")
|
||||
}
|
||||
if pd.RequestMethod == "" {
|
||||
return errors.New("请求方法不能为空")
|
||||
}
|
||||
if pd.ProductID == "" {
|
||||
return errors.New("产品ID不能为空")
|
||||
}
|
||||
|
||||
// 验证请求方法
|
||||
validMethods := []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
|
||||
methodValid := false
|
||||
for _, method := range validMethods {
|
||||
if strings.ToUpper(pd.RequestMethod) == method {
|
||||
methodValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !methodValid {
|
||||
return fmt.Errorf("无效的请求方法: %s", pd.RequestMethod)
|
||||
}
|
||||
|
||||
// 验证URL格式(简单验证)
|
||||
if !strings.HasPrefix(pd.RequestURL, "http://") && !strings.HasPrefix(pd.RequestURL, "https://") {
|
||||
return errors.New("请求链接必须以http://或https://开头")
|
||||
}
|
||||
|
||||
// 验证版本号格式
|
||||
if pd.Version != "" {
|
||||
if !isValidVersion(pd.Version) {
|
||||
return fmt.Errorf("无效的版本号格式: %s", pd.Version)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CanPublish 检查是否可以发布
|
||||
func (pd *ProductDocumentation) CanPublish() error {
|
||||
if err := pd.Validate(); err != nil {
|
||||
return fmt.Errorf("文档验证失败: %w", err)
|
||||
}
|
||||
if pd.BasicInfo == "" {
|
||||
return errors.New("基础说明不能为空")
|
||||
}
|
||||
if pd.RequestParams == "" {
|
||||
return errors.New("请求参数不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateDocumentation 更新文档内容并自动递增版本
|
||||
func (pd *ProductDocumentation) UpdateDocumentation(requestURL, requestMethod, basicInfo, requestParams, responseFields, responseExample, errorCodes string) error {
|
||||
// 验证必填字段
|
||||
if requestURL == "" || requestMethod == "" {
|
||||
return errors.New("请求链接和请求方法不能为空")
|
||||
}
|
||||
|
||||
// 更新内容
|
||||
pd.UpdateContent(requestURL, requestMethod, basicInfo, requestParams, responseFields, responseExample, errorCodes)
|
||||
|
||||
// 自动递增版本
|
||||
pd.IncrementVersion()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// GetDocumentationSummary 获取文档摘要
|
||||
func (pd *ProductDocumentation) GetDocumentationSummary() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"id": pd.ID,
|
||||
"product_id": pd.ProductID,
|
||||
"request_url": pd.RequestURL,
|
||||
"method": pd.RequestMethod,
|
||||
"version": pd.Version,
|
||||
"created_at": pd.CreatedAt,
|
||||
"updated_at": pd.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// HasRequiredFields 检查是否包含必需字段
|
||||
func (pd *ProductDocumentation) HasRequiredFields() bool {
|
||||
return pd.RequestURL != "" &&
|
||||
pd.RequestMethod != "" &&
|
||||
pd.ProductID != "" &&
|
||||
pd.BasicInfo != "" &&
|
||||
pd.RequestParams != ""
|
||||
}
|
||||
|
||||
// IsComplete 检查文档是否完整
|
||||
func (pd *ProductDocumentation) IsComplete() bool {
|
||||
return pd.HasRequiredFields() &&
|
||||
pd.ResponseFields != "" &&
|
||||
pd.ResponseExample != "" &&
|
||||
pd.ErrorCodes != ""
|
||||
}
|
||||
|
||||
// GetCompletionPercentage 获取文档完成度百分比
|
||||
func (pd *ProductDocumentation) GetCompletionPercentage() int {
|
||||
totalFields := 8 // 总字段数
|
||||
completedFields := 0
|
||||
|
||||
if pd.RequestURL != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.RequestMethod != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.BasicInfo != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.RequestParams != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.ResponseFields != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.ResponseExample != "" {
|
||||
completedFields++
|
||||
}
|
||||
if pd.ErrorCodes != "" {
|
||||
completedFields++
|
||||
}
|
||||
return (completedFields * 100) / totalFields
|
||||
}
|
||||
|
||||
// isValidVersion 验证版本号格式
|
||||
func isValidVersion(version string) bool {
|
||||
// 简单的版本号验证:x.y.z 格式
|
||||
parts := strings.Split(version, ".")
|
||||
if len(parts) < 1 || len(parts) > 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, part := range parts {
|
||||
if part == "" {
|
||||
return false
|
||||
}
|
||||
// 检查是否为数字
|
||||
for _, char := range part {
|
||||
if char < '0' || char > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user