diff --git a/app-cache.go b/app-cache.go index d638fda..a7f458e 100644 --- a/app-cache.go +++ b/app-cache.go @@ -3,8 +3,9 @@ package main import "federated.computer/wp-sync-slowtwitch/services/wordpress" type AppCache struct { - UsersCache map[string]wordpress.UserData - TagCache map[string]wordpress.TagData + UsersCache map[string]wordpress.UserData + TagCache map[string]wordpress.TagData + CategoryCache map[string]wordpress.CategoryData } func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { @@ -30,3 +31,19 @@ func (appCache *AppCache) SetTag(tag wordpress.TagData) { } appCache.TagCache[tag.Name] = tag } + +func (appCache *AppCache) GetCategory(categoryName string) (wordpress.CategoryData, bool) { + if appCache.CategoryCache != nil { + category, ok := appCache.CategoryCache[categoryName] + return category, ok + } + + return wordpress.CategoryData{}, false +} + +func (appCache *AppCache) SetCategory(category wordpress.CategoryData) { + if appCache.CategoryCache == nil { + appCache.CategoryCache = make(map[string]wordpress.CategoryData) + } + appCache.CategoryCache[category.Name] = category +} diff --git a/main.go b/main.go index 19713cb..efd4401 100644 --- a/main.go +++ b/main.go @@ -12,15 +12,33 @@ const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" var appCache AppCache func main() { - //Get category and cache - //Get single category from cache by name - //Get photo and cache with URL - //Create Category (with parent) + add to cache - //Upload Photo + add to cache - //Use cached data to correctly build post form submissions - createImage := wordpress.CreateImage{ - Url: "https://www.slowtwitch.com/articles/images/3/218713-largest_IMG_4527.jpg", + // TODO Create Category with or without parent + add to cache + // TODO Use cached data to correctly build post form submissions + + categoryNames := []string{"testcat", "testcat3", "you wont find me"} + + for _, categoryName := range categoryNames { + categoryData, ok := wordpress.GetCategory(categoryName, baseUrl, wordpressKey, wordpressSecret) + + if ok { + appCache.SetCategory(categoryData) + fmt.Println(categoryData.Name, categoryData.Id, categoryData.ParentId) + } else { + fmt.Println("not found on website: ", categoryName) + } + } + + for _, categoryName := range categoryNames { + category, ok := appCache.GetCategory(categoryName) + + if ok { + fmt.Println("Found single in cache: ", category.Name) + } else { + fmt.Println("Not found in cache:", categoryName) + } + } + + for _, category := range appCache.CategoryCache { + fmt.Println("From Cache: ", category.Name) } - imageData := createImage.Execute(baseUrl, wordpressKey, wordpressSecret) - fmt.Println(imageData.Link) } diff --git a/services/wordpress/get-category.go b/services/wordpress/get-category.go new file mode 100644 index 0000000..b183e52 --- /dev/null +++ b/services/wordpress/get-category.go @@ -0,0 +1,28 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" + "net/url" +) + +type CategoryData struct { + Id int `json:"id"` + ParentId int `json:"parent_id"` + Name string `json:"name"` + ParentName string `json:"parent_name"` +} + +func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) { + endpoint := baseUrl + "categories?search=" + url.QueryEscape(name) + response := utilities.GetHttpRequestToWordpress(endpoint, user, pass) + var categoryData []CategoryData + err := json.Unmarshal(response, &categoryData) + utilities.CheckError(err) + for _, category := range categoryData { + if category.Name == name { + return category, true + } + } + return CategoryData{}, false +}