DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 587179c2f07f6dde69d0a2bc867ecf10efdd28ce tixe/api/links.go -rw-r--r-- 3.2 KiB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * 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 api

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

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/oklog/ulid/v2"
)

type postLinksNew struct {
	Visual string `form:"visual"`
	Link   string `form:"link"`
}

func LinkNew(c *gin.Context) {
	data := &postLinksNew{}
	if err := c.Bind(data); err != nil {
		log.Printf("[tixe/api] ERROR: Could not bind new link data: %v", err)
		c.String(http.StatusBadRequest, "Could not bind new link data")
		return;
	}

	session := sessions.Default(c)
	user := session.Get("user").(types.User)

	linkId := ulid.Make().String()

	_, err := db.PgPool.Exec(context.Background(),
		"INSERT INTO links(id, user_id, visual, link) VALUES($1, $2, $3, $4)",
		linkId, user.Id, data.Visual, data.Link)
	if err != nil {
		log.Printf("[tixe/api] ERROR: Could not create new link entry in database: %v", err)
		c.String(http.StatusInternalServerError, "Could not create new link entry in database!")
		return;
	}

	c.Redirect(http.StatusFound, "/")
}

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

	linkId := c.Param("id")

	_, err := db.PgPool.Exec(context.Background(),
		"DELETE FROM links WHERE id = $1 AND user_id = $2", linkId, user.Id)
	if err != nil {
		errStr := "Could not delete link entry from database"
		log.Printf("[tixe/api] ERROR: %s: %v", errStr, err)
		c.String(http.StatusInternalServerError, errStr)
		return;
	}

	c.Redirect(http.StatusFound, "/")
}

type postVisual struct {
	Visual string `form:"visual"`
}

func LinkUpdateVisual(c *gin.Context) {
	data := &postVisual{}
	if err := c.Bind(data); err != nil {
		errStr := "Could not bind link visual update data"
		log.Printf("[tixe/api] ERROR: %s: %v", errStr, err)
		c.String(http.StatusBadRequest, errStr)
		return;
	}

	session := sessions.Default(c)
	user := session.Get("user").(types.User)

	linkId := c.Param("id")

	_, err := db.PgPool.Exec(context.Background(),
		"UPDATE links SET visual = $1 WHERE id = $2 AND user_id = $3", data.Visual, linkId, user.Id)
	if err != nil {
		errStr := "Could not update link visual in database"
		log.Printf("[tixe/api] ERROR: %s: %v", errStr, err)
		c.String(http.StatusInternalServerError, errStr)
		return;
	}

	c.Redirect(http.StatusFound, "/link/" + linkId)
}

type postLink struct {
	Link string `form:"link"`
}

func LinkUpdateLink(c *gin.Context) {
	data := &postLink{}
	if err := c.Bind(data); err != nil {
		errStr := "Could not bind link 'link' update data"
		log.Printf("[tixe/api] ERROR: %s: %v", errStr, err)
		c.String(http.StatusBadRequest, errStr)
		return;
	}

	session := sessions.Default(c)
	user := session.Get("user").(types.User)

	linkId := c.Param("id")

	_, err := db.PgPool.Exec(context.Background(),
		"UPDATE links SET link = $1 WHERE id = $2 AND user_id = $3", data.Link, linkId, user.Id)
	if err != nil {
		errStr := "Could not update link 'link' in database"
		log.Printf("[tixe/api] ERROR: %s: %v", errStr, err)
		c.String(http.StatusInternalServerError, errStr)
		return;
	}

	c.Redirect(http.StatusFound, "/link/" + linkId)
}