aboutsummaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'controllers')
-rw-r--r--controllers/pages.go34
-rw-r--r--controllers/post.go51
2 files changed, 85 insertions, 0 deletions
diff --git a/controllers/pages.go b/controllers/pages.go
new file mode 100644
index 0000000..c5c5306
--- /dev/null
+++ b/controllers/pages.go
@@ -0,0 +1,34 @@
+package controllers
+
+import (
+ "paperchan.club/database"
+ "paperchan.club/models"
+ "github.com/gofiber/fiber/v2"
+ "log"
+)
+
+func ThreadList(c *fiber.Ctx) error {
+ var threads []models.Thread
+ db := database.DB
+ if err := db.Select(&threads, "SELECT a.*, (SELECT COUNT(*) FROM \"post\" AS b WHERE b.thread = a.id) AS replies FROM \"post\" AS \"a\" WHERE \"thread\" IS NULL ORDER BY (SELECT c.created_at FROM \"post\" AS c WHERE c.thread = a.id OR c.id = a.id ORDER BY c.created_at DESC LIMIT 1) DESC"); err != nil {
+ log.Println(err)
+ return c.Status(500).SendString("ERROR")
+ }
+ return c.Render("index", fiber.Map{
+ "posts": threads,
+ })
+}
+
+func Thread(c *fiber.Ctx) error {
+ id := c.Params("id")
+ var posts []models.Post
+ db := database.DB
+ if err := db.Select(&posts, "SELECT * FROM \"post\" WHERE id = $1 OR thread = $1 ORDER BY created_at ASC", id); err != nil {
+ log.Println(err)
+ return c.Status(500).SendString("ERROR")
+ }
+ return c.Render("thread", fiber.Map{
+ "threadId": id,
+ "posts": posts,
+ })
+}
diff --git a/controllers/post.go b/controllers/post.go
new file mode 100644
index 0000000..4ec49c4
--- /dev/null
+++ b/controllers/post.go
@@ -0,0 +1,51 @@
+package controllers
+
+import (
+ "log"
+ "paperchan.club/database"
+ "paperchan.club/themagicpipe"
+ "database/sql"
+ "github.com/gofiber/fiber/v2"
+ "strconv"
+)
+
+// data received by /api/post
+type PostApi struct {
+ Picture string `json:"picture" xml:"picture" form:"picture"`
+ Thread string `json:"thread" xml:"thread" form:"thread"`
+}
+
+func Publish(c *fiber.Ctx) error {
+ p := new(PostApi)
+ if err := c.BodyParser(p); err != nil {
+ return c.JSON(fiber.Map{
+ "status": "error",
+ })
+ }
+ picture := p.Picture
+ var thread sql.NullInt32
+ if parsed, err := strconv.ParseInt(p.Thread, 10, 32); err != nil {
+ thread.Valid = false
+ } else {
+ thread.Int32 = int32(parsed)
+ thread.Valid = true
+ }
+ ip := c.IP()
+ fixedPic, err := themagicpipe.DataURLConverter(picture)
+ if err != nil {
+ return c.JSON(fiber.Map{
+ "status": "error",
+ })
+ }
+ db := database.DB
+ if _, err := db.Exec("INSERT INTO \"post\" (picture, ip_address, thread) VALUES ($1, $2, $3)", fixedPic, ip, thread); err == nil {
+ return c.JSON(fiber.Map{
+ "status": "ok",
+ })
+ } else {
+ log.Println(err)
+ return c.JSON(fiber.Map{
+ "status": "database error",
+ })
+ }
+}