get categories and cache

This commit is contained in:
Ross Trottier 2024-05-09 14:06:01 -06:00
parent b59d36f2c2
commit c0dacf4fb7
3 changed files with 75 additions and 12 deletions

View File

@ -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
}

38
main.go
View File

@ -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)
}

View File

@ -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
}