aboutsummaryrefslogtreecommitdiff
path: root/routes/node.go
diff options
context:
space:
mode:
Diffstat (limited to 'routes/node.go')
-rw-r--r--routes/node.go75
1 files changed, 75 insertions, 0 deletions
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))
+}