139 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			139 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package product | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"context" | ||
|  | 
 | ||
|  | 	"tyapi-server/internal/application/product/dto/commands" | ||
|  | 	"tyapi-server/internal/application/product/dto/responses" | ||
|  | 	"tyapi-server/internal/domains/product/entities" | ||
|  | 	"tyapi-server/internal/domains/product/services" | ||
|  | ) | ||
|  | 
 | ||
|  | // DocumentationApplicationServiceInterface 文档应用服务接口 | ||
|  | type DocumentationApplicationServiceInterface interface { | ||
|  | 	// CreateDocumentation 创建文档 | ||
|  | 	CreateDocumentation(ctx context.Context, cmd *commands.CreateDocumentationCommand) (*responses.DocumentationResponse, error) | ||
|  | 
 | ||
|  | 	// UpdateDocumentation 更新文档 | ||
|  | 	UpdateDocumentation(ctx context.Context, id string, cmd *commands.UpdateDocumentationCommand) (*responses.DocumentationResponse, error) | ||
|  | 
 | ||
|  | 	// GetDocumentation 获取文档 | ||
|  | 	GetDocumentation(ctx context.Context, id string) (*responses.DocumentationResponse, error) | ||
|  | 
 | ||
|  | 	// GetDocumentationByProductID 通过产品ID获取文档 | ||
|  | 	GetDocumentationByProductID(ctx context.Context, productID string) (*responses.DocumentationResponse, error) | ||
|  | 
 | ||
|  | 	// DeleteDocumentation 删除文档 | ||
|  | 	DeleteDocumentation(ctx context.Context, id string) error | ||
|  | 
 | ||
|  | 	// GetDocumentationsByProductIDs 批量获取文档 | ||
|  | 	GetDocumentationsByProductIDs(ctx context.Context, productIDs []string) ([]responses.DocumentationResponse, error) | ||
|  | } | ||
|  | 
 | ||
|  | // DocumentationApplicationService 文档应用服务 | ||
|  | type DocumentationApplicationService struct { | ||
|  | 	docService *services.ProductDocumentationService | ||
|  | } | ||
|  | 
 | ||
|  | // NewDocumentationApplicationService 创建文档应用服务实例 | ||
|  | func NewDocumentationApplicationService(docService *services.ProductDocumentationService) *DocumentationApplicationService { | ||
|  | 	return &DocumentationApplicationService{ | ||
|  | 		docService: docService, | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | // CreateDocumentation 创建文档 | ||
|  | func (s *DocumentationApplicationService) CreateDocumentation(ctx context.Context, cmd *commands.CreateDocumentationCommand) (*responses.DocumentationResponse, error) { | ||
|  | 	// 创建文档实体 | ||
|  | 	doc := &entities.ProductDocumentation{ | ||
|  | 		RequestURL:      cmd.RequestURL, | ||
|  | 		RequestMethod:   cmd.RequestMethod, | ||
|  | 		BasicInfo:       cmd.BasicInfo, | ||
|  | 		RequestParams:   cmd.RequestParams, | ||
|  | 		ResponseFields:  cmd.ResponseFields, | ||
|  | 		ResponseExample: cmd.ResponseExample, | ||
|  | 		ErrorCodes:      cmd.ErrorCodes, | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 调用领域服务创建文档 | ||
|  | 	err := s.docService.CreateDocumentation(ctx, cmd.ProductID, doc) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 返回响应 | ||
|  | 	resp := responses.NewDocumentationResponse(doc) | ||
|  | 	return &resp, nil | ||
|  | } | ||
|  | 
 | ||
|  | // UpdateDocumentation 更新文档 | ||
|  | func (s *DocumentationApplicationService) UpdateDocumentation(ctx context.Context, id string, cmd *commands.UpdateDocumentationCommand) (*responses.DocumentationResponse, error) { | ||
|  | 	// 调用领域服务更新文档 | ||
|  | 	err := s.docService.UpdateDocumentation(ctx, id, | ||
|  | 		cmd.RequestURL, | ||
|  | 		cmd.RequestMethod, | ||
|  | 		cmd.BasicInfo, | ||
|  | 		cmd.RequestParams, | ||
|  | 		cmd.ResponseFields, | ||
|  | 		cmd.ResponseExample, | ||
|  | 		cmd.ErrorCodes, | ||
|  | 	) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 获取更新后的文档 | ||
|  | 	doc, err := s.docService.GetDocumentation(ctx, id) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 返回响应 | ||
|  | 	resp := responses.NewDocumentationResponse(doc) | ||
|  | 	return &resp, nil | ||
|  | } | ||
|  | 
 | ||
|  | // GetDocumentation 获取文档 | ||
|  | func (s *DocumentationApplicationService) GetDocumentation(ctx context.Context, id string) (*responses.DocumentationResponse, error) { | ||
|  | 	doc, err := s.docService.GetDocumentation(ctx, id) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 返回响应 | ||
|  | 	resp := responses.NewDocumentationResponse(doc) | ||
|  | 	return &resp, nil | ||
|  | } | ||
|  | 
 | ||
|  | // GetDocumentationByProductID 通过产品ID获取文档 | ||
|  | func (s *DocumentationApplicationService) GetDocumentationByProductID(ctx context.Context, productID string) (*responses.DocumentationResponse, error) { | ||
|  | 	doc, err := s.docService.GetDocumentationByProductID(ctx, productID) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 返回响应 | ||
|  | 	resp := responses.NewDocumentationResponse(doc) | ||
|  | 	return &resp, nil | ||
|  | } | ||
|  | 
 | ||
|  | // DeleteDocumentation 删除文档 | ||
|  | func (s *DocumentationApplicationService) DeleteDocumentation(ctx context.Context, id string) error { | ||
|  | 	return s.docService.DeleteDocumentation(ctx, id) | ||
|  | } | ||
|  | 
 | ||
|  | // GetDocumentationsByProductIDs 批量获取文档 | ||
|  | func (s *DocumentationApplicationService) GetDocumentationsByProductIDs(ctx context.Context, productIDs []string) ([]responses.DocumentationResponse, error) { | ||
|  | 	docs, err := s.docService.GetDocumentationsByProductIDs(ctx, productIDs) | ||
|  | 	if err != nil { | ||
|  | 		return nil, err | ||
|  | 	} | ||
|  | 
 | ||
|  | 	var docResponses []responses.DocumentationResponse | ||
|  | 	for _, doc := range docs { | ||
|  | 		docResponses = append(docResponses, responses.NewDocumentationResponse(doc)) | ||
|  | 	} | ||
|  | 
 | ||
|  | 	return docResponses, nil | ||
|  | } |