diff --git a/app-cache.go b/app-cache.go new file mode 100644 index 0000000..7c2e01c --- /dev/null +++ b/app-cache.go @@ -0,0 +1,19 @@ +package main + +import "federated.computer/wp-sync-slowtwitch/services/wordpress" + +type AppCache struct { + UsersCache map[string]wordpress.UserData +} + +func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { + user, ok := appCache.UsersCache[username] + return user, ok +} + +func (appCache *AppCache) SetUser(user wordpress.UserData) { + if appCache.UsersCache == nil { + appCache.UsersCache = make(map[string]wordpress.UserData) + } + appCache.UsersCache[user.Name] = user +} diff --git a/main.go b/main.go index 1cd3cfa..e22d637 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,8 @@ import ( ) const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/" -const user = "admin" -const password = "S34E keY1 A1uX 6ncs Rx4T f21W" +const wordpressKey = "admin" +const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" var appCache AppCache @@ -23,24 +23,11 @@ func main() { //Upload Photo + add to cache //Use cached data to correctly build post form submissions - posts := wordpress.GetPosts(baseUrl, user, password) - for i, postData := range posts { - fmt.Println("Post Number:", i+1, postData.Title.Rendered) + user, ok := wordpress.GetUser(baseUrl, "Bongo Bangidsn Hobs", wordpressKey, wordpressSecret) + if ok { + fmt.Println("User found:", user.Name) + appCache.SetUser(user) + } else { + fmt.Println("User not found") } } - -type AppCache struct { - UsersCache map[string]wordpress.UserData -} - -func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { - user, ok := appCache.UsersCache[username] - return user, ok -} - -func (appCache *AppCache) SetUser(user wordpress.UserData) { - if appCache.UsersCache == nil { - appCache.UsersCache = make(map[string]wordpress.UserData) - } - appCache.UsersCache[user.Username] = user -} diff --git a/services/wordpress/create-post.go b/services/wordpress/create-post.go index 7ea188a..e0f6d31 100644 --- a/services/wordpress/create-post.go +++ b/services/wordpress/create-post.go @@ -17,10 +17,15 @@ type CreatePost struct { Slug string `json:"slug"` } -func (parameters *CreatePost) Execute(baseUrl, user, pass string) PostData { +type CreatePostResponse struct { + Id int `json:"id"` + Link string `json:"link"` +} + +func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse { endpoint := baseUrl + "posts" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) - var post PostData + var post CreatePostResponse err := json.Unmarshal(body, &post) utilities.CheckError(err) diff --git a/services/wordpress/create-user.go b/services/wordpress/create-user.go index e07abd0..cc09668 100644 --- a/services/wordpress/create-user.go +++ b/services/wordpress/create-user.go @@ -15,6 +15,14 @@ type CreateUser struct { Password string `json:"password"` } +type CreateUserResponse struct { + Id int `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` +} + func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData { endpoint := baseUrl + "users" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) diff --git a/services/wordpress/get-posts.go b/services/wordpress/get-posts.go index d042f11..f9adde7 100644 --- a/services/wordpress/get-posts.go +++ b/services/wordpress/get-posts.go @@ -24,7 +24,6 @@ type PostData struct { func GetPosts(baseUrl, user, pass string) []PostData { url := baseUrl + "posts?per_page=99" body := utilities.GetHttpRequestToWordpress(url, user, pass) - var posts []PostData err := json.Unmarshal(body, &posts) utilities.CheckError(err) diff --git a/services/wordpress/get-user.go b/services/wordpress/get-user.go index 170c49b..a1b9066 100644 --- a/services/wordpress/get-user.go +++ b/services/wordpress/get-user.go @@ -7,15 +7,12 @@ import ( ) type UserData struct { - Id int `json:"id"` - Username string `json:"username"` - Email string `json:"email"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` + Id int `json:"id"` + Name string `json:"name"` } -func GetUser(baseUrl, username, user, pass string) (UserData, bool) { - endpoint := baseUrl + "/users?search=" + url.QueryEscape(username) +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 @@ -23,7 +20,7 @@ func GetUser(baseUrl, username, user, pass string) (UserData, bool) { if err == nil { for _, userData := range userData { - if userData.Username == username { + if userData.Name == name { return userData, true } }