2024-05-16 18:50:53 +00:00
|
|
|
package migration
|
2024-05-16 19:48:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
|
|
|
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
|
|
|
"fmt"
|
2024-05-17 18:49:55 +00:00
|
|
|
"strings"
|
2024-05-16 19:48:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MigrateAuthors struct {
|
|
|
|
SlowtwitchDatabase *sql.DB
|
|
|
|
ResultsDatabase *sql.DB
|
|
|
|
WordpressBaseUrl string
|
|
|
|
WordpressUser string
|
|
|
|
WordpressPassword string
|
|
|
|
}
|
|
|
|
|
2024-05-21 16:50:05 +00:00
|
|
|
// TODO Add first and last name
|
2024-05-16 19:48:39 +00:00
|
|
|
func (migration *MigrateAuthors) Execute() []EditorResult {
|
|
|
|
editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase)
|
|
|
|
var output []EditorResult
|
|
|
|
|
|
|
|
for _, editor := range editors {
|
|
|
|
hasBeenMigrated := EditorHasBeenMigrated(editor.Username, migration.ResultsDatabase)
|
|
|
|
|
|
|
|
if hasBeenMigrated == false {
|
2024-05-21 16:50:05 +00:00
|
|
|
firstName, lastName := getFirstAndLastName(editor.Name)
|
|
|
|
|
2024-05-16 19:48:39 +00:00
|
|
|
createUser := wordpress.CreateUser{
|
2024-05-21 16:50:05 +00:00
|
|
|
Username: strings.TrimSpace(editor.Username),
|
|
|
|
Email: strings.TrimSpace(editor.Email),
|
|
|
|
Password: editor.Password,
|
|
|
|
Roles: "editor",
|
|
|
|
FirstName: firstName,
|
|
|
|
LastName: lastName,
|
2024-05-16 19:48:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 18:49:55 +00:00
|
|
|
result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
|
|
|
|
|
|
|
wordpressId := 0
|
|
|
|
isSuccess := false
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
wordpressId = result.Id
|
|
|
|
isSuccess = true
|
|
|
|
}
|
|
|
|
|
2024-05-16 19:48:39 +00:00
|
|
|
editorResult := EditorResult{
|
2024-05-17 18:49:55 +00:00
|
|
|
Username: editor.Username,
|
|
|
|
Email: editor.Email,
|
|
|
|
WordpressId: wordpressId,
|
|
|
|
IsSuccess: isSuccess,
|
2024-05-16 19:48:39 +00:00
|
|
|
}
|
2024-05-17 18:49:55 +00:00
|
|
|
err = CreateEditorResult(editorResult, migration.ResultsDatabase)
|
2024-05-16 19:48:39 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
output = append(output, editorResult)
|
|
|
|
fmt.Println("Created user:", result.Username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
2024-05-21 16:50:05 +00:00
|
|
|
|
|
|
|
func getFirstAndLastName(name string) (firstName string, lastName string) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
names := strings.Split(name, " ")
|
|
|
|
|
|
|
|
firstName = strings.TrimSpace(names[0])
|
|
|
|
|
|
|
|
lastName = ""
|
|
|
|
if len(names) > 1 {
|
|
|
|
for _, name := range names[1:] {
|
|
|
|
lastName += " " + name
|
|
|
|
}
|
|
|
|
lastName = strings.TrimSpace(lastName)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|