From e9a64f3b41b5eae47dec7c0ecfd1caae83136abc Mon Sep 17 00:00:00 2001 From: Alexander NeonXP Kiryukhin Date: Sat, 20 Jul 2024 21:56:37 +0300 Subject: initial --- routes/node.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 routes/node.go (limited to 'routes/node.go') diff --git a/routes/node.go b/routes/node.go new file mode 100644 index 0000000..c754b9a --- /dev/null +++ b/routes/node.go @@ -0,0 +1,75 @@ +package routes + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/labstack/echo/v4" + "gitrepo.ru/neonxp/gorum/contextlib" + "gitrepo.ru/neonxp/gorum/models" + "gitrepo.ru/neonxp/gorum/utils" + "gitrepo.ru/neonxp/gorum/views" +) + +func (r *Router) Node(c echo.Context) error { + sParentID := c.Param("id") + parentID := 0 + var err error + if sParentID != "" { + parentID, err = strconv.Atoi(sParentID) + if err != nil { + return err + } + } + + node := &models.Node{ + ID: 0, + Text: "Gorum", + } + if parentID > 0 { + node, err = r.nodeRepo.Get(c.Request().Context(), parentID) + if err != nil { + return err + } + } + + nodes, count, err := r.nodeRepo.List(c.Request().Context(), parentID) + if err != nil { + return err + } + + return utils.Render(c, views.Node(node, nodes, count)) +} + +func (r *Router) NewPost(c echo.Context) error { + req := new(nodeRequest) + if err := c.Bind(req); err != nil { + return err + } + user := contextlib.GetUser(c.Request().Context()) + if user == nil { + return echo.ErrForbidden + } + sParentID := c.Param("id") + parentID, err := strconv.Atoi(sParentID) + if err != nil { + return err + } + + if c.Request().Method == http.MethodPost { + postID, err := r.nodeRepo.Create(c.Request().Context(), req.Type, req.Text, user.ID, parentID) + if err != nil { + return err + } + + return c.Redirect(302, fmt.Sprintf("/n/%d#post%d", parentID, postID)) + } + + node, err := r.nodeRepo.Get(c.Request().Context(), parentID) + if err != nil { + return err + } + + return utils.Render(c, views.NewNode(node)) +} -- cgit v1.2.3