DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 587179c2f07f6dde69d0a2bc867ecf10efdd28ce tixe/handlers/link.go -rw-r--r-- 980 bytes
587179c2Jonni Liljamo fix: verify users existence in IsAuthenticated 1 year, 1 month 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
/*
 * 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"
	"net/http"
	"tixe/db"
	"tixe/template"
	"tixe/types"
	"tixe/util"

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

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

	linkId := c.Param("id")

	var link types.Link
	err := db.PgPool.QueryRow(context.Background(),
		"SELECT visual, link FROM links WHERE id = $1 AND user_id = $2",
			linkId, user.Id).Scan(&link.Visual, &link.Link)
	if err != nil {
		errStr := "Could not query link entry from database"
		util.RenderError(c, "db error", errStr, user)
		return
	}

	link.Id = linkId

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