DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: fff9260842462f4b7bbc4b889ae11eb141ae5143 tixe/handlers/tags.go -rw-r--r-- 1.5 KiB
fff92608Jonni Liljamo docs: remove old comment 1 year, 20 days ago
                                                                                
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
package handlers

import (
	"context"
	"log"
	"net/http"
	"tixe/db"
	"tixe/template"
	"tixe/types"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

type tag struct {
	Id        string
	Tag       string
	TimesUsed uint
}

type tagsData struct {
	Tags []tag
}

func Tags(c *gin.Context) {
	session := sessions.Default(c)
	user := session.Get("user").(types.User)

	var tags []tag
	// Query tags which the user has defined
	// TODO: Query also the amount of times the tag is used, meaning join
	// with the links table too.
	rows, _ := db.PgPool.Query(context.Background(),
		`SELECT id, tag FROM tags
			WHERE user_id = $1`, user.Id)
	for rows.Next() {
		var id string
		var tagstr string
		err := rows.Scan(&id, &tagstr)
		if err != nil {
			// FIXME: user doesn't currently know if this happens (though it shouldn't (tm))
			// something something handle me better
			log.Printf("[tixe/handlers] ERROR: Failed to scan a row when querying for tags: %v", err)
			continue
		}
		// FIXME: There has to be a better way to do this, right?
		// Like, not appending to an array, but some other other data structure
		// completely, or somehow we should get the amount of rows first, so
		// we can set the array values, instead of doing this append...
		tags = append(tags, tag { Id: id, Tag: tagstr, TimesUsed: 0})
	}

	data := tagsData {
		Tags: tags,
	}

	html := template.TmplEngine.Render("tags.tmpl", map[string]interface{}{"title": "tags", "user": user, "data": data})
	c.Data(http.StatusOK, "text/html", html)
}