about summary refs log tree commit diff
path: root/src/db.go
blob: 1ec09413757a643e7e952ff41ff197ee844fbc80 (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
86
87
88
89
90
91
92
93
94
95
96
97
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++ {
<<<<<<< HEAD

		date := time.Now().Add(-3 * 7 * 24 * time.Hour).Add(time.Duration(i) * 7 * 24 * time.Hour)
=======
		date := time.Now().Add(time.Duration(i) * 7 * 24 * time.Hour)
>>>>>>> refs/remotes/origin/master

		layout := "2006-01-02"
		formattedDate := date.Format(layout)

		talk := &structs.Talk{
			UUID:          uuid.New(),
<<<<<<< HEAD
			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,
=======
			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(),
>>>>>>> refs/remotes/origin/master
			FormattedDate: formattedDate,
			Upcoming:      true,
		}
		err := db.InsertTalk(pgdb, talk)
		if err != nil {
			logrus.Fatalf("Could not insert talk into the database: %s", err)
		}
	}
}