first commit
This commit is contained in:
74
api/handlers/post_category_view_handler.go
Normal file
74
api/handlers/post_category_view_handler.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gauth-central/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PostCategoryViewHandler struct {
|
||||
viewService *services.PostCategoryViewService
|
||||
}
|
||||
|
||||
func NewPostCategoryViewHandler(viewService *services.PostCategoryViewService) *PostCategoryViewHandler {
|
||||
return &PostCategoryViewHandler{viewService: viewService}
|
||||
}
|
||||
|
||||
// TrackPostCategoryView godoc
|
||||
// @Summary Track post category view
|
||||
// @Description Record a post category view (daily per IP)
|
||||
// @Tags post-category-views
|
||||
// @Produce json
|
||||
// @Param id path string true "Category ID"
|
||||
// @Success 200 {object} models.PostCategoryView
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /post-categories/{id}/views [post]
|
||||
// TrackPostCategoryView records a category view.
|
||||
func (h *PostCategoryViewHandler) TrackPostCategoryView(c *gin.Context) {
|
||||
categoryID := c.Param("id")
|
||||
ipAddress := strings.TrimSpace(c.ClientIP())
|
||||
userAgent := strings.TrimSpace(c.GetHeader("User-Agent"))
|
||||
|
||||
view, err := h.viewService.TrackView(categoryID, ipAddress, userAgent)
|
||||
if err != nil {
|
||||
status := http.StatusInternalServerError
|
||||
if err.Error() == "invalid category_id" {
|
||||
status = http.StatusBadRequest
|
||||
}
|
||||
c.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, view)
|
||||
}
|
||||
|
||||
// AdminGetPostCategoryViews godoc
|
||||
// @Summary Get post category views (Admin)
|
||||
// @Description Retrieve views for a category
|
||||
// @Tags admin
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce json
|
||||
// @Param category_id query string true "Category ID"
|
||||
// @Success 200 {array} models.PostCategoryView
|
||||
// @Failure 400 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /admin/post-category-views [get]
|
||||
// AdminGetPostCategoryViews returns views for a category.
|
||||
func (h *PostCategoryViewHandler) AdminGetPostCategoryViews(c *gin.Context) {
|
||||
categoryID := strings.TrimSpace(c.Query("category_id"))
|
||||
if categoryID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "category_id is required"})
|
||||
return
|
||||
}
|
||||
|
||||
views, err := h.viewService.GetViewsByCategory(categoryID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, views)
|
||||
}
|
||||
Reference in New Issue
Block a user