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