fixed redirects - refactored for readability

This commit is contained in:
Ross Trottier 2024-05-16 12:50:53 -06:00
parent 83ef9f4192
commit 7931a859f3
15 changed files with 76 additions and 60 deletions

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
) )
const baseUrl = "https://slowtwitch.cloud/wp-json/" const baseUrl = "https://slowtwitch.cloud/"
const wordpressKey = "admin@slowtwitch.cloud" const wordpressKey = "admin@slowtwitch.cloud"
const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh" const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh"

View File

@ -20,7 +20,7 @@ func Connect(user, pass, url, port, dbName string) (*sql.DB, error) {
return nil, err return nil, err
} }
fmt.Println("Successfully connected!") fmt.Println("Successfully connected to:", dbName)
return db, nil return db, nil
} }

View File

@ -0,0 +1 @@
package migration

View File

@ -29,43 +29,22 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
errorMessage := "" errorMessage := ""
//Get wordpress parent if exists
wordpressParentId, err := getWordpressParentCategoryId(category, migration, errorMessage) wordpressParentId, err := getWordpressParentCategoryId(category, migration, errorMessage)
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
err = nil err = nil
} }
//convert url for redirect slowtwitchPath, slowtwitchUrl, httpStatus := getSlowtwitchUrlsAndVerify(category)
slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName) wordpressCategory := createInWordpress(category, wordpressParentId, migration)
slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath) err, errorMessage = createRedirect(wordpressCategory, migration, slowtwitchPath, err, errorMessage)
httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl) overallResult := submitResults(wordpressCategory, category, slowtwitchUrl, httpStatus, errorMessage, err, migration)
//create in wp fmt.Println("Successfully Created Category:", wordpressCategory.Name)
createWordpressCategory := wordpress.CreateCategory{ output = append(output, overallResult)
Name: strings.Trim(category.Name, " "),
Description: "",
ParentId: wordpressParentId,
} }
wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) return output
//submit redirect
createRedirect := wordpress.CreateRedirect{
Url: slowtwitchPath,
Title: "Category: " + wordpressCategory.Name,
MatchType: "page",
ActionType: "url",
ActionCode: 301,
GroupId: 1,
ActionData: wordpress.ActionData{
Url: wordpressCategory.Slug,
},
} }
_, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) func submitResults(wordpressCategory wordpress.CategoryData, category slowtwitch.SlowtwitchCategory, slowtwitchUrl string, httpStatus int, errorMessage string, err error, migration *MigrateCategories) CategoryResult {
if err != nil {
errorMessage = errorMessage + err.Error()
err = nil
}
//submit results
overallResult := CategoryResult{ overallResult := CategoryResult{
WordpressId: wordpressCategory.Id, WordpressId: wordpressCategory.Id,
SlowtwitchId: category.Id, SlowtwitchId: category.Id,
@ -81,9 +60,45 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
fmt.Println(err) fmt.Println(err)
err = nil err = nil
} }
output = append(output, overallResult) return overallResult
} }
return output
func createRedirect(wordpressCategory wordpress.CategoryData, migration *MigrateCategories, slowtwitchPath string, err error, errorMessage string) (error, string) {
redirectUrl := "/" + strings.ReplaceAll(wordpressCategory.Link, migration.WordpressBaseUrl, "")
createRedirect := wordpress.CreateRedirect{
Url: slowtwitchPath,
Title: "Category: " + wordpressCategory.Name,
MatchType: "page",
ActionType: "url",
ActionCode: 301,
GroupId: 1,
ActionData: wordpress.ActionData{
Url: redirectUrl,
},
}
_, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil {
errorMessage = errorMessage + err.Error()
err = nil
}
return err, errorMessage
}
func createInWordpress(category slowtwitch.SlowtwitchCategory, wordpressParentId int, migration *MigrateCategories) wordpress.CategoryData {
createWordpressCategory := wordpress.CreateCategory{
Name: strings.Trim(category.Name, " "),
Description: "",
ParentId: wordpressParentId,
}
wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
return wordpressCategory
}
func getSlowtwitchUrlsAndVerify(category slowtwitch.SlowtwitchCategory) (string, string, int) {
slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName)
slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath)
httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl)
return slowtwitchPath, slowtwitchUrl, httpStatus
} }
func getWordpressParentCategoryId(category slowtwitch.SlowtwitchCategory, migration *MigrateCategories, errorMessage string) (int, error) { func getWordpressParentCategoryId(category slowtwitch.SlowtwitchCategory, migration *MigrateCategories, errorMessage string) (int, error) {

View File

@ -8,7 +8,7 @@ func ConvertUrlToCategoryFormat(oldUrl string) string {
output = strings.ReplaceAll(output, "(", "_") output = strings.ReplaceAll(output, "(", "_")
output = strings.ReplaceAll(output, ")", "_") output = strings.ReplaceAll(output, ")", "_")
return output + "/index.html" return "/" + output + "/index.html"
} }
func GetURL(path string) string { func GetURL(path string) string {

View File

@ -12,7 +12,7 @@ type CreateCategory struct {
} }
func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData { func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData {
endpoint := baseUrl + "wp/v2/categories" endpoint := baseUrl + "wp-json/wp/v2/categories"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var category CategoryData var category CategoryData
err := json.Unmarshal(body, &category) err := json.Unmarshal(body, &category)

View File

@ -24,7 +24,7 @@ func (parameters *CreateImage) Execute(baseUrl, user, pass string) CreateImageRe
defer utilities.CloseBodyAndCheckError(resp.Body) defer utilities.CloseBodyAndCheckError(resp.Body)
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
utilities.CheckError(err) utilities.CheckError(err)
request, err := http.NewRequest("POST", baseUrl+"wp/v2/media", bytes.NewReader(body)) request, err := http.NewRequest("POST", baseUrl+"wp-json/wp/v2/media", bytes.NewReader(body))
utilities.CheckError(err) utilities.CheckError(err)
filename := GetFileName(parameters.Url) filename := GetFileName(parameters.Url)
request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`) request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`)

View File

@ -24,7 +24,7 @@ type CreatePostResponse struct {
} }
func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse { func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse {
endpoint := baseUrl + "wp/v2/posts" endpoint := baseUrl + "wp-json/wp/v2/posts"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var post CreatePostResponse var post CreatePostResponse
err := json.Unmarshal(body, &post) err := json.Unmarshal(body, &post)

View File

@ -34,7 +34,7 @@ type CreatedRedirect struct {
} }
func (parameters *CreateRedirect) Execute(baseUrl, user, pass string) (CreatedRedirect, error) { func (parameters *CreateRedirect) Execute(baseUrl, user, pass string) (CreatedRedirect, error) {
url := baseUrl + "redirection/v1/redirect" url := baseUrl + "wp-json/redirection/v1/redirect"
response := utilities.PostHttpRequestToWordpress(url, user, pass, parameters) response := utilities.PostHttpRequestToWordpress(url, user, pass, parameters)
var results CreateRedirectResponse var results CreateRedirectResponse
err := json.Unmarshal(response, &results) err := json.Unmarshal(response, &results)

View File

@ -18,7 +18,7 @@ type CreateTagResponse struct {
} }
func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse { func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse {
endpoint := baseUrl + "wp/v2/tags" endpoint := baseUrl + "wp-json/wp/v2/tags"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var tagResponse CreateTagResponse var tagResponse CreateTagResponse
err := json.Unmarshal(body, &tagResponse) err := json.Unmarshal(body, &tagResponse)

View File

@ -24,7 +24,7 @@ type CreateUserResponse struct {
} }
func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse { func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse {
endpoint := baseUrl + "wp/v2/users" endpoint := baseUrl + "wp-json/wp/v2/users"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var userData CreateUserResponse var userData CreateUserResponse
err := json.Unmarshal(body, &userData) err := json.Unmarshal(body, &userData)

View File

@ -16,7 +16,7 @@ type CategoryData struct {
} }
func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) { func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) {
endpoint := baseUrl + "wp/v2/categories?search=" + url.QueryEscape(name) endpoint := baseUrl + "wp-json/wp/v2/categories?search=" + url.QueryEscape(name)
response := utilities.GetHttpRequestToWordpress(endpoint, user, pass) response := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var categoryData []CategoryData var categoryData []CategoryData
err := json.Unmarshal(response, &categoryData) err := json.Unmarshal(response, &categoryData)

View File

@ -22,7 +22,7 @@ type PostData struct {
} }
func GetPosts(baseUrl, user, pass string) []PostData { func GetPosts(baseUrl, user, pass string) []PostData {
url := baseUrl + "wp/v2/posts?per_page=99" url := baseUrl + "wp-json/wp/v2/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)

View File

@ -13,7 +13,7 @@ type TagData struct {
} }
func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) { func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) {
endpoint := baseUrl + "wp/v2/tags?search=" + url.QueryEscape(tagName) endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var tagData []TagData var tagData []TagData
err := json.Unmarshal(body, &tagData) err := json.Unmarshal(body, &tagData)

View File

@ -12,7 +12,7 @@ type UserData struct {
} }
func GetUser(baseUrl, name, user, pass string) (UserData, bool) { func GetUser(baseUrl, name, user, pass string) (UserData, bool) {
endpoint := baseUrl + "wp/v2/users?search=" + url.QueryEscape(name) endpoint := baseUrl + "wp-json/wp/v2/users?search=" + url.QueryEscape(name)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var userData []UserData var userData []UserData
err := json.Unmarshal(body, &userData) err := json.Unmarshal(body, &userData)