2024-03-05 19:15:53 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-28 18:39:43 +00:00
|
|
|
"fmt"
|
2024-03-31 15:19:45 +00:00
|
|
|
"github.com/rs/zerolog"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/app/database"
|
|
|
|
|
"go-humas-be/app/database/entity"
|
|
|
|
|
"go-humas-be/app/module/articles/request"
|
2025-02-15 01:56:13 +00:00
|
|
|
"go-humas-be/app/module/articles/response"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/utils/paginator"
|
2024-03-31 15:19:45 +00:00
|
|
|
"strings"
|
2025-02-15 01:56:13 +00:00
|
|
|
"time"
|
2024-03-05 19:15:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type articlesRepository struct {
|
2024-03-31 15:19:45 +00:00
|
|
|
DB *database.Database
|
|
|
|
|
Log zerolog.Logger
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArticlesRepository define interface of IArticlesRepository
|
|
|
|
|
type ArticlesRepository interface {
|
|
|
|
|
GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error)
|
|
|
|
|
FindOne(id uint) (articles *entity.Articles, err error)
|
2024-05-05 09:24:49 +00:00
|
|
|
FindByFilename(thumbnailName string) (articleReturn *entity.Articles, err error)
|
|
|
|
|
Create(articles *entity.Articles) (articleReturn *entity.Articles, err error)
|
2024-03-05 19:15:53 +00:00
|
|
|
Update(id uint, articles *entity.Articles) (err error)
|
|
|
|
|
Delete(id uint) (err error)
|
2025-02-15 01:56:13 +00:00
|
|
|
SummaryStats(userID uint) (articleSummaryStats *response.ArticleSummaryStats, err error)
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-31 15:19:45 +00:00
|
|
|
func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRepository {
|
2024-03-05 19:15:53 +00:00
|
|
|
return &articlesRepository{
|
2024-03-31 15:19:45 +00:00
|
|
|
DB: db,
|
|
|
|
|
Log: log,
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// implement interface of IArticlesRepository
|
|
|
|
|
func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) {
|
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
|
|
query := _i.DB.DB.Model(&entity.Articles{})
|
2025-02-14 10:23:07 +00:00
|
|
|
if req.CategoryId != nil {
|
|
|
|
|
query = query.Joins("JOIN article_category_details acd ON acd.article_id = articles.id").
|
|
|
|
|
Where("acd.category_id = ?", req.CategoryId)
|
|
|
|
|
}
|
|
|
|
|
query = query.Where("articles.is_active = ?", true)
|
2024-03-31 15:19:45 +00:00
|
|
|
|
|
|
|
|
if req.Title != nil && *req.Title != "" {
|
|
|
|
|
title := strings.ToLower(*req.Title)
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("LOWER(articles.title) LIKE ?", "%"+strings.ToLower(title)+"%")
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
if req.Description != nil && *req.Description != "" {
|
|
|
|
|
description := strings.ToLower(*req.Description)
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("LOWER(articles.description) LIKE ?", "%"+strings.ToLower(description)+"%")
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
if req.Tags != nil && *req.Tags != "" {
|
|
|
|
|
tags := strings.ToLower(*req.Tags)
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("LOWER(articles.tags) LIKE ?", "%"+strings.ToLower(tags)+"%")
|
2024-05-05 09:24:49 +00:00
|
|
|
}
|
2024-03-31 15:19:45 +00:00
|
|
|
if req.TypeId != nil {
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("articles.type_id = ?", req.TypeId)
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
|
|
|
|
if req.IsPublish != nil {
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("articles.is_publish = ?", req.IsPublish)
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
2025-02-06 21:14:25 +00:00
|
|
|
if req.IsDraft != nil {
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("articles.is_draft = ?", req.IsDraft)
|
2025-02-06 21:14:25 +00:00
|
|
|
}
|
2024-03-31 15:19:45 +00:00
|
|
|
if req.StatusId != nil {
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("articles.status_id = ?", req.StatusId)
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
2024-04-16 02:08:00 +00:00
|
|
|
if req.CreatedById != nil {
|
2025-02-14 10:23:07 +00:00
|
|
|
query = query.Where("articles.created_by_id = ?", req.CreatedById)
|
2024-04-16 02:08:00 +00:00
|
|
|
}
|
2024-03-05 19:15:53 +00:00
|
|
|
query.Count(&count)
|
|
|
|
|
|
2024-04-28 18:39:43 +00:00
|
|
|
if req.Pagination.SortBy != "" {
|
|
|
|
|
direction := "ASC"
|
|
|
|
|
if req.Pagination.Sort == "desc" {
|
|
|
|
|
direction = "DESC"
|
|
|
|
|
}
|
|
|
|
|
query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
|
2025-02-13 15:32:59 +00:00
|
|
|
} else {
|
|
|
|
|
direction := "DESC"
|
2025-02-14 10:23:07 +00:00
|
|
|
sortBy := "articles.created_at"
|
2025-02-13 15:32:59 +00:00
|
|
|
query.Order(fmt.Sprintf("%s %s", sortBy, direction))
|
2024-04-28 18:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
req.Pagination.Count = count
|
|
|
|
|
req.Pagination = paginator.Paging(req.Pagination)
|
|
|
|
|
|
|
|
|
|
err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&articless).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paging = *req.Pagination
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) FindOne(id uint) (articles *entity.Articles, err error) {
|
|
|
|
|
if err := _i.DB.DB.First(&articles, id).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return articles, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
func (_i *articlesRepository) FindByFilename(thumbnailName string) (articles *entity.Articles, err error) {
|
|
|
|
|
|
|
|
|
|
if err := _i.DB.DB.Where("thumbnail_name = ?", thumbnailName).First(&articles).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return articles, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) {
|
|
|
|
|
result := _i.DB.DB.Create(articles)
|
|
|
|
|
return articles, result.Error
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Update(id uint, articles *entity.Articles) (err error) {
|
|
|
|
|
return _i.DB.DB.Model(&entity.Articles{}).
|
|
|
|
|
Where(&entity.Articles{ID: id}).
|
|
|
|
|
Updates(articles).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Delete(id uint) error {
|
|
|
|
|
return _i.DB.DB.Delete(&entity.Articles{}, id).Error
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|
2025-02-15 01:56:13 +00:00
|
|
|
|
|
|
|
|
func (_i *articlesRepository) SummaryStats(userID uint) (articleSummaryStats *response.ArticleSummaryStats, err error) {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
startOfDay := now.Truncate(24 * time.Hour)
|
|
|
|
|
startOfWeek := now.AddDate(0, 0, -int(now.Weekday())+1).Truncate(24 * time.Hour)
|
|
|
|
|
|
|
|
|
|
// Query
|
|
|
|
|
err = _i.DB.DB.Model(&entity.Articles{}).
|
|
|
|
|
Select(
|
|
|
|
|
"COUNT(*) AS total_all, "+
|
|
|
|
|
"COALESCE(SUM(view_count), 0) AS total_views, "+
|
|
|
|
|
"COALESCE(SUM(share_count), 0) AS total_shares, "+
|
|
|
|
|
"COALESCE(SUM(comment_count), 0) AS total_comments, "+
|
|
|
|
|
"COUNT(CASE WHEN created_at >= ? THEN 1 END) AS total_today, "+
|
|
|
|
|
"COUNT(CASE WHEN created_at >= ? THEN 1 END) AS total_this_week",
|
|
|
|
|
startOfDay, startOfWeek,
|
|
|
|
|
).
|
|
|
|
|
Where("created_by_id = ?", userID).
|
|
|
|
|
Scan(&articleSummaryStats).Error
|
|
|
|
|
|
|
|
|
|
return articleSummaryStats, err
|
|
|
|
|
}
|