Generete Module Handler
This commit is contained in:
57
internal/handlers/component/example.go
Normal file
57
internal/handlers/component/example.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"api-service/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ExampleHandler handles example GET and POST services
|
||||
type ExampleHandler struct{}
|
||||
|
||||
// NewExampleHandler creates a new ExampleHandler
|
||||
func NewExampleHandler() *ExampleHandler {
|
||||
return &ExampleHandler{}
|
||||
}
|
||||
|
||||
// GetExample godoc
|
||||
// @Summary Example GET service
|
||||
// @Description Returns a simple message for GET request
|
||||
// @Tags example
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.ExampleGetResponse "Example GET response"
|
||||
// @Router /api/v1/example [get]
|
||||
func (h *ExampleHandler) GetExample(c *gin.Context) {
|
||||
response := models.ExampleGetResponse{
|
||||
Message: "This is a GET example response",
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// PostExample godoc
|
||||
// @Summary Example POST service
|
||||
// @Description Accepts a JSON payload and returns a response with an ID
|
||||
// @Tags example
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.ExamplePostRequest true "Example POST request"
|
||||
// @Success 200 {object} models.ExamplePostResponse "Example POST response"
|
||||
// @Failure 400 {object} models.ErrorResponse "Bad request"
|
||||
// @Router /api/v1/example [post]
|
||||
func (h *ExampleHandler) PostExample(c *gin.Context) {
|
||||
var req models.ExamplePostRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response := models.ExamplePostResponse{
|
||||
ID: uuid.NewString(),
|
||||
Message: "Received data for " + req.Name,
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
Reference in New Issue
Block a user