From 66b9dfe2182167ed67b8f7e9fdf8250b2dc5a60b Mon Sep 17 00:00:00 2001 From: xoy Date: Sat, 28 Jan 2023 18:21:21 +0100 Subject: [PATCH] Datenbank Script erstellt, mit Events angefangen --- database.go | 28 ++++++++++++++++++++++++++++ events.go | 20 ++++++++++++++++++++ func.go | 9 +++++++++ main.go | 1 + 4 files changed, 58 insertions(+) create mode 100644 database.go create mode 100644 events.go diff --git a/database.go b/database.go new file mode 100644 index 0000000..e62cabf --- /dev/null +++ b/database.go @@ -0,0 +1,28 @@ +package main + +import ( + "database/sql" +) + +func dbConnect(username string, password string, address string, port string, database string) *sql.DB { + db, err := sql.Open("mysql", username+":"+password+"@tcp("+address+":"+port+")/"+database) + + if err != nil { + panic(err) + } + + return db +} + +func dbClose(database *sql.DB) { + database.Close() +} + +func dbQuerry(database *sql.DB, sqlCode string) *sql.Rows { + results, err := database.Query("SELECT id, name FROM tags") + if err != nil { + panic(err) + } + + return results +} diff --git a/events.go b/events.go new file mode 100644 index 0000000..ed8112f --- /dev/null +++ b/events.go @@ -0,0 +1,20 @@ +package main + +func getEvents() []event { + db := dbConnect("root", "", "localhost", "3306", "ctdo") + + rows := dbQuerry(db, "SELECT * FROM events;") + + events := []event{} + + for rows.Next() { + Event := new(event) + err := rows.Scan(&Event.date, &Event.description, &Event.media, &Event.title) + if err != nil { + panic(err) + } + events = append(events, *Event) + } + + return events +} diff --git a/func.go b/func.go index d34b8b8..7a815c6 100644 --- a/func.go +++ b/func.go @@ -134,6 +134,15 @@ func htmlReplacer(input string, activePage string) string { output = strings.ReplaceAll(output, "!TOPICTREFF", htmlElement("h3", "Nächster Topictreff findet in "+string(getNextTopic().days)+" Tagen statt!", "class=\"topic\"")+htmlElement("p", "Am "+getNextTopic().date, "class=\"topic\"")) } + events := getEvents() + + if len(events) == 0 { + output = strings.ReplaceAll(output, "!EVENTS", htmlNewBanner("Rundgang", "https://www.chaostreff-dortmund.de/rundgang/")) + } else { + + output = strings.ReplaceAll(output, "!EVENTS", htmlNewBanner("Rundgang", "https://www.chaostreff-dortmund.de/rundgang/")) + } + output = strings.ReplaceAll(output, "!NEWBANNER", htmlNewBanner("Rundgang", "https://www.chaostreff-dortmund.de/rundgang/")) return output diff --git a/main.go b/main.go index 0ab12e4..d5d6f10 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( ) func main() { + println(len(getEvents())) addr := ":80" handler()