fix: categories filter is multiple now

This commit is contained in:
ericprd 2025-03-07 14:30:31 +08:00
parent 51b50a3132
commit c410e651ce
6 changed files with 19 additions and 20 deletions

View File

@ -4,16 +4,16 @@ import (
categorydomain "legalgo-BE-go/internal/domain/category"
)
func (a *accessor) GetIDByCode(code string) (string, error) {
var category string
func (a *accessor) GetIDByCode(codes []string) ([]string, error) {
var categories []string
if err := a.db.
Model(&categorydomain.Category{}).
Select("id").Where("code = ?", code).
Pluck("id", &category).
Select("id").Where("code IN ?", codes).
Pluck("id", &categories).
Error; err != nil {
return "", err
return nil, err
}
return category, nil
return categories, nil
}

View File

@ -12,7 +12,7 @@ type accessor struct {
type Category interface {
GetAllModel() ([]categorydomain.Category, error)
GetByIDs([]string) ([]categorydomain.Category, error)
GetIDByCode(string) (string, error)
GetIDByCode([]string) ([]string, error)
CreateModel(categorydomain.CategoryReq) error
Update(categorydomain.Category) error
Delete(string) error

View File

@ -9,9 +9,9 @@ func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, erro
Preload("Categories").
Preload("Author")
if filter.Category != "" {
if len(filter.Category) > 0 {
query = query.Joins("JOIN news_categories nc ON nc.news_id = news.id").
Where("nc.category_id = ?", filter.Category)
Where("nc.category_id IN (?)", filter.Category)
}
if len(filter.Tags) > 0 {

View File

@ -21,7 +21,7 @@ func GetAll(
ctx := r.Context()
query := r.URL.Query()
category := query.Get("category")
category := query.Get("categories")
tags := query.Get("tags")
news, err = newsSvc.GetAll(category, tags)

View File

@ -52,5 +52,5 @@ type NewsUpdate struct {
type NewsFilter struct {
Tags []string
Category string
Category []string
}

View File

@ -5,16 +5,15 @@ import (
"strings"
)
func (i *impl) GetAll(categoryCode, tagCodes string) ([]newsdomain.News, error) {
var (
category string
err error
)
func (i *impl) GetAll(categoriesCode, tagCodes string) ([]newsdomain.News, error) {
var err error
categories := []string{}
tags := []string{}
news := []newsdomain.News{}
tagCodeArr := strings.Split(tagCodes, " ")
categoryCodeArr := strings.Split(categoriesCode, " ")
if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
@ -27,20 +26,20 @@ func (i *impl) GetAll(categoryCode, tagCodes string) ([]newsdomain.News, error)
}
}
if categoryCode != "" {
category, err = i.categoryRepo.GetIDByCode(categoryCode)
if len(categoryCodeArr) > 0 && categoryCodeArr[0] != "" {
categories, err = i.categoryRepo.GetIDByCode(categoryCodeArr)
if err != nil {
return news, err
}
if category == "" {
if len(categories) < 1 {
return news, nil
}
}
filter := newsdomain.NewsFilter{
Tags: tags,
Category: category,
Category: categories,
}
return i.newsRepo.GetAll(filter)
}