fix: get news by query category and tags filters

This commit is contained in:
ericprd
2025-03-07 00:55:50 +08:00
parent e3a9b67e28
commit 51b50a3132
10 changed files with 114 additions and 10 deletions
+42 -3
View File
@@ -1,7 +1,46 @@
package newssvc
import newsdomain "legalgo-BE-go/internal/domain/news"
import (
newsdomain "legalgo-BE-go/internal/domain/news"
"strings"
)
func (i *impl) GetAll() ([]newsdomain.News, error) {
return i.newsRepo.GetAll()
func (i *impl) GetAll(categoryCode, tagCodes string) ([]newsdomain.News, error) {
var (
category string
err error
)
tags := []string{}
news := []newsdomain.News{}
tagCodeArr := strings.Split(tagCodes, " ")
if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
if err != nil {
return news, err
}
if len(tags) < 1 {
return news, nil
}
}
if categoryCode != "" {
category, err = i.categoryRepo.GetIDByCode(categoryCode)
if err != nil {
return news, err
}
if category == "" {
return news, nil
}
}
filter := newsdomain.NewsFilter{
Tags: tags,
Category: category,
}
return i.newsRepo.GetAll(filter)
}
+1 -1
View File
@@ -16,7 +16,7 @@ type impl struct {
}
type News interface {
GetAll() ([]newsdomain.News, error)
GetAll(string, string) ([]newsdomain.News, error)
GetBySlug(string) (*newsdomain.News, error)
Create(newsdomain.NewsReq, string) error
Update(string, newsdomain.NewsUpdate) error