Files
gobeyhan/app/blog/handlers/category_view_handler.go
Beyhan Oğur f34e54c5a5 first commit
2026-04-26 21:43:40 +03:00

112 lines
3.0 KiB
Go

package handlers
import (
"gobeyhan/app/blog/services"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type CategoryViewHandler struct {
service *services.CategoryViewService
}
func NewCategoryViewHandler(service *services.CategoryViewService) *CategoryViewHandler {
return &CategoryViewHandler{service: service}
}
// TrackCategoryView godoc
// @Summary Track a category view
// @Description Record a view event for a category (public endpoint)
// @Tags category-views
// @Accept json
// @Produce json
// @Param id path int true "Category ID"
// @Success 200 {object} map[string]string
// @Router /api/v1/categories/{id}/view [post]
func (h *CategoryViewHandler) TrackCategoryView(c *gin.Context) {
idStr := c.Param("id")
categoryID, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
return
}
ipAddress := c.ClientIP()
userAgent := c.Request.UserAgent()
if err := h.service.TrackCategoryView(categoryID, ipAddress, userAgent); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "View tracked successfully"})
}
// AdminGetAllCategoryViews godoc
// @Summary Get all category views (Admin)
// @Description Get paginated list of all category views
// @Tags admin,category-views
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/admin/category-views [get]
func (h *CategoryViewHandler) AdminGetAllCategoryViews(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
if page < 1 {
page = 1
}
if limit < 1 || limit > 100 {
limit = 10
}
views, total, err := h.service.GetAllCategoryViews(page, limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"data": views,
"total": total,
"page": page,
"limit": limit,
})
}
// GetCategoryViewStats godoc
// @Summary Get view stats for a category (Admin)
// @Description Get view count and details for a specific category
// @Tags admin,category-views
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "Category ID"
// @Success 200 {object} map[string]interface{}
// @Router /api/v1/admin/categories/{id}/views [get]
func (h *CategoryViewHandler) GetCategoryViewStats(c *gin.Context) {
idStr := c.Param("id")
categoryID, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid category ID"})
return
}
count, err := h.service.GetCategoryViewCount(categoryID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"category_id": categoryID,
"view_count": count,
})
}