From 00ba765dc892738272f907183dc74f6b022fe39a Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Tue, 14 May 2024 15:59:04 -0600 Subject: [PATCH] get slowtwitch categories --- main.go | 28 +++++++++---------- services/slowtwitch/get-category.go | 40 ++++++++++++++++++++++++++++ services/slowtwitch/slowtwitch-db.go | 26 ++++++++++++++++++ 3 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 services/slowtwitch/get-category.go create mode 100644 services/slowtwitch/slowtwitch-db.go diff --git a/main.go b/main.go index 82d4c13..26c00d3 100644 --- a/main.go +++ b/main.go @@ -1,35 +1,33 @@ package main import ( - "database/sql" + "federated.computer/wp-sync-slowtwitch/services/slowtwitch" "fmt" - _ "github.com/go-sql-driver/mysql" ) const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/" const wordpressKey = "admin" const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" +const slowtwitchAdminUser = "admin" +const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv" +const slowtwitchDbUrl = "slowtwitch.northend.network" +const slowtwitchDbPort = "3306" +const slowtwitchDbName = "slowtwitch" + var appCache AppCache func main() { - // TODO Create Category with or without parent + add to cache // TODO Use cached data to correctly build post form submissions + slowtwitchDB, err := slowtwitch.Connect(slowtwitchAdminUser, slowtwitchAdminPass, slowtwitchDbUrl, slowtwitchDbPort, slowtwitchDbName) -} - -func connectToMariaDB() (*sql.DB, error) { - db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/mydb") if err != nil { - return nil, err + fmt.Println(err) } - // Ping the MariaDB server to ensure connectivity - err = db.Ping() - if err != nil { - return nil, err - } + categories := slowtwitch.GetCategories(slowtwitchDB) - fmt.Println("Connected to MariaDB!") - return db, nil + for _, category := range categories { + fmt.Println(category.FullName) + } } diff --git a/services/slowtwitch/get-category.go b/services/slowtwitch/get-category.go new file mode 100644 index 0000000..3648eac --- /dev/null +++ b/services/slowtwitch/get-category.go @@ -0,0 +1,40 @@ +package slowtwitch + +import ( + "database/sql" + "fmt" +) + +type SlowtwitchCategory struct { + Id int + Name string + FullName string + FatherId int + CatDepth int + NumberOfLinks int +} + +func GetCategories(db *sql.DB) []SlowtwitchCategory { + rows, err := db.Query("select ID, Name, Full_Name, FatherID, CatDepth, Number_of_Links from glinks_Category;") + + if err != nil { + fmt.Println(err) + } + + defer rows.Close() + + var categories []SlowtwitchCategory + + for rows.Next() { + category := SlowtwitchCategory{} + err := rows.Scan(&category.Id, &category.Name, &category.FullName, &category.FatherId, &category.CatDepth, &category.NumberOfLinks) + + if err != nil { + fmt.Println(err) + } + + categories = append(categories, category) + } + + return categories +} diff --git a/services/slowtwitch/slowtwitch-db.go b/services/slowtwitch/slowtwitch-db.go new file mode 100644 index 0000000..b3e1b25 --- /dev/null +++ b/services/slowtwitch/slowtwitch-db.go @@ -0,0 +1,26 @@ +package slowtwitch + +import ( + "database/sql" + "fmt" + _ "github.com/go-sql-driver/mysql" +) + +func Connect(user, pass, url, port, dbName string) (*sql.DB, error) { + connectionString := user + ":" + pass + "@tcp(" + url + ":" + port + ")/" + dbName + + db, err := sql.Open("mysql", connectionString) + + if err != nil { + return nil, err + } + + err = db.Ping() + if err != nil { + return nil, err + } + + fmt.Println("Successfully connected!") + + return db, nil +}