package repository import ( "fmt" "github.com/rs/zerolog" "go-humas-be/app/database" "go-humas-be/app/database/entity" "go-humas-be/app/module/articles/request" "go-humas-be/app/module/articles/response" "go-humas-be/utils/paginator" "strings" "time" ) type articlesRepository struct { DB *database.Database Log zerolog.Logger } // 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) FindByFilename(thumbnailName string) (articleReturn *entity.Articles, err error) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) Update(id uint, articles *entity.Articles) (err error) Delete(id uint) (err error) SummaryStats(userID uint) (articleSummaryStats *response.ArticleSummaryStats, err error) } func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRepository { return &articlesRepository{ DB: db, Log: log, } } // 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{}) 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) if req.Title != nil && *req.Title != "" { title := strings.ToLower(*req.Title) query = query.Where("LOWER(articles.title) LIKE ?", "%"+strings.ToLower(title)+"%") } if req.Description != nil && *req.Description != "" { description := strings.ToLower(*req.Description) query = query.Where("LOWER(articles.description) LIKE ?", "%"+strings.ToLower(description)+"%") } if req.Tags != nil && *req.Tags != "" { tags := strings.ToLower(*req.Tags) query = query.Where("LOWER(articles.tags) LIKE ?", "%"+strings.ToLower(tags)+"%") } if req.TypeId != nil { query = query.Where("articles.type_id = ?", req.TypeId) } if req.IsPublish != nil { query = query.Where("articles.is_publish = ?", req.IsPublish) } if req.IsDraft != nil { query = query.Where("articles.is_draft = ?", req.IsDraft) } if req.StatusId != nil { query = query.Where("articles.status_id = ?", req.StatusId) } if req.CreatedById != nil { query = query.Where("articles.created_by_id = ?", req.CreatedById) } query.Count(&count) if req.Pagination.SortBy != "" { direction := "ASC" if req.Pagination.Sort == "desc" { direction = "DESC" } query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction)) } else { direction := "DESC" sortBy := "articles.created_at" query.Order(fmt.Sprintf("%s %s", sortBy, direction)) } 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 } 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 } 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 } 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 }