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