refactor: update GetAll method to return NewsResponse and rename clicked to views
This commit is contained in:
parent
09d9c4efb8
commit
49dc0e4a7b
@ -12,9 +12,9 @@ const (
|
||||
notActive is_active = "false"
|
||||
)
|
||||
|
||||
func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, error) {
|
||||
var news []newsdomain.News
|
||||
query := a.db.
|
||||
func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.NewsResponse, error) {
|
||||
var news []newsdomain.NewsResponse
|
||||
query := a.db.Table("news").
|
||||
Preload("Tags").
|
||||
Preload("Categories").
|
||||
Preload("Author").
|
||||
@ -35,7 +35,7 @@ func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, erro
|
||||
}
|
||||
|
||||
query.
|
||||
Select("news.*, COUNT(content_logs.content_id) as clicked").
|
||||
Select("news.*, COUNT(content_logs.content_id) as views").
|
||||
Group("news.id").
|
||||
Order("news.created_at DESC")
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ type accessor struct {
|
||||
}
|
||||
|
||||
type News interface {
|
||||
GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, error)
|
||||
GetAll(filter newsdomain.NewsFilter) ([]newsdomain.NewsResponse, error)
|
||||
GetBySlug(string) (*newsdomain.News, error)
|
||||
GetByID(string) (*newsdomain.News, error)
|
||||
Create(newsdomain.News) error
|
||||
|
||||
@ -15,7 +15,7 @@ func GetAll(
|
||||
) {
|
||||
router.Get("/news", func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
news []newsdomain.News
|
||||
news []newsdomain.NewsResponse
|
||||
err error
|
||||
)
|
||||
ctx := r.Context()
|
||||
|
||||
@ -27,19 +27,23 @@ type News struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
FeaturedImage string `json:"featured_image"`
|
||||
Tags []tagdomain.Tag `gorm:"many2many:news_tags" json:"tags"`
|
||||
Categories []categorydomain.Category `gorm:"many2many:news_categories" json:"categories"`
|
||||
Tags []tagdomain.Tag `gorm:"many2many:news_tags;foreignKey:ID;joinForeignKey:news_id;References:ID;joinReferences:tag_id" json:"tags"`
|
||||
Categories []categorydomain.Category `gorm:"many2many:news_categories;foreignKey:ID;joinForeignKey:news_id;References:ID;joinReferences:category_id" json:"categories"`
|
||||
IsPremium bool `json:"is_premium"`
|
||||
Slug string `json:"slug"`
|
||||
AuthorID string `json:"author_id"`
|
||||
LiveAt time.Time `json:"live_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Clicked int64 `json:"clicked" gorm:"-"`
|
||||
|
||||
Author Staff `json:"author"`
|
||||
}
|
||||
|
||||
type NewsResponse struct {
|
||||
News
|
||||
Views int64 `json:"views"`
|
||||
}
|
||||
|
||||
type NewsUpdate struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
|
||||
@ -37,7 +37,6 @@ func (i *impl) Create(spec newsdomain.NewsReq, staffId string) error {
|
||||
AuthorID: staffId,
|
||||
Tags: tags,
|
||||
Categories: categories,
|
||||
Clicked: 0,
|
||||
}
|
||||
|
||||
if err := i.newsRepo.Create(newSpec); err != nil {
|
||||
|
||||
@ -5,12 +5,12 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (i *impl) GetAll(filter newsdomain.Filter) ([]newsdomain.News, error) {
|
||||
func (i *impl) GetAll(filter newsdomain.Filter) ([]newsdomain.NewsResponse, error) {
|
||||
var err error
|
||||
|
||||
categories := []string{}
|
||||
tags := []string{}
|
||||
news := []newsdomain.News{}
|
||||
news := []newsdomain.NewsResponse{}
|
||||
|
||||
tagCodeArr := strings.Split(filter.Tags, " ")
|
||||
categoryCodeArr := strings.Split(filter.Category, " ")
|
||||
|
||||
@ -16,7 +16,7 @@ type impl struct {
|
||||
}
|
||||
|
||||
type News interface {
|
||||
GetAll(filter newsdomain.Filter) ([]newsdomain.News, error)
|
||||
GetAll(filter newsdomain.Filter) ([]newsdomain.NewsResponse, error)
|
||||
GetBySlug(string) (*newsdomain.News, error)
|
||||
GetByID(string) (*newsdomain.News, error)
|
||||
Create(newsdomain.NewsReq, string) error
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user