find user by username

This commit is contained in:
Ross Trottier 2024-05-07 18:52:47 -06:00
parent f264b68565
commit 880d4985f4
6 changed files with 47 additions and 32 deletions

19
app-cache.go Normal file
View File

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

29
main.go
View File

@ -6,8 +6,8 @@ import (
) )
const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/" const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/"
const user = "admin" const wordpressKey = "admin"
const password = "S34E keY1 A1uX 6ncs Rx4T f21W" const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
var appCache AppCache var appCache AppCache
@ -23,24 +23,11 @@ func main() {
//Upload Photo + add to cache //Upload Photo + add to cache
//Use cached data to correctly build post form submissions //Use cached data to correctly build post form submissions
posts := wordpress.GetPosts(baseUrl, user, password) user, ok := wordpress.GetUser(baseUrl, "Bongo Bangidsn Hobs", wordpressKey, wordpressSecret)
for i, postData := range posts { if ok {
fmt.Println("Post Number:", i+1, postData.Title.Rendered) 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
}

View File

@ -17,10 +17,15 @@ type CreatePost struct {
Slug string `json:"slug"` 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" endpoint := baseUrl + "posts"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var post PostData var post CreatePostResponse
err := json.Unmarshal(body, &post) err := json.Unmarshal(body, &post)
utilities.CheckError(err) utilities.CheckError(err)

View File

@ -15,6 +15,14 @@ type CreateUser struct {
Password string `json:"password"` 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 { func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData {
endpoint := baseUrl + "users" endpoint := baseUrl + "users"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)

View File

@ -24,7 +24,6 @@ type PostData struct {
func GetPosts(baseUrl, user, pass string) []PostData { func GetPosts(baseUrl, user, pass string) []PostData {
url := baseUrl + "posts?per_page=99" url := baseUrl + "posts?per_page=99"
body := utilities.GetHttpRequestToWordpress(url, user, pass) body := utilities.GetHttpRequestToWordpress(url, user, pass)
var posts []PostData var posts []PostData
err := json.Unmarshal(body, &posts) err := json.Unmarshal(body, &posts)
utilities.CheckError(err) utilities.CheckError(err)

View File

@ -8,14 +8,11 @@ import (
type UserData struct { type UserData struct {
Id int `json:"id"` Id int `json:"id"`
Username string `json:"username"` Name string `json:"name"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
} }
func GetUser(baseUrl, username, user, pass string) (UserData, bool) { func GetUser(baseUrl, name, user, pass string) (UserData, bool) {
endpoint := baseUrl + "/users?search=" + url.QueryEscape(username) endpoint := baseUrl + "users?search=" + url.QueryEscape(name)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var userData []UserData var userData []UserData
@ -23,7 +20,7 @@ func GetUser(baseUrl, username, user, pass string) (UserData, bool) {
if err == nil { if err == nil {
for _, userData := range userData { for _, userData := range userData {
if userData.Username == username { if userData.Name == name {
return userData, true return userData, true
} }
} }