first commit
This commit is contained in:
235
app/blog/handlers/category_handler.go
Normal file
235
app/blog/handlers/category_handler.go
Normal file
@@ -0,0 +1,235 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gobeyhan/app/blog/services"
|
||||
"gobeyhan/database/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CategoryHandler struct {
|
||||
service *services.CategoryService
|
||||
}
|
||||
|
||||
func NewCategoryHandler(service *services.CategoryService) *CategoryHandler {
|
||||
return &CategoryHandler{service: service}
|
||||
}
|
||||
|
||||
// GetAllCategories godoc
|
||||
// @Summary Get all active categories
|
||||
// @Description Get list of all active categories (public endpoint)
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {array} models.Category
|
||||
// @Router /api/v1/categories [get]
|
||||
func (h *CategoryHandler) GetAllCategories(c *gin.Context) {
|
||||
categories, err := h.service.GetAllCategories(true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": categories})
|
||||
}
|
||||
|
||||
// GetCategoryBySlug godoc
|
||||
// @Summary Get category by slug
|
||||
// @Description Get a single category by its slug (public endpoint)
|
||||
// @Tags categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param slug path string true "Category Slug"
|
||||
// @Success 200 {object} models.Category
|
||||
// @Router /api/v1/categories/{slug} [get]
|
||||
func (h *CategoryHandler) GetCategoryBySlug(c *gin.Context) {
|
||||
slug := c.Param("slug")
|
||||
|
||||
category, err := h.service.GetCategoryBySlug(slug)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if category == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Category not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": category})
|
||||
}
|
||||
|
||||
// AdminGetAllCategories godoc
|
||||
// @Summary Get all categories (Admin)
|
||||
// @Description Get list of all categories including inactive ones
|
||||
// @Tags admin,categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {array} models.Category
|
||||
// @Router /api/v1/admin/categories [get]
|
||||
func (h *CategoryHandler) AdminGetAllCategories(c *gin.Context) {
|
||||
categories, err := h.service.GetAllCategories(false)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": categories})
|
||||
}
|
||||
|
||||
// GetCategoryByID godoc
|
||||
// @Summary Get category by ID (Admin)
|
||||
// @Description Get a single category by ID
|
||||
// @Tags admin,categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Category ID"
|
||||
// @Success 200 {object} models.Category
|
||||
// @Router /api/v1/admin/categories/{id} [get]
|
||||
func (h *CategoryHandler) GetCategoryByID(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
|
||||
return
|
||||
}
|
||||
|
||||
category, err := h.service.GetCategoryByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if category == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Category not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": category})
|
||||
}
|
||||
|
||||
// CreateCategory godoc
|
||||
// @Summary Create a new category (Admin)
|
||||
// @Description Create a new category
|
||||
// @Tags admin,categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param category body models.Category true "Category object"
|
||||
// @Success 201 {object} models.Category
|
||||
// @Router /api/v1/admin/categories [post]
|
||||
func (h *CategoryHandler) CreateCategory(c *gin.Context) {
|
||||
var input struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Keywords string `json:"keywords"`
|
||||
Desc string `json:"description"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
Order *int `json:"order"`
|
||||
Slug string `json:"slug"`
|
||||
ParentID *uint64 `json:"parent_id"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
category := &models.Category{
|
||||
Title: input.Title,
|
||||
Keywords: input.Keywords,
|
||||
Desc: input.Desc,
|
||||
Slug: input.Slug,
|
||||
ParentID: input.ParentID,
|
||||
Image: input.Image,
|
||||
}
|
||||
|
||||
if input.IsActive != nil {
|
||||
category.IsActive = *input.IsActive
|
||||
} else {
|
||||
category.IsActive = true
|
||||
}
|
||||
|
||||
if input.Order != nil {
|
||||
category.Order = *input.Order
|
||||
} else {
|
||||
category.Order = 1
|
||||
}
|
||||
|
||||
if err := h.service.CreateCategory(category); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"data": category})
|
||||
}
|
||||
|
||||
// UpdateCategory godoc
|
||||
// @Summary Update a category (Admin)
|
||||
// @Description Update an existing category
|
||||
// @Tags admin,categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Category ID"
|
||||
// @Param category body models.Category true "Category object"
|
||||
// @Success 200 {object} models.Category
|
||||
// @Router /api/v1/admin/categories/{id} [put]
|
||||
func (h *CategoryHandler) UpdateCategory(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var input map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.UpdateCategory(id, input); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch updated category
|
||||
category, err := h.service.GetCategoryByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": category})
|
||||
}
|
||||
|
||||
// DeleteCategory godoc
|
||||
// @Summary Delete a category (Admin)
|
||||
// @Description Delete a category by ID
|
||||
// @Tags admin,categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Category ID"
|
||||
// @Success 200 {object} map[string]string
|
||||
// @Router /api/v1/admin/categories/{id} [delete]
|
||||
func (h *CategoryHandler) DeleteCategory(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.DeleteCategory(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Category deleted successfully"})
|
||||
}
|
||||
Reference in New Issue
Block a user