From c410e651ce9a8a3a852697c378891e362a1b909e Mon Sep 17 00:00:00 2001 From: ericprd Date: Fri, 7 Mar 2025 14:30:31 +0800 Subject: [PATCH] fix: categories filter is multiple now --- internal/accessor/category/get_by_codes.go | 12 ++++++------ internal/accessor/category/impl.go | 2 +- internal/accessor/news/get_all.go | 4 ++-- internal/api/http/news/get_all.go | 2 +- internal/domain/news/spec.go | 2 +- internal/services/news/get_all.go | 17 ++++++++--------- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/internal/accessor/category/get_by_codes.go b/internal/accessor/category/get_by_codes.go index d716263..5856bbd 100644 --- a/internal/accessor/category/get_by_codes.go +++ b/internal/accessor/category/get_by_codes.go @@ -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 } diff --git a/internal/accessor/category/impl.go b/internal/accessor/category/impl.go index bf0c1d9..bbdde67 100644 --- a/internal/accessor/category/impl.go +++ b/internal/accessor/category/impl.go @@ -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 diff --git a/internal/accessor/news/get_all.go b/internal/accessor/news/get_all.go index 048f0a5..d753e5f 100644 --- a/internal/accessor/news/get_all.go +++ b/internal/accessor/news/get_all.go @@ -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 { diff --git a/internal/api/http/news/get_all.go b/internal/api/http/news/get_all.go index d74d550..6879587 100644 --- a/internal/api/http/news/get_all.go +++ b/internal/api/http/news/get_all.go @@ -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) diff --git a/internal/domain/news/spec.go b/internal/domain/news/spec.go index 0d123cb..1eda171 100644 --- a/internal/domain/news/spec.go +++ b/internal/domain/news/spec.go @@ -52,5 +52,5 @@ type NewsUpdate struct { type NewsFilter struct { Tags []string - Category string + Category []string } diff --git a/internal/services/news/get_all.go b/internal/services/news/get_all.go index b064422..8499e64 100644 --- a/internal/services/news/get_all.go +++ b/internal/services/news/get_all.go @@ -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) }