first commit
This commit is contained in:
67
app/blog/services/category_view_service.go
Normal file
67
app/blog/services/category_view_service.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"gobeyhan/database"
|
||||
"gobeyhan/database/models"
|
||||
)
|
||||
|
||||
type CategoryViewService struct{}
|
||||
|
||||
func NewCategoryViewService() *CategoryViewService {
|
||||
return &CategoryViewService{}
|
||||
}
|
||||
|
||||
// TrackCategoryView records a category view
|
||||
func (s *CategoryViewService) TrackCategoryView(categoryID uint64, ipAddress, userAgent string) error {
|
||||
view := &models.CategoryView{
|
||||
CategoryID: categoryID,
|
||||
IPAddress: ipAddress,
|
||||
UserAgent: userAgent,
|
||||
}
|
||||
return database.DB.Create(view).Error
|
||||
}
|
||||
|
||||
// GetCategoryViewCount gets total view count for a category
|
||||
func (s *CategoryViewService) GetCategoryViewCount(categoryID uint64) (int64, error) {
|
||||
var count int64
|
||||
err := database.DB.Model(&models.CategoryView{}).
|
||||
Where("category_id = ?", categoryID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetAllCategoryViews gets all views for a category with pagination
|
||||
func (s *CategoryViewService) GetAllCategoryViews(page, limit int) ([]models.CategoryView, int64, error) {
|
||||
var views []models.CategoryView
|
||||
var total int64
|
||||
|
||||
query := database.DB.Preload("Category")
|
||||
|
||||
query.Model(&models.CategoryView{}).Count(&total)
|
||||
|
||||
err := query.
|
||||
Offset((page - 1) * limit).
|
||||
Limit(limit).
|
||||
Order("created_at DESC").
|
||||
Find(&views).Error
|
||||
|
||||
return views, total, err
|
||||
}
|
||||
|
||||
// GetViewsByCategory gets views for a specific category
|
||||
func (s *CategoryViewService) GetViewsByCategory(categoryID uint64, page, limit int) ([]models.CategoryView, int64, error) {
|
||||
var views []models.CategoryView
|
||||
var total int64
|
||||
|
||||
query := database.DB.Where("category_id = ?", categoryID)
|
||||
|
||||
query.Model(&models.CategoryView{}).Count(&total)
|
||||
|
||||
err := query.
|
||||
Offset((page - 1) * limit).
|
||||
Limit(limit).
|
||||
Order("created_at DESC").
|
||||
Find(&views).Error
|
||||
|
||||
return views, total, err
|
||||
}
|
||||
Reference in New Issue
Block a user