fix: improvement logs and record news access

This commit is contained in:
ericprd
2025-03-20 11:11:08 +08:00
parent 290d7f6701
commit d8f6968f64
13 changed files with 81 additions and 25 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ func (a *accessor) GetAll() ([]adsdomain.AdsResponse, error) {
var ads []adsdomain.AdsResponse
if err := a.db.Table("ads").
Select("ads.*, COUNT(log_ads.content_id) as clicked").
Joins("LEFT JOIN log_ads ON log_ads.content_id = ads.id").
Select("ads.*, COUNT(content_logs.content_id) as clicked").
Joins("LEFT JOIN content_logs ON content_logs.content_id = ads.id").
Group("ads.id").
Scan(&ads).Error; err != nil {
return ads, fmt.Errorf("failed to get all ads: %v", err)
+3 -2
View File
@@ -7,10 +7,11 @@ import (
"github.com/google/uuid"
)
func (a *accessor) CreateLogAds(spec logsdomain.LogsSpec) error {
newSpec := database.LogAds{
func (a *accessor) CreateLog(spec logsdomain.LogsSpec) error {
newSpec := database.ContentLog{
ID: uuid.NewString(),
ContentID: spec.ContentID,
Category: spec.Category,
}
if spec.UserID != nil {
+1 -1
View File
@@ -11,7 +11,7 @@ type accessor struct {
}
type Log interface {
CreateLogAds(logsdomain.LogsSpec) error
CreateLog(logsdomain.LogsSpec) error
GetAllLogAds(string) ([]adsdomain.Ads, error)
}
+7 -1
View File
@@ -7,7 +7,8 @@ func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, erro
query := a.db.
Preload("Tags").
Preload("Categories").
Preload("Author")
Preload("Author").
Joins("LEFT JOIN content_logs ON content_logs.content_id = news.id")
if len(filter.Category) > 0 {
query = query.Joins("JOIN news_categories nc ON nc.news_id = news.id").
@@ -19,6 +20,11 @@ func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, erro
Where("nt.tag_id IN (?)", filter.Tags)
}
query.
Select("news.*, COUNT(content_logs.content_id) as clicked").
Group("news.id").
Order("news.created_at DESC")
if err := query.
Find(&news).Error; err != nil {
return nil, err