Post-Migrator/services/migration/migrate-authors.go

61 lines
1.4 KiB
Go

package migration
import (
"database/sql"
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
"federated.computer/wp-sync-slowtwitch/services/wordpress"
"fmt"
"strings"
)
type MigrateAuthors struct {
SlowtwitchDatabase *sql.DB
ResultsDatabase *sql.DB
WordpressBaseUrl string
WordpressUser string
WordpressPassword string
}
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 {
createUser := wordpress.CreateUser{
Username: strings.Trim(editor.Username, " "),
Email: strings.Trim(editor.Email, " "),
Password: editor.Password,
Roles: "editor",
}
result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
wordpressId := 0
isSuccess := false
if err == nil {
wordpressId = result.Id
isSuccess = true
}
editorResult := EditorResult{
Username: editor.Username,
Email: editor.Email,
WordpressId: wordpressId,
IsSuccess: isSuccess,
}
err = CreateEditorResult(editorResult, migration.ResultsDatabase)
if err != nil {
fmt.Println(err)
}
output = append(output, editorResult)
fmt.Println("Created user:", result.Username)
}
}
return output
}