create category

This commit is contained in:
Ross Trottier 2024-05-09 15:25:13 -06:00
parent c0dacf4fb7
commit 45358bca52
2 changed files with 22 additions and 31 deletions

31
main.go
View File

@ -1,10 +1,5 @@
package main
import (
"federated.computer/wp-sync-slowtwitch/services/wordpress"
"fmt"
)
const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/"
const wordpressKey = "admin"
const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
@ -15,30 +10,4 @@ func main() {
// 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)
}
}

View File

@ -0,0 +1,22 @@
package wordpress
import (
"encoding/json"
"federated.computer/wp-sync-slowtwitch/utilities"
)
type CreateCategory struct {
Name string `json:"name"`
Description string `json:"description"`
ParentId int `json:"parent"`
Slug string `json:"slug"`
}
func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData {
endpoint := baseUrl + "categories"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var category CategoryData
err := json.Unmarshal(body, &category)
utilities.CheckError(err)
return category
}