fix: add clicked count on ads

This commit is contained in:
ericprd 2025-03-17 22:35:22 +08:00
parent c0dcdb77fa
commit 7b529ba46e
5 changed files with 16 additions and 6 deletions

View File

@ -5,9 +5,14 @@ import (
adsdomain "legalgo-BE-go/internal/domain/ads"
)
func (a *accessor) GetAll() ([]adsdomain.Ads, error) {
var ads []adsdomain.Ads
if err := a.db.Find(&ads).Error; err != nil {
func (a *accessor) GetAll() ([]adsdomain.AdsResponse, error) {
var ads []adsdomain.AdsResponse
if err := a.db.Table("ads").
Select("ads.*, COUNT(log_ads.ads_id) as clicked").
Joins("LEFT JOIN log_ads ON log_ads.ads_id = ads.id").
Group("ads.id").
Scan(&ads).Error; err != nil {
return ads, fmt.Errorf("failed to get all ads: %v", err)
}

View File

@ -11,7 +11,7 @@ type accessor struct {
type Ads interface {
Create(adsdomain.Ads) error
GetAll() ([]adsdomain.Ads, error)
GetAll() ([]adsdomain.AdsResponse, error)
Update(adsdomain.Ads) error
Delete(string) error
}

View File

@ -14,6 +14,11 @@ type Ads struct {
UpdatedAt time.Time `json:"updated_at"`
}
type AdsResponse struct {
Ads
Clicked int64 `json:"clicked"`
}
type AdsReq struct {
Image string `json:"image" validate:"required"`
URL string `json:"url" validate:"required"`

View File

@ -2,6 +2,6 @@ package adssvc
import adsdomain "legalgo-BE-go/internal/domain/ads"
func (i *impl) GetAll() ([]adsdomain.Ads, error) {
func (i *impl) GetAll() ([]adsdomain.AdsResponse, error) {
return i.adsRepo.GetAll()
}

View File

@ -11,7 +11,7 @@ type impl struct {
type Ads interface {
Create(adsdomain.AdsReq) error
GetAll() ([]adsdomain.Ads, error)
GetAll() ([]adsdomain.AdsResponse, error)
Update(string, adsdomain.AdsReq) error
Delete(string) error
}