aboutsummaryrefslogtreecommitdiff
path: root/modules/grils/grils.go
blob: 047385e102449a6fb3c16edc7bf771f6d5fccd2f (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
package grils

import (
	"fagott.pw/charakterin"
	"fagott.pw/grilist/grilist"
	"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 {
	return []grilist.DashboardCategory{}
}

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{}
}