68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"tencent_ocr/pkg/service"
|
|
)
|
|
|
|
type RateHandler struct {
|
|
geminiService *service.GeminiService
|
|
apiKey string
|
|
}
|
|
|
|
type RateRequest struct {
|
|
Content string `json:"content" binding:"required"`
|
|
Criteria string `json:"criteria"`
|
|
WritingRequirement string `json:"writing_requirement"`
|
|
APIKey string `json:"apikey" binding:"required"`
|
|
}
|
|
|
|
type RateResponse struct {
|
|
Rate int `json:"rate"`
|
|
Summary string `json:"summary"`
|
|
DetailedReview string `json:"detailed_review"`
|
|
Success bool `json:"success"`
|
|
}
|
|
|
|
func NewRateHandler(geminiService *service.GeminiService, apiKey string) *RateHandler {
|
|
return &RateHandler{
|
|
geminiService: geminiService,
|
|
apiKey: apiKey,
|
|
}
|
|
}
|
|
|
|
func (h *RateHandler) HandleRate(c *gin.Context) {
|
|
var req RateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, RateResponse{
|
|
Success: false,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Validate API key
|
|
if req.APIKey != h.apiKey {
|
|
c.JSON(http.StatusUnauthorized, RateResponse{
|
|
Success: false,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Process with Gemini
|
|
result, err := h.geminiService.ProcessText(c.Request.Context(), req.Content, req.Criteria, req.WritingRequirement)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, RateResponse{
|
|
Success: false,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, RateResponse{
|
|
Rate: result.Rate,
|
|
Summary: result.Summary,
|
|
DetailedReview: result.DetailedReview,
|
|
Success: true,
|
|
})
|
|
} |