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 } // TODO Add first and last name func (migration *MigrateAuthors) Execute() []EditorResult { editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase) var output []EditorResult for _, editor := range editors { hasBeenMigrated := EditorHasBeenMigrated(editor.Username, editor.Email, migration.ResultsDatabase) if hasBeenMigrated == false { firstName, lastName := getFirstAndLastName(editor.Name) createUser := wordpress.CreateUser{ Username: strings.TrimSpace(editor.Username), Email: strings.TrimSpace(editor.Email), Password: editor.Password, Roles: "editor", FirstName: firstName, LastName: lastName, } result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) wordpressId := 0 isSuccess := false if err == nil { wordpressId = result.Id isSuccess = true } else { fmt.Println("error creating user:", err) } 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 } 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 }