about summary refs log tree commit diff
path: root/src/db.go
diff options
context:
space:
mode:
authorhanemile <mail@emile.space>2020-07-19 13:15:31 +0200
committerhanemile <mail@emile.space>2020-07-19 13:15:31 +0200
commit50500b9e8eb0b891f598e66801f5dcca55d1186e (patch)
tree5f07d91b90d95cc1634bccdb5a53aaae0d9fff9f /src/db.go
parent0773cb78d1e2ae090602e5c89e7951b16dbeb21c (diff)
src update
Diffstat (limited to 'src/db.go')
-rw-r--r--src/db.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/db.go b/src/db.go
new file mode 100644
index 0000000..713d8e6
--- /dev/null
+++ b/src/db.go
@@ -0,0 +1,84 @@
+package main
+
+import (
+	"fmt"
+	"math/rand"
+	"time"
+
+	"git.darknebu.la/chaosdorf/freitagsfoo/src/db"
+	"git.darknebu.la/chaosdorf/freitagsfoo/src/structs"
+	pg "github.com/go-pg/pg/v9"
+	"github.com/google/uuid"
+	"github.com/sirupsen/logrus"
+)
+
+// initialize the DB by first deleting all the tables and then creating the
+// tables again
+func initDB(pgdb *pg.DB) {
+	deleteAllTables(pgdb)
+	createAllTables(pgdb)
+
+	createSomeTalks(pgdb)
+}
+
+// delete all tables in the database
+func deleteAllTables(pgdb *pg.DB) {
+	err := db.DeleteAllTables(pgdb)
+	if err != nil {
+		logrus.Fatalf("Could not delete the database schema: %s", err)
+
+	}
+}
+
+// create the schema
+func createAllTables(pgdb *pg.DB) {
+	err := db.CreateSchema(pgdb)
+	if err != nil {
+		logrus.Fatalf("Could not create the database schema: %s", err)
+	}
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// This section is just there to insert stuff into the database for testing
+////////////////////////////////////////////////////////////////////////////////
+
+func createSomeTalks(pgdb *pg.DB) {
+
+	rand.Seed(time.Now().UnixNano())
+	nicknames := []string{
+		"Thorin",
+		"Fili",
+		"Kili",
+		"Balin",
+		"Dwalin",
+		"Oin",
+		"Gloin",
+		"Dori",
+		"Nori",
+		"Ori",
+		"Bifur",
+		"Bofur",
+		"Bombur",
+	}
+	for i := 0; i < 10; i++ {
+		date := time.Now().Add(time.Duration(i) * 7 * 24 * time.Hour)
+
+		layout := "2006-01-02"
+		formattedDate := date.Format(layout)
+
+		talk := &structs.Talk{
+			UUID:          uuid.New(),
+			Title:         fmt.Sprintf("Wie baut man eigentlich Raketen ohne Brennstoff nr. %d", i),
+			Description:   fmt.Sprintf("Sunt rerum illo corrupti. Similique qui rem debitis. Accusamus et rerum sint et amet eos nemo. Et enim omnis et. Tempora et corrupti aut ea et vel. \n Dolor est quae sed molestiae nisi esse aliquid atque. Voluptas vero et ducimus voluptatem in eaque. Quo illum et delectus vel sed molestias quidem. Consequuntur unde dolores quis sunt exercitationem eos et provident. Animi eaque temporibus alias. %d", i),
+			Slides:        "./uploads/black.png",
+			Nickname:      nicknames[rand.Intn(len(nicknames))],
+			Date:          time.Now(),
+			FormattedDate: formattedDate,
+			Upcoming:      true,
+		}
+		err := db.InsertTalk(pgdb, talk)
+		if err != nil {
+			logrus.Fatalf("Could not insert talk into the database: %s", err)
+		}
+	}
+}