diff --git a/app-cache.go b/app-cache.go index 7c2e01c..d638fda 100644 --- a/app-cache.go +++ b/app-cache.go @@ -4,6 +4,7 @@ import "federated.computer/wp-sync-slowtwitch/services/wordpress" type AppCache struct { UsersCache map[string]wordpress.UserData + TagCache map[string]wordpress.TagData } func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { @@ -17,3 +18,15 @@ func (appCache *AppCache) SetUser(user wordpress.UserData) { } appCache.UsersCache[user.Name] = user } + +func (appCache *AppCache) GetTag(tagName string) (wordpress.TagData, bool) { + tag, ok := appCache.TagCache[tagName] + return tag, ok +} + +func (appCache *AppCache) SetTag(tag wordpress.TagData) { + if appCache.TagCache == nil { + appCache.TagCache = make(map[string]wordpress.TagData) + } + appCache.TagCache[tag.Name] = tag +} diff --git a/main.go b/main.go index e22d637..2ffdea5 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,6 @@ const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" var appCache AppCache func main() { - //Get user and cache //Get category and cache //Get single category from cache by name //Get tag and cache @@ -23,11 +22,11 @@ func main() { //Upload Photo + add to cache //Use cached data to correctly build post form submissions - user, ok := wordpress.GetUser(baseUrl, "Bongo Bangidsn Hobs", wordpressKey, wordpressSecret) + tag, ok := wordpress.GetTag("golang-tag", baseUrl, wordpressKey, wordpressSecret) if ok { - fmt.Println("User found:", user.Name) - appCache.SetUser(user) + appCache.SetTag(tag) + fmt.Println("Found tag:", tag.Name) } else { - fmt.Println("User not found") + fmt.Println("Could not find tag.") } } diff --git a/services/wordpress/create-post.go b/services/wordpress/create-post.go index e0f6d31..c24d3fc 100644 --- a/services/wordpress/create-post.go +++ b/services/wordpress/create-post.go @@ -28,6 +28,5 @@ func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResp var post CreatePostResponse err := json.Unmarshal(body, &post) utilities.CheckError(err) - return post } diff --git a/services/wordpress/create-tag.go b/services/wordpress/create-tag.go new file mode 100644 index 0000000..13f720f --- /dev/null +++ b/services/wordpress/create-tag.go @@ -0,0 +1,27 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" +) + +type CreateTag struct { + Name string `json:"name"` + Description string `json:"description"` + Slug string `json:"slug"` +} + +type CreateTagResponse struct { + Id int `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` +} + +func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse { + endpoint := baseUrl + "tags" + body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) + var tagResponse CreateTagResponse + err := json.Unmarshal(body, &tagResponse) + utilities.CheckError(err) + return tagResponse +} diff --git a/services/wordpress/create-user.go b/services/wordpress/create-user.go index cc09668..c26e238 100644 --- a/services/wordpress/create-user.go +++ b/services/wordpress/create-user.go @@ -23,10 +23,10 @@ type CreateUserResponse struct { LastName string `json:"last_name"` } -func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData { +func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse { endpoint := baseUrl + "users" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) - var userData UserData + var userData CreateUserResponse err := json.Unmarshal(body, &userData) utilities.CheckError(err) return userData diff --git a/services/wordpress/get-tag.go b/services/wordpress/get-tag.go new file mode 100644 index 0000000..c9f40a6 --- /dev/null +++ b/services/wordpress/get-tag.go @@ -0,0 +1,28 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" + "net/url" +) + +type TagData struct { + Name string `json:"name"` + Id int `json:"id"` + Slug string `json:"slug"` +} + +func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) { + endpoint := baseUrl + "tags?search=" + url.QueryEscape(tagName) + body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) + var tagData []TagData + err := json.Unmarshal(body, &tagData) + if err == nil { + for _, data := range tagData { + if data.Name == tagName { + return data, true + } + } + } + return TagData{}, false +} diff --git a/services/wordpress/get-user.go b/services/wordpress/get-user.go index a1b9066..d7fa3c0 100644 --- a/services/wordpress/get-user.go +++ b/services/wordpress/get-user.go @@ -14,10 +14,8 @@ type UserData struct { func GetUser(baseUrl, name, user, pass string) (UserData, bool) { endpoint := baseUrl + "users?search=" + url.QueryEscape(name) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) - var userData []UserData err := json.Unmarshal(body, &userData) - if err == nil { for _, userData := range userData { if userData.Name == name { @@ -25,6 +23,5 @@ func GetUser(baseUrl, name, user, pass string) (UserData, bool) { } } } - return UserData{}, false }