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)
|
||||
}
|
||||
55
internal/handlers/component/health.go
Normal file
55
internal/handlers/component/health.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"api-service/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HealthHandler handles health check endpoints
|
||||
type HealthHandler struct{}
|
||||
|
||||
// NewHealthHandler creates a new HealthHandler
|
||||
func NewHealthHandler() *HealthHandler {
|
||||
return &HealthHandler{}
|
||||
}
|
||||
|
||||
// GetHealth godoc
|
||||
// @Summary Health check endpoint
|
||||
// @Description Returns the health status of the API service
|
||||
// @Tags health
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.HealthResponse "Health status"
|
||||
// @Failure 500 {object} models.ErrorResponse "Internal server error"
|
||||
// @Router /health [get]
|
||||
func (h *HealthHandler) GetHealth(c *gin.Context) {
|
||||
health := models.HealthResponse{
|
||||
Status: "healthy",
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
Details: map[string]string{
|
||||
"service": "api-service",
|
||||
"version": "1.0.0",
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, health)
|
||||
}
|
||||
|
||||
// HelloWorld godoc
|
||||
// @Summary Hello World endpoint
|
||||
// @Description Returns a hello world message
|
||||
// @Tags root
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.HelloWorldResponse "Hello world message"
|
||||
// @Router / [get]
|
||||
func (h *HealthHandler) HelloWorld(c *gin.Context) {
|
||||
response := models.HelloWorldResponse{
|
||||
Message: "Hello World",
|
||||
Version: "1.0.0",
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
124
internal/handlers/component/product.go
Normal file
124
internal/handlers/component/product.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"api-service/internal/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ProductHandler handles product services
|
||||
type ProductHandler struct{}
|
||||
|
||||
// NewProductHandler creates a new ProductHandler
|
||||
func NewProductHandler() *ProductHandler {
|
||||
return &ProductHandler{}
|
||||
}
|
||||
|
||||
// GetProduct godoc
|
||||
// @Summary Get product
|
||||
// @Description Returns a list of products
|
||||
// @Tags product
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.ProductGetResponse "Product GET response"
|
||||
// @Router /api/v1/products [get]
|
||||
func (h *ProductHandler) GetProduct(c *gin.Context) {
|
||||
response := models.ProductGetResponse{
|
||||
Message: "List of products",
|
||||
Data: []string{"Product 1", "Product 2"},
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetProductByID godoc
|
||||
// @Summary Get product by ID
|
||||
// @Description Returns a single product by ID
|
||||
// @Tags product
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Product ID"
|
||||
// @Success 200 {object} models.ProductGetByIDResponse "Product GET by ID response"
|
||||
// @Failure 404 {object} models.ErrorResponse "Product not found"
|
||||
// @Router /api/v1/products/{id} [get]
|
||||
func (h *ProductHandler) GetProductByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
response := models.ProductGetByIDResponse{
|
||||
ID: id,
|
||||
Message: "Product details",
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// CreateProduct godoc
|
||||
// @Summary Create product
|
||||
// @Description Creates a new product
|
||||
// @Tags product
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.ProductCreateRequest true "Product creation request"
|
||||
// @Success 201 {object} models.ProductCreateResponse "Product created successfully"
|
||||
// @Failure 400 {object} models.ErrorResponse "Bad request"
|
||||
// @Router /api/v1/products [post]
|
||||
func (h *ProductHandler) CreateProduct(c *gin.Context) {
|
||||
var req models.ProductCreateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response := models.ProductCreateResponse{
|
||||
ID: uuid.NewString(),
|
||||
Message: "Product created successfully",
|
||||
Data: req,
|
||||
}
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// UpdateProduct godoc
|
||||
// @Summary Update product
|
||||
// @Description Updates an existing product
|
||||
// @Tags product
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Product ID"
|
||||
// @Param request body models.ProductUpdateRequest true "Product update request"
|
||||
// @Success 200 {object} models.ProductUpdateResponse "Product updated successfully"
|
||||
// @Failure 400 {object} models.ErrorResponse "Bad request"
|
||||
// @Failure 404 {object} models.ErrorResponse "Product not found"
|
||||
// @Router /api/v1/products/{id} [put]
|
||||
func (h *ProductHandler) UpdateProduct(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var req models.ProductUpdateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response := models.ProductUpdateResponse{
|
||||
ID: id,
|
||||
Message: "Product updated successfully",
|
||||
Data: req,
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// DeleteProduct godoc
|
||||
// @Summary Delete product
|
||||
// @Description Deletes a product by ID
|
||||
// @Tags product
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Product ID"
|
||||
// @Success 200 {object} models.ProductDeleteResponse "Product deleted successfully"
|
||||
// @Failure 404 {object} models.ErrorResponse "Product not found"
|
||||
// @Router /api/v1/products/{id} [delete]
|
||||
func (h *ProductHandler) DeleteProduct(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
response := models.ProductDeleteResponse{
|
||||
ID: id,
|
||||
Message: "Product deleted successfully",
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
Reference in New Issue
Block a user