From 1991f263226ded71dfe89ab1b83d2804c9e0fd70 Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Sat, 20 Jan 2024 20:08:47 +0100 Subject: [PATCH] Backend ws publishes for CUD --- backend/handlers/cardstags.go | 62 ++++++++++++++++- backend/handlers/filters.go | 60 +++++++++++++++- backend/handlers/tags.go | 124 ++++++++++++++++++++++++++++++++-- backend/handlers/views.go | 60 +++++++++++++++- 4 files changed, 291 insertions(+), 15 deletions(-) diff --git a/backend/handlers/cardstags.go b/backend/handlers/cardstags.go index 9b9e636..1079368 100644 --- a/backend/handlers/cardstags.go +++ b/backend/handlers/cardstags.go @@ -67,7 +67,24 @@ func CreateCardTag(c *fiber.Ctx) error { }) } - return c.SendStatus(fiber.StatusCreated) + err = c.SendStatus(fiber.StatusCreated) + if err != nil { + return err + } + + source := c.Get("X-Request-Source"); + if source == "" { + return nil; + } + + publish(fiber.Map{ + "object": "cardtag", + "action": "create", + "data": cardtag, + "X-Request-Source": source, + }); + + return nil } func GetCardTags(c *fiber.Ctx) error { @@ -143,7 +160,25 @@ func DeleteCardTag(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + + if err != nil { + return err + } + + source := c.Get("X-Request-Source"); + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "cardtag", + "action": "delete", + "id": cardID, + "X-Request-Source": source, + }); + + return nil } func DeleteCardTags(c *fiber.Ctx) error { @@ -172,6 +207,8 @@ func DeleteCardTags(c *fiber.Ctx) error { } return c.SendStatus(fiber.StatusNoContent) + + // TODO publish event } func UpdateCardTag(c *fiber.Ctx) error { @@ -224,5 +261,24 @@ func UpdateCardTag(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source"); + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "cardtag", + "action": "update", + "card_id": cardID, + "tag_id": tagID, + "changes": cardtag, + "X-Request-Source": source, + }) + + return nil } diff --git a/backend/handlers/filters.go b/backend/handlers/filters.go index 7102ea8..d0c3fd5 100644 --- a/backend/handlers/filters.go +++ b/backend/handlers/filters.go @@ -47,9 +47,28 @@ func CreateFilter(c *fiber.Ctx) error { }) } - return c.Status(fiber.StatusCreated).JSON(fiber.Map{ + err = c.Status(fiber.StatusCreated).JSON(fiber.Map{ "id": id, }) + if err != nil { + return err + } + + filter.ID = id + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "filter", + "action": "create", + "data": filter, + "X-Request-Source": source, + }) + + return nil } func GetFilter(c *fiber.Ctx) error { @@ -107,7 +126,25 @@ func UpdateFilter(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "filter", + "action": "update", + "id": id, + "changes": filter, + "X-Request-Source": source, + }) + + return nil } func DeleteFilter(c *fiber.Ctx) error { @@ -127,5 +164,22 @@ func DeleteFilter(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "filter", + "action": "delete", + "id": id, + "X-Request-Source": source, + }) + + return nil } \ No newline at end of file diff --git a/backend/handlers/tags.go b/backend/handlers/tags.go index 1d06c04..3b0653e 100644 --- a/backend/handlers/tags.go +++ b/backend/handlers/tags.go @@ -35,7 +35,26 @@ func CreateTag(c *fiber.Ctx) error { }) } - return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) + err = c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) + if err != nil { + return err + } + + tag.ID = id + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tag", + "action": "create", + "data": tag, + "X-Request-Source": source, + }) + + return nil } func GetTag(c *fiber.Ctx) error { @@ -76,7 +95,24 @@ func DeleteTag(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tag", + "action": "delete", + "id": id, + "X-Request-Source": source, + }) + + return nil } func UpdateTag(c *fiber.Ctx) error { @@ -102,7 +138,25 @@ func UpdateTag(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tag", + "action": "update", + "id": id, + "changes": tag, + "X-Request-Source": source, + }) + + return nil } func CreateTagOption(c *fiber.Ctx) error { @@ -135,7 +189,26 @@ func CreateTagOption(c *fiber.Ctx) error { }) } - return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) + err = c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) + if err != nil { + return err + } + + option.ID = id + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tagoption", + "action": "create", + "data": option, + "X-Request-Source": source, + }) + + return nil } func GetTagOptions(c *fiber.Ctx) error { @@ -200,7 +273,25 @@ func DeleteTagOption(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tagoption", + "action": "delete", + "tag_id": tagID, + "option_id": optionID, + "X-Request-Source": source, + }) + + return nil } func UpdateTagOption(c *fiber.Ctx) error { @@ -242,7 +333,26 @@ func UpdateTagOption(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "tagoption", + "action": "update", + "tag_id": tagID, + "option_id": optionID, + "changes": option, + "X-Request-Source": source, + }) + + return nil } func DeleteTagOptions(c *fiber.Ctx) error { @@ -271,4 +381,6 @@ func DeleteTagOptions(c *fiber.Ctx) error { } return c.SendStatus(fiber.StatusNoContent) + + // TODO: publish event } diff --git a/backend/handlers/views.go b/backend/handlers/views.go index cc27b7c..4daf10e 100644 --- a/backend/handlers/views.go +++ b/backend/handlers/views.go @@ -35,9 +35,28 @@ func CreateView(c *fiber.Ctx) error { }) } - return c.Status(fiber.StatusCreated).JSON(fiber.Map{ + err = c.Status(fiber.StatusCreated).JSON(fiber.Map{ "id": id, }) + if err != nil { + return err + } + + view.ID = id + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "view", + "action": "create", + "data": view, + "X-Request-Source": source, + }) + + return nil } func GetView(c *fiber.Ctx) error { @@ -85,7 +104,25 @@ func UpdateView(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "view", + "action": "update", + "id": id, + "changes": view, + "X-Request-Source": source, + }) + + return nil } func DeleteView(c *fiber.Ctx) error { @@ -106,7 +143,24 @@ func DeleteView(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } - return c.SendStatus(fiber.StatusNoContent) + err = c.SendStatus(fiber.StatusNoContent) + if err != nil { + return err + } + + source := c.Get("X-Request-Source") + if source == "" { + return nil + } + + publish(fiber.Map{ + "object": "view", + "action": "delete", + "id": id, + "X-Request-Source": source, + }) + + return nil } func GetViewFilters(c *fiber.Ctx) error {