From ce0a2b123603edcde4e17eacc02bafeec85f74ab Mon Sep 17 00:00:00 2001 From: vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:38:23 +0200 Subject: delete api for moderation --- controllers/moderation.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 controllers/moderation.go (limited to 'controllers/moderation.go') diff --git a/controllers/moderation.go b/controllers/moderation.go new file mode 100644 index 0000000..0313798 --- /dev/null +++ b/controllers/moderation.go @@ -0,0 +1,51 @@ +package controllers + +import ( + "log" + "os" + "paperchan.club/database" + "github.com/gofiber/fiber/v2" + "golang.org/x/crypto/bcrypt" + "strconv" +) + +// data received by DELETE /api/post +type DeleteApi struct { + Id string `json:"id" xml:"id" form:"id"` + Pass string `json:"pass" xml:"pass" form:"pass"` //moderation password defined in .env +} +func Delete(c *fiber.Ctx) error { + d := new(DeleteApi) + if err := c.BodyParser(d); err != nil { + log.Println(err) + return c.JSON(fiber.Map{ + "status": "error", + }) + } + id, err := strconv.ParseInt(d.Id, 10, 32) + if err != nil { + log.Println(err) + return c.JSON(fiber.Map{ + "status": "error", + }) + } + if bcrypt.CompareHashAndPassword([]byte(os.Getenv("MODPASS")),[]byte(d.Pass)) != nil { + log.Println(os.Getenv("MODPASS")) + log.Println(err) + return c.JSON(fiber.Map{ + "status": "forbidden", + }) + } else { + db := database.DB + if _, err := db.Exec("DELETE FROM \"post\" WHERE id = $1", id); err == nil { + return c.JSON(fiber.Map{ + "status": "ok", + }) + } else { + log.Println(err) + return c.JSON(fiber.Map{ + "status": "database error", + }) + } + } +} -- cgit v1.2.3