DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: cbbeec42f7cede004eeabda047e8dccb04f3a552 tixe/handlers/index.go -rw-r--r-- 1.2 KiB
cbbeec42Jonni Liljamo docs: update README 10 months 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
/*
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */
package handlers

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

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

type linksData struct {
	Links []types.Link
}

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

	var links []types.Link
	rows, _ := db.PgPool.Query(context.Background(),
		`SELECT id, visual, link FROM links
			WHERE user_id = $1
			  ORDER BY id DESC`, user.Id)
	for rows.Next() {
		var id, visual, linkstr string
		err := rows.Scan(&id, &visual, &linkstr)
		if err != nil {
			// FIXME:
			log.Printf("[tixe/handlers] ERROR: Failed to scan a row when querying for links: %v", err)
			continue
		}
		// FIXME:
		links = append(links, types.Link { Id: id, Visual: visual, Link: linkstr })
	}

	data := linksData {
		Links: links,
	}

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