125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
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)
|
|
}
|