package blockchain import ( "net/http" "github.com/gin-gonic/gin" ) // Handler handles HTTP requests for blockchain operations type Handler struct { service *BlockchainService } // NewHandler creates a new blockchain handler func NewHandler(service *BlockchainService) *Handler { return &Handler{ service: service, } } // RegisterRoutes registers the blockchain routes func (h *Handler) RegisterRoutes(router *gin.Engine) { blockchain := router.Group("/blockchain") { blockchain.POST("/submit-hash", h.HandleSubmitHash) blockchain.POST("/propose-insight", h.HandleProposeInsight) blockchain.POST("/vote", h.HandleVote) } } // SubmitHashRequest represents a request to submit a hash type SubmitHashRequest struct { Hash string `json:"hash" binding:"required"` } // ProposeInsightRequest represents a request to propose an insight type ProposeInsightRequest struct { Insight string `json:"insight" binding:"required"` } // VoteRequest represents a request to vote on an insight type VoteRequest struct { InsightID string `json:"insightId" binding:"required"` Vote bool `json:"vote"` } // Response represents a generic response type Response struct { Success bool `json:"success"` Message string `json:"message"` Data any `json:"data,omitempty"` } // HandleSubmitHash handles requests to submit a hash func (h *Handler) HandleSubmitHash(c *gin.Context) { var req SubmitHashRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, Response{ Success: false, Message: "Invalid request format", }) return } txHash, err := h.service.SubmitPerspective(req.Hash) if err != nil { c.JSON(http.StatusInternalServerError, Response{ Success: false, Message: err.Error(), }) return } c.JSON(http.StatusOK, Response{ Success: true, Message: "Hash submitted successfully", Data: map[string]string{ "transactionHash": txHash, }, }) } // HandleProposeInsight handles requests to propose an insight func (h *Handler) HandleProposeInsight(c *gin.Context) { var req ProposeInsightRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, Response{ Success: false, Message: "Invalid request format", }) return } txHash, err := h.service.ProposeInsight(req.Insight) if err != nil { c.JSON(http.StatusInternalServerError, Response{ Success: false, Message: err.Error(), }) return } c.JSON(http.StatusOK, Response{ Success: true, Message: "Insight proposed successfully", Data: map[string]string{ "transactionHash": txHash, }, }) } // HandleVote handles requests to vote on an insight func (h *Handler) HandleVote(c *gin.Context) { var req VoteRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, Response{ Success: false, Message: "Invalid request format", }) return } txHash, err := h.service.VoteOnInsight(req.InsightID, req.Vote) if err != nil { c.JSON(http.StatusInternalServerError, Response{ Success: false, Message: err.Error(), }) return } c.JSON(http.StatusOK, Response{ Success: true, Message: "Vote submitted successfully", Data: map[string]string{ "transactionHash": txHash, }, }) }