aboutsummaryrefslogtreecommitdiff
path: root/modules/grils/grils.go
blob: fbe57e3e9320659f7faff44ceab7e60eedfd9f07 (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
98
99
100
101
102
103
package grils

import (
	"fagott.pw/charakterin"
	"fagott.pw/grilist/frontend"
	"fagott.pw/grilist/grilist"
	"fmt"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"strconv"
	"time"
)

type GrilsModule struct {
	g *grilist.Grilist
}

func (m *GrilsModule) Name() string {
	return "Grils"
}

func (m *GrilsModule) Init(g *grilist.Grilist) {
	m.g = g
	m.g.Router.GET("/gril/:id", m.viewGril)
}

func (m *GrilsModule) Interface() interface{} {
	return m
}

func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
	var categories []grilist.DashboardCategory

	var grils []*Gril

	// TODO
	// NUR TEMPORÄR
	g, _ := m.FromID(1)
	grils = append(grils, g)

	cat := grilist.DashboardCategory{
		Title: "Neue Grils",
	}

	for _, gril := range grils {
		cat.Cards = append(cat.Cards, frontend.Card{
			Title:       gril.RomajiName,
			Description: gril.KanjiName,
			Size:        "medium",
			Actions: []frontend.Action{
				frontend.Action{
					Name: "anguckieren",
					Link: fmt.Sprintf("/gril/%d", gril.ID),
				},
			},
		})
	}

	categories = append(categories, cat)
	return categories
}

func (m *GrilsModule) FromID(id int) (*Gril, error) {
	return &Gril{
		ID:         id,
		KanjiName:  "藤林 杏",
		RomajiName: "Kyou Fujibayashi",
		OtherNames: []string{"ふじばやし きょう"},
		Age:        17,
		Birthday:   time.Now(),
		Tags:       []string{"tsundere", "hair intakes", "hair ribbon", "school uniform", "school crest"},
	}, nil
}

func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	loggedIn := false
	if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil {
		loggedIn = true
	}
	sid := p.ByName("id")

	id, err := strconv.Atoi(sid)
	if err != nil {
		http.Redirect(w, r, "/", 302)
		return
	}

	gril, err := m.FromID(id)
	if err != nil {
		http.Redirect(w, r, "/", 302)
		return
	}

	data := make(map[string]interface{})
	data["loggedIn"] = loggedIn
	data["gril"] = gril

	m.g.Renderer.RenderPage("gril", w, data)
}

func New() *GrilsModule {
	return &GrilsModule{}
}