DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 7746581cf2c2201816d744bc5c3ae1d1e5dc1ede tixe/handlers/link.go -rw-r--r-- 935 bytes
7746581cJonni Liljamo feat: link management! 11 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
/*
 * 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"

	"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 {
		// something something show error page and abort i guess
		c.Abort()
		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)
}