178 lines
4.8 KiB
Go
178 lines
4.8 KiB
Go
![]() |
package perspective
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
// PerspectiveHandler handles HTTP requests for perspectives
|
||
|
type PerspectiveHandler struct {
|
||
|
service *PerspectiveService
|
||
|
}
|
||
|
|
||
|
// NewPerspectiveHandler creates a new perspective handler
|
||
|
func NewPerspectiveHandler(service *PerspectiveService) *PerspectiveHandler {
|
||
|
return &PerspectiveHandler{
|
||
|
service: service,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// RegisterRoutes registers the perspective routes
|
||
|
func (h *PerspectiveHandler) RegisterRoutes(router *gin.Engine) {
|
||
|
perspective := router.Group("/perspective")
|
||
|
{
|
||
|
perspective.OPTIONS("/submit", h.handleCORS)
|
||
|
perspective.POST("/submit", h.HandleSubmitPerspective)
|
||
|
perspective.GET("/get/:hash", h.HandleGetPerspective)
|
||
|
perspective.GET("/issue/:issueId", h.HandleGetPerspectivesByIssue)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// SubmitPerspectiveRequest represents a request to submit a perspective
|
||
|
type SubmitPerspectiveRequest struct {
|
||
|
IssueID string `json:"issueId" binding:"required"`
|
||
|
Text string `json:"text" binding:"required"`
|
||
|
FileURL string `json:"fileUrl,omitempty"`
|
||
|
UserAddress string `json:"userAddress" binding:"required"`
|
||
|
IsVerified bool `json:"isVerified"`
|
||
|
CaptchaToken string `json:"captchaToken,omitempty"`
|
||
|
}
|
||
|
|
||
|
// PerspectiveResponse represents a response for perspective operations
|
||
|
type PerspectiveResponse struct {
|
||
|
Success bool `json:"success"`
|
||
|
Message string `json:"message"`
|
||
|
IPFSHash string `json:"ipfsHash,omitempty"`
|
||
|
IPFSUrl string `json:"ipfsUrl,omitempty"`
|
||
|
Data interface{} `json:"data,omitempty"`
|
||
|
}
|
||
|
|
||
|
// handleCORS handles CORS preflight requests
|
||
|
func (h *PerspectiveHandler) handleCORS(c *gin.Context) {
|
||
|
c.Header("Access-Control-Allow-Origin", "*")
|
||
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
||
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||
|
c.Status(http.StatusOK)
|
||
|
}
|
||
|
|
||
|
// HandleSubmitPerspective handles perspective submission requests
|
||
|
func (h *PerspectiveHandler) HandleSubmitPerspective(c *gin.Context) {
|
||
|
c.Header("Access-Control-Allow-Origin", "*")
|
||
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
||
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||
|
|
||
|
var request SubmitPerspectiveRequest
|
||
|
if err := c.ShouldBindJSON(&request); err != nil {
|
||
|
c.JSON(http.StatusBadRequest, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: "Invalid request format",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ipfsHash, err := h.service.SubmitPerspective(
|
||
|
c.Request.Context(),
|
||
|
request.IssueID,
|
||
|
request.Text,
|
||
|
request.FileURL,
|
||
|
request.UserAddress,
|
||
|
request.IsVerified,
|
||
|
request.CaptchaToken,
|
||
|
)
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusBadRequest, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Generate an IPFS gateway URL
|
||
|
ipfsUrl := h.service.ipfsService.GetIPFSGatewayURL(ipfsHash)
|
||
|
|
||
|
c.JSON(http.StatusOK, PerspectiveResponse{
|
||
|
Success: true,
|
||
|
Message: "Perspective submitted successfully",
|
||
|
IPFSHash: ipfsHash,
|
||
|
IPFSUrl: ipfsUrl,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// HandleGetPerspective handles perspective retrieval requests
|
||
|
func (h *PerspectiveHandler) HandleGetPerspective(c *gin.Context) {
|
||
|
c.Header("Access-Control-Allow-Origin", "*")
|
||
|
|
||
|
ipfsHash := c.Param("hash")
|
||
|
if ipfsHash == "" {
|
||
|
c.JSON(http.StatusBadRequest, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: "Missing IPFS hash",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
perspective, err := h.service.GetPerspective(ipfsHash)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusNotFound, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ipfsUrl := h.service.ipfsService.GetIPFSGatewayURL(ipfsHash)
|
||
|
|
||
|
c.JSON(http.StatusOK, PerspectiveResponse{
|
||
|
Success: true,
|
||
|
Message: "Perspective retrieved successfully",
|
||
|
IPFSHash: ipfsHash,
|
||
|
IPFSUrl: ipfsUrl,
|
||
|
Data: perspective,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// HandleGetPerspectivesByIssue handles retrieval of all perspectives for an issue
|
||
|
func (h *PerspectiveHandler) HandleGetPerspectivesByIssue(c *gin.Context) {
|
||
|
c.Header("Access-Control-Allow-Origin", "*")
|
||
|
|
||
|
issueID := c.Param("issueId")
|
||
|
if issueID == "" {
|
||
|
c.JSON(http.StatusBadRequest, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: "Missing issue ID",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
perspectives, err := h.service.GetPerspectivesByIssue(issueID)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, PerspectiveResponse{
|
||
|
Success: false,
|
||
|
Message: "Failed to retrieve perspectives",
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Enhance perspectives with IPFS URLs
|
||
|
type EnhancedPerspective struct {
|
||
|
Perspective
|
||
|
IPFSUrl string `json:"ipfsUrl"`
|
||
|
}
|
||
|
|
||
|
enhancedPerspectives := make([]EnhancedPerspective, len(perspectives))
|
||
|
for i, p := range perspectives {
|
||
|
enhancedPerspectives[i] = EnhancedPerspective{
|
||
|
Perspective: p,
|
||
|
IPFSUrl: h.service.ipfsService.GetIPFSGatewayURL(p.IPFSHash),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, PerspectiveResponse{
|
||
|
Success: true,
|
||
|
Message: "Perspectives retrieved successfully",
|
||
|
Data: enhancedPerspectives,
|
||
|
})
|
||
|
}
|