initial commit
This commit is contained in:
51
internal/api/handler.go
Normal file
51
internal/api/handler.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/paramah/ai_devs4/s01e03/internal/usecase"
|
||||
)
|
||||
|
||||
// Handler handles HTTP requests
|
||||
type Handler struct {
|
||||
conversationUC *usecase.ConversationUseCase
|
||||
}
|
||||
|
||||
// NewHandler creates a new API handler
|
||||
func NewHandler(conversationUC *usecase.ConversationUseCase) *Handler {
|
||||
return &Handler{
|
||||
conversationUC: conversationUC,
|
||||
}
|
||||
}
|
||||
|
||||
// ShadowRequest represents the request for /shadow endpoint
|
||||
type ShadowRequest struct {
|
||||
SessionID string `json:"sessionID" binding:"required"`
|
||||
Msg string `json:"msg" binding:"required"`
|
||||
}
|
||||
|
||||
// ShadowResponse represents the response for /shadow endpoint
|
||||
type ShadowResponse struct {
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
// Shadow handles the /shadow endpoint
|
||||
func (h *Handler) Shadow(c *gin.Context) {
|
||||
var req ShadowRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Process message
|
||||
response, err := h.conversationUC.ProcessMessage(c.Request.Context(), req.SessionID, req.Msg)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ShadowResponse{
|
||||
Msg: response,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user