fix
This commit is contained in:
@@ -71,16 +71,39 @@ func (uc *SettingsUsecase) ImportDictionary(ctx context.Context, dictionary *Dic
|
||||
return uc.SettingsRepo.ImportDictionary(ctx, dictionary, details)
|
||||
}
|
||||
|
||||
func (uc *SettingsUsecase) DictionaryDetailsByParent(ctx context.Context, dictionaryID, parentID uint) ([]*DictionaryDetail, error) {
|
||||
func (uc *SettingsUsecase) DictionaryDetailsByParent(ctx context.Context, dictionaryID uint, parentID *uint, includeChildren bool) ([]*DictionaryDetail, error) {
|
||||
items, _, err := uc.ListDictionaryDetails(ctx, 0, 0, DictionaryDetailFilter{DictionaryID: dictionaryID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]*DictionaryDetail, 0)
|
||||
direct := make([]*DictionaryDetail, 0)
|
||||
for _, item := range items {
|
||||
if (parentID == 0 && item.ParentID == nil) || (item.ParentID != nil && *item.ParentID == parentID) {
|
||||
out = append(out, item)
|
||||
if (parentID == nil && item.ParentID == nil) || (parentID != nil && item.ParentID != nil && *item.ParentID == *parentID) {
|
||||
direct = append(direct, item)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
if !includeChildren {
|
||||
return direct, nil
|
||||
}
|
||||
children := make(map[uint][]*DictionaryDetail)
|
||||
for _, item := range items {
|
||||
item.Children = []*DictionaryDetail{}
|
||||
if item.ParentID != nil {
|
||||
children[*item.ParentID] = append(children[*item.ParentID], item)
|
||||
}
|
||||
}
|
||||
var attach func(*DictionaryDetail)
|
||||
attach = func(item *DictionaryDetail) {
|
||||
item.Children = children[item.ID]
|
||||
if item.Children == nil {
|
||||
item.Children = []*DictionaryDetail{}
|
||||
}
|
||||
for _, child := range item.Children {
|
||||
attach(child)
|
||||
}
|
||||
}
|
||||
for _, item := range direct {
|
||||
attach(item)
|
||||
}
|
||||
return direct, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user