124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"path/filepath"
|
|
"resume-platform/internal/service/document"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// DocumentHandler 文档处理器,负责作品集文档的上传、查询和删除
|
|
type DocumentHandler struct {
|
|
documentService document.DocumentService
|
|
}
|
|
|
|
// NewDocumentHandler 创建文档处理器实例
|
|
// @param documentService 文档服务层
|
|
// @return *DocumentHandler 处理器实例
|
|
// @author sunct
|
|
func NewDocumentHandler(documentService document.DocumentService) *DocumentHandler {
|
|
return &DocumentHandler{documentService: documentService}
|
|
}
|
|
|
|
// UploadDocument 上传文档接口,读取文件内容后交给 Service 层做向量化处理
|
|
// @param c Gin 上下文
|
|
// @author sunct
|
|
func (h *DocumentHandler) UploadDocument(c *gin.Context) {
|
|
userID := c.Query("user_id")
|
|
if userID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id is required"})
|
|
return
|
|
}
|
|
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
|
|
return
|
|
}
|
|
|
|
fileType := filepath.Ext(file.Filename)
|
|
|
|
reader, err := file.Open()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to open file"})
|
|
return
|
|
}
|
|
defer reader.Close()
|
|
|
|
content, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read file"})
|
|
return
|
|
}
|
|
|
|
document, err := h.documentService.UploadDocument(c.Request.Context(), userID, file.Filename, fileType, file.Size, string(content))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"id": document.ID, "name": document.Name, "status": document.Status})
|
|
}
|
|
|
|
// GetDocuments 获取用户文档列表接口
|
|
// @param c Gin 上下文
|
|
// @author sunct
|
|
func (h *DocumentHandler) GetDocuments(c *gin.Context) {
|
|
userID := c.Query("user_id")
|
|
if userID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id is required"})
|
|
return
|
|
}
|
|
|
|
documents, err := h.documentService.GetDocuments(c.Request.Context(), userID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, documents)
|
|
}
|
|
|
|
// GetDocument 获取单篇文档详情接口
|
|
// @param c Gin 上下文
|
|
// @author sunct
|
|
func (h *DocumentHandler) GetDocument(c *gin.Context) {
|
|
userID := c.Query("user_id")
|
|
documentID := c.Param("id")
|
|
|
|
if userID == "" || documentID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id and document_id are required"})
|
|
return
|
|
}
|
|
|
|
document, err := h.documentService.GetDocument(c.Request.Context(), userID, documentID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "document not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, document)
|
|
}
|
|
|
|
// DeleteDocument 删除文档接口
|
|
// @param c Gin 上下文
|
|
// @author sunct
|
|
func (h *DocumentHandler) DeleteDocument(c *gin.Context) {
|
|
userID := c.Query("user_id")
|
|
documentID := c.Param("id")
|
|
|
|
if userID == "" || documentID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id and document_id are required"})
|
|
return
|
|
}
|
|
|
|
err := h.documentService.DeleteDocument(c.Request.Context(), userID, documentID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "document deleted successfully"})
|
|
} |