aboutsummaryrefslogtreecommitdiff
path: root/modules/lists/lists.go
blob: 4c4288f114035d47b17a3a037eec232dd2ddf85f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package lists

import (
	"database/sql"
	"errors"
	"fagott.pw/charakterin"
	"fagott.pw/grilist/frontend"
	"fagott.pw/grilist/grilist"
	"fagott.pw/grilist/modules/grils"
	"fmt"
	"github.com/julienschmidt/httprouter"
	"github.com/lib/pq"
	"log"
	"net/http"
	"strconv"
)

// Module und so.
type Module struct {
	g     *grilist.Grilist
	grils *grils.GrilsModule
}

// List ist eine Liste an DINGEN.
type List struct {
	ID          int
	Name        string
	Description string
	Owner       *charakterin.User
	ForkOf      sql.NullInt64
	UpdatedAt   pq.NullTime
	Grils       []*ListGril
}

// ListGril ist ein geranktes Gril
type ListGril struct {
	Gril  *grils.Gril
	Order int
}

// Name gibt den Namen des Moduls zurück
func (m *Module) Name() string {
	return "Lists"
}

// Init initialisiert das Modul
func (m *Module) Init(g *grilist.Grilist) {
	m.g = g

	gm, ok := g.Modules["Grils"]
	if !ok {
		log.Fatal("lists: grils module not found")
	}

	grilsModule, ok := gm.(*grils.GrilsModule)
	if !ok {
		log.Fatal("lists: error with grils module")
	}
	m.grils = grilsModule
	m.g.Router.GET("/list/:id", m.viewList)
}

func (m *Module) getListGrils(list *List) error {
	rows, err := m.g.DB.Query(`SELECT gril_id, "order" FROM grilist.lists_grils WHERE list_id = $1`, list.ID)
	if err != nil {
		return err
	}

	defer rows.Close()
	for rows.Next() {
		var grilID int
		lg := &ListGril{}

		if err := rows.Scan(&grilID, &lg.Order); err != nil {
			log.Println("error scanning row in getListGrils:", err)
			continue
		}

		gril, err := m.grils.FromID(grilID)
		if err != nil {
			log.Println("error getting listGril:", err)
			continue
		}
		lg.Gril = gril

		list.Grils = append(list.Grils, lg)
	}
	return nil
}

func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, error) {
	var lists []*List

	rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, name, description, fork_of, updated_at, user_id FROM grilist.lists WHERE %s`, whereClause), params...)
	if err != nil {
		return nil, err
	}

	defer rows.Close()
	for rows.Next() {
		list := &List{}
		var ownerID int
		if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt, &ownerID); err != nil {
			log.Println("error scanning row in getLists:", err)
			continue
		}

		if err := m.getListGrils(list); err != nil {
			log.Println(err)
			continue
		}

		// Owner kriegn
		owner, err := m.g.Charakterin.GetUserByID(ownerID)
		if err != nil {
			log.Println("error retreiving owner of list", err)
			continue
		}
		list.Owner = owner

		lists = append(lists, list)
	}

	return lists, nil
}

// GetUserLists gibt die Listen eines Benutzers zurück.
func (m *Module) GetUserLists(u *charakterin.User) []*List {
	lists, err := m.getLists(`user_id = $1`, u.ID)
	if err != nil {
		log.Println(err)
	}
	return lists
}

// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
func (m *Module) FromID(id int) (*List, error) {
	lists, err := m.getLists(`id = $1`, id)
	if err != nil {
		return nil, err
	}

	if len(lists) == 0 {
		return nil, errors.New("no list found")
	}
	return lists[0], nil
}

func listsToCards(lists []*List) []frontend.Card {
	var cards []frontend.Card

	for _, list := range lists {
		s := "s"
		if len(list.Grils) == 1 {
			s = ""
		}
		cards = append(cards, frontend.Card{
			Title:       list.Name,
			Description: list.Description,
			Size:        "medium",
			Actions: []frontend.Action{
				frontend.Action{
					Name: "anguckieren",
					Link: fmt.Sprintf("/list/%d", list.ID),
				},
				frontend.Action{
					Name:     fmt.Sprintf(`<span class="chip">%d gril%s</span>`, len(list.Grils), s),
					Link:     "#",
					Disabled: true,
				},
			},
		})
	}
	return cards
}

// ProvideDashboardData gibt Daten für das Dashboard bezogen auf den Benutzer zurück
func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
	var categories []grilist.DashboardCategory

	// Neue Listen
	lists, err := m.getLists(`1=1 ORDER BY id DESC LIMIT 5`)
	if err != nil {
		log.Println(err)
		return categories
	}

	categories = append(categories, grilist.DashboardCategory{
		Title: "Neueste Listen",
		Cards: listsToCards(lists),
	})

	if user == nil {
		return categories
	}

	// Listen des Benutzers
	lists, err = m.getLists(`user_id = $1`, user.ID)
	if err != nil {
		log.Println(err)
		return categories
	}

	categories = append(categories, grilist.DashboardCategory{
		Title: "Meine Listen",
		Cards: listsToCards(lists),
	})

	return categories
}

func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	user, _ := m.g.Charakterin.GetUserFromRequest(r)
	sid := p.ByName("id")

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

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

	data := m.g.Renderer.DefaultData()
	data["user"] = user
	data["list"] = list

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

// New erstellt eine neue Instanz des Modules
func New() *Module {
	return &Module{}
}