about summary refs log tree commit diff
path: root/src/db.go
blob: dfd509320d8301fd3015fef795c8850ff80c843a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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(-3 * 7 * 24 * time.Hour).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:          date,
			FormattedDate: formattedDate,
			Upcoming:      true,
		}
		err := db.InsertTalk(pgdb, talk)
		if err != nil {
			logrus.Fatalf("Could not insert talk into the database: %s", err)
		}
	}
}