get slowtwitch categories

This commit is contained in:
Ross Trottier 2024-05-14 15:59:04 -06:00
parent f4e7e3446c
commit 00ba765dc8
3 changed files with 79 additions and 15 deletions

28
main.go
View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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
}