Datenbank Script erstellt, mit Events angefangen

This commit is contained in:
xoy 2023-01-28 18:21:21 +01:00
parent ea5e59b45d
commit 189fc4c5f2
4 changed files with 58 additions and 0 deletions

28
database.go Normal file
View File

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

20
events.go Normal file
View File

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

View File

@ -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\"")) 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/")) output = strings.ReplaceAll(output, "!NEWBANNER", htmlNewBanner("Rundgang", "https://www.chaostreff-dortmund.de/rundgang/"))
return output return output

View File

@ -8,6 +8,7 @@ import (
) )
func main() { func main() {
println(len(getEvents()))
addr := ":80" addr := ":80"
handler() handler()