/*
* 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 postTagsNew struct {
Tag string `form:"tag"`
}
func TagsNew(c *gin.Context) {
data := &postTagsNew{}
if err := c.Bind(data); err != nil {
log.Printf("[tixe/api] ERROR: Could not bind new tag data: %v", err)
c.String(http.StatusBadRequest, "Could not bind new tag data")
return;
}
session := sessions.Default(c)
user := session.Get("user").(types.User)
tagId := ulid.Make().String()
_, err := db.PgPool.Exec(context.Background(),
"INSERT INTO tags(id, user_id, tag) VALUES($1, $2, $3)", tagId, user.Id, data.Tag)
if err != nil {
log.Printf("[tixe/api] ERROR: Could not create new tag entry in database: %v", err)
c.String(http.StatusInternalServerError, "Could not create new tag entry in database!")
return;
}
c.Redirect(http.StatusFound, "/tags")
}
type postTagsDelete struct {
Id string `form:"id"`
}
func TagsDelete(c *gin.Context) {
data := &postTagsDelete{}
if err := c.Bind(data); err != nil {
log.Printf("[tixe/api] ERROR: Could not bind delete tag data: %v", err)
c.String(http.StatusBadRequest, "Could not bind delete tag data")
return;
}
session := sessions.Default(c)
user := session.Get("user").(types.User)
_, err := db.PgPool.Exec(context.Background(),
"DELETE FROM tags WHERE id = $1 AND user_id = $2", data.Id, user.Id)
if err != nil {
log.Printf("[tixe/api] ERROR: Could not delete tag entry from database: %v", err)
c.String(http.StatusInternalServerError, "Could not delete tag entry from database!")
return;
}
c.Redirect(http.StatusFound, "/tags")
}