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