Compare commits
2 Commits
51b50a3132
...
c48a4b944b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c48a4b944b | ||
|
|
c410e651ce |
@ -4,16 +4,16 @@ import (
|
|||||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *accessor) GetIDByCode(code string) (string, error) {
|
func (a *accessor) GetIDByCode(codes []string) ([]string, error) {
|
||||||
var category string
|
var categories []string
|
||||||
|
|
||||||
if err := a.db.
|
if err := a.db.
|
||||||
Model(&categorydomain.Category{}).
|
Model(&categorydomain.Category{}).
|
||||||
Select("id").Where("code = ?", code).
|
Select("id").Where("code IN ?", codes).
|
||||||
Pluck("id", &category).
|
Pluck("id", &categories).
|
||||||
Error; err != nil {
|
Error; err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return category, nil
|
return categories, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ type accessor struct {
|
|||||||
type Category interface {
|
type Category interface {
|
||||||
GetAllModel() ([]categorydomain.Category, error)
|
GetAllModel() ([]categorydomain.Category, error)
|
||||||
GetByIDs([]string) ([]categorydomain.Category, error)
|
GetByIDs([]string) ([]categorydomain.Category, error)
|
||||||
GetIDByCode(string) (string, error)
|
GetIDByCode([]string) ([]string, error)
|
||||||
CreateModel(categorydomain.CategoryReq) error
|
CreateModel(categorydomain.CategoryReq) error
|
||||||
Update(categorydomain.Category) error
|
Update(categorydomain.Category) error
|
||||||
Delete(string) error
|
Delete(string) error
|
||||||
|
|||||||
@ -9,9 +9,9 @@ func (a *accessor) GetAll(filter newsdomain.NewsFilter) ([]newsdomain.News, erro
|
|||||||
Preload("Categories").
|
Preload("Categories").
|
||||||
Preload("Author")
|
Preload("Author")
|
||||||
|
|
||||||
if filter.Category != "" {
|
if len(filter.Category) > 0 {
|
||||||
query = query.Joins("JOIN news_categories nc ON nc.news_id = news.id").
|
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 {
|
if len(filter.Tags) > 0 {
|
||||||
|
|||||||
@ -2,7 +2,9 @@ package newsrepository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||||
newsdomain "legalgo-BE-go/internal/domain/news"
|
newsdomain "legalgo-BE-go/internal/domain/news"
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
@ -34,26 +36,27 @@ func (a *accessor) Update(spec newsdomain.News) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return fmt.Errorf("failed to update news: %v", err)
|
return fmt.Errorf("failed to update news: %v", err)
|
||||||
}
|
}
|
||||||
if len(spec.Tags) < 1 {
|
|
||||||
if err := tx.Model(&spec).Association("Tags").Clear(); err != nil {
|
tagsDeleted := make([]tagdomain.Tag, len(spec.Tags))
|
||||||
tx.Rollback()
|
copy(tagsDeleted, spec.Tags)
|
||||||
return fmt.Errorf("failed to clear tags: %v", err)
|
if err := tx.Model(&spec).Association("Tags").Clear(); err != nil {
|
||||||
}
|
tx.Rollback()
|
||||||
|
return fmt.Errorf("failed to remove previous tags: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(spec.Categories) < 1 {
|
if err := tx.Model(&spec).Association("Tags").Append(tagsDeleted); err != nil {
|
||||||
if err := tx.Model(&spec).Association("Categories").Clear(); err != nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return fmt.Errorf("failed to clear categories: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tx.Model(&spec).Association("Tags").Append(spec.Tags); err != nil {
|
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return fmt.Errorf("failed to add tags: %v", err)
|
return fmt.Errorf("failed to add tags: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(&spec).Association("Categories").Append(spec.Categories); err != nil {
|
categoriesDeleted := make([]categorydomain.Category, len(spec.Categories))
|
||||||
|
copy(categoriesDeleted, spec.Categories)
|
||||||
|
if err := tx.Model(&spec).Association("Categories").Clear(); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return fmt.Errorf("failed to remove previous categories: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(&spec).Association("Categories").Append(categoriesDeleted); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return fmt.Errorf("failed to add categories: %v", err)
|
return fmt.Errorf("failed to add categories: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ func GetAll(
|
|||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
|
||||||
category := query.Get("category")
|
category := query.Get("categories")
|
||||||
tags := query.Get("tags")
|
tags := query.Get("tags")
|
||||||
|
|
||||||
news, err = newsSvc.GetAll(category, tags)
|
news, err = newsSvc.GetAll(category, tags)
|
||||||
|
|||||||
@ -52,5 +52,5 @@ type NewsUpdate struct {
|
|||||||
|
|
||||||
type NewsFilter struct {
|
type NewsFilter struct {
|
||||||
Tags []string
|
Tags []string
|
||||||
Category string
|
Category []string
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,16 +5,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (i *impl) GetAll(categoryCode, tagCodes string) ([]newsdomain.News, error) {
|
func (i *impl) GetAll(categoriesCode, tagCodes string) ([]newsdomain.News, error) {
|
||||||
var (
|
var err error
|
||||||
category string
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
|
categories := []string{}
|
||||||
tags := []string{}
|
tags := []string{}
|
||||||
news := []newsdomain.News{}
|
news := []newsdomain.News{}
|
||||||
|
|
||||||
tagCodeArr := strings.Split(tagCodes, " ")
|
tagCodeArr := strings.Split(tagCodes, " ")
|
||||||
|
categoryCodeArr := strings.Split(categoriesCode, " ")
|
||||||
|
|
||||||
if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
|
if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
|
||||||
tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
|
tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
|
||||||
@ -27,20 +26,20 @@ func (i *impl) GetAll(categoryCode, tagCodes string) ([]newsdomain.News, error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if categoryCode != "" {
|
if len(categoryCodeArr) > 0 && categoryCodeArr[0] != "" {
|
||||||
category, err = i.categoryRepo.GetIDByCode(categoryCode)
|
categories, err = i.categoryRepo.GetIDByCode(categoryCodeArr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return news, err
|
return news, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if category == "" {
|
if len(categories) < 1 {
|
||||||
return news, nil
|
return news, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filter := newsdomain.NewsFilter{
|
filter := newsdomain.NewsFilter{
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Category: category,
|
Category: categories,
|
||||||
}
|
}
|
||||||
return i.newsRepo.GetAll(filter)
|
return i.newsRepo.GetAll(filter)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user