/*
* 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)
}