diff --git a/main.go b/main.go index 879edc6..450a0c2 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,14 @@ func main() { fmt.Println(resultsDBerr) } + editorMigration := migration.MigrateAuthors{ + SlowtwitchDatabase: slowtwitchDB, + ResultsDatabase: resultsDB, + WordpressBaseUrl: baseUrl, + WordpressUser: wordpressKey, + WordpressPassword: wordpressSecret, + } + editorMigration.Execute() categoryMigration := migration.MigrateCategories{ SlowtwitchDatabase: slowtwitchDB, ResultsDatabase: resultsDB, diff --git a/services/migration/editor-result.go b/services/migration/editor-result.go new file mode 100644 index 0000000..d7ff441 --- /dev/null +++ b/services/migration/editor-result.go @@ -0,0 +1,54 @@ +package migration + +import ( + "database/sql" + "errors" + "fmt" +) + +type EditorResult struct { + WordpressId int + Username string + Email string + IsSuccess bool +} + +func CreateEditorResult(result EditorResult, db *sql.DB) error { + _, err := db.Exec("insert into EditorsResults(WordpressId, Username, Email, IsSuccess) values (?, ?, ?, ?)", result.WordpressId, result.Username, result.Email, result.IsSuccess) + return err +} + +func GetEditor(username string, db *sql.DB) (EditorResult, error) { + rows, err := db.Query("select WordpressId, Username, Email, (IsSuccess = b'1') from EditorsResults where Username = ?", username) + if err != nil { + fmt.Println(err) + } + + defer rows.Close() + + for rows.Next() { + editor := EditorResult{} + + err := rows.Scan(&editor.WordpressId, &editor.Username, &editor.Email, &editor.IsSuccess) + + if err != nil { + fmt.Println(err) + } + + if editor.Username == username { + return editor, nil + } + } + + return EditorResult{}, errors.New("Could not find editor.") +} + +func EditorHasBeenMigrated(username string, db *sql.DB) bool { + _, err := GetEditor(username, db) + + if err == nil { + return true + } else { + return false + } +} diff --git a/services/migration/migrate-authors.go b/services/migration/migrate-authors.go index 753000b..bf7f618 100644 --- a/services/migration/migrate-authors.go +++ b/services/migration/migrate-authors.go @@ -1 +1,50 @@ package migration + +import ( + "database/sql" + "federated.computer/wp-sync-slowtwitch/services/slowtwitch" + "federated.computer/wp-sync-slowtwitch/services/wordpress" + "fmt" +) + +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: editor.Username, + Email: editor.Email, + Password: editor.Password, + Roles: "editor", + } + + result := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + editorResult := EditorResult{ + Username: result.Username, + Email: result.Username, + WordpressId: result.Id, + IsSuccess: true, + } + err := CreateEditorResult(editorResult, migration.ResultsDatabase) + if err != nil { + fmt.Println(err) + } + output = append(output, editorResult) + fmt.Println("Created user:", result.Username) + } + } + + return output +} diff --git a/services/slowtwitch/get-users.go b/services/slowtwitch/get-users.go new file mode 100644 index 0000000..bd8c28b --- /dev/null +++ b/services/slowtwitch/get-users.go @@ -0,0 +1,36 @@ +package slowtwitch + +import ( + "database/sql" + "fmt" +) + +type SlowtwitchUser struct { + Username string + Password string + Email string +} + +func GetUsers(db *sql.DB) []SlowtwitchUser { + rows, err := db.Query("select Username, Password, Email from glinks_Users;") + + if err != nil { + fmt.Println(err) + } + + defer rows.Close() + + var users []SlowtwitchUser + + for rows.Next() { + user := SlowtwitchUser{} + err := rows.Scan(&user.Username, &user.Password, &user.Email) + + if err != nil { + fmt.Println(err) + } + users = append(users, user) + } + + return users +} diff --git a/services/wordpress/create-user.go b/services/wordpress/create-user.go index 16251b2..4fcf4e9 100644 --- a/services/wordpress/create-user.go +++ b/services/wordpress/create-user.go @@ -6,13 +6,10 @@ import ( ) type CreateUser struct { - Username string `json:"username"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - Name string `json:"name"` - Email string `json:"email"` - Roles string `json:"roles"` - Password string `json:"password"` + Username string `json:"username"` + Email string `json:"email"` + Roles string `json:"roles"` + Password string `json:"password"` } type CreateUserResponse struct {