PiwigoDirectoryUploader/internal/pkg/piwigo/category.go
Philipp Häfelfinger 8068d829fe Refactored piwigo context to a object oriented implementation
Optimized http calls to reuse more code and DRY
Interface for PiwigoContext WIP
Naming WIP
2019-03-20 00:17:58 +01:00

53 lines
1.3 KiB
Go

package piwigo
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
)
type PiwigoCategory struct {
Id int
ParentId int
Name string
Key string
}
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
categoryLookups := map[string]*PiwigoCategory{}
for _, category := range categories {
logrus.Debugf("Loaded existing category %s", category.Key)
categoryLookups[category.Key] = category
}
return categoryLookups
}
func buildCategoryMap(statusResponse *getCategoryListResponse) map[int]*PiwigoCategory {
categories := map[int]*PiwigoCategory{}
for _, category := range statusResponse.Result.Categories {
categories[category.ID] = &PiwigoCategory{Id: category.ID, ParentId: category.IDUppercat, Name: category.Name, Key: category.Name}
}
return categories
}
func buildCategoryKeys(categories map[int]*PiwigoCategory) {
for _, category := range categories {
if category.ParentId == 0 {
category.Key = category.Name
continue
}
key := category.Name
parentId := category.ParentId
for parentId != 0 {
parent := categories[parentId]
// as we build the category as a directory hierarchy,
// we have to use the path separator to construct the path key
key = fmt.Sprintf("%s%c%s", parent.Name, os.PathSeparator, key)
parentId = parent.ParentId
}
category.Key = key
}
}