about summary refs log tree commit diff
path: root/src/db/talk.go
blob: c286aa83d3a4732592d048668393ffc7c406eee7 (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
package db

import (
	"fmt"

	"git.darknebu.la/chaosdorf/freitagsfoo/src/structs"
	pg "github.com/go-pg/pg/v9"
	"github.com/google/uuid"
)

// InsertTalk inserts the given talk into the database
func InsertTalk(db *pg.DB, talk *structs.Talk) error {
	err := db.Insert(talk)
	if err != nil {
		return fmt.Errorf("could not insert talk into the db: %s", err)
	}

	return nil
}

// UpcomingTalksLimited returns the next 3 upcoming talks
func UpcomingTalksLimited(db *pg.DB) ([]structs.Talk, error) {
	var talks []structs.Talk
	err := db.Model(&talks).Order("id DESC").Limit(3).Select()
	if err != nil {
		return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err)
	}

	return talks, nil
}

// UpcomingTalks returns the next upcoming talks
func UpcomingTalks(db *pg.DB) ([]structs.Talk, error) {
	var talks []structs.Talk
	err := db.Model(&talks).Order("id DESC").Select()
	if err != nil {
		return []structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err)
	}

	return talks, nil
}

// CountUpcomingTalks counts the amount of talks upcoming
func CountUpcomingTalks(db *pg.DB) (int, error) {

	var talks []structs.Talk
	count, err := db.Model(&talks).Where("upcoming = ?", true).SelectAndCount()
	if err != nil {
		return -1, fmt.Errorf("could not get the talks from the db: %s", err)
	}

	return count, nil
}

// TalkByUUID returns the talk with the given UUID
func TalkByUUID(db *pg.DB, uuidString string) (structs.Talk, error) {
	parsedUUID, err := uuid.Parse(uuidString)
	if err != nil {
		return structs.Talk{}, fmt.Errorf("could not parse the UUID: %s", err)
	}

	var talk structs.Talk
	err = db.Model(&talk).Where("uuid = ?", parsedUUID).Select()
	if err != nil {
		return structs.Talk{}, fmt.Errorf("could not get the talks from the db: %s", err)
	}

	return talk, nil
}