diff options
Diffstat (limited to 'routes/node.go')
-rw-r--r-- | routes/node.go | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/routes/node.go b/routes/node.go deleted file mode 100644 index e8aa455..0000000 --- a/routes/node.go +++ /dev/null @@ -1,129 +0,0 @@ -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, err := r.nodeRepo.List(c.Request().Context(), parentID) - if err != nil { - return err - } - - topics := make([]*models.Node, 0, len(nodes)) - posts := make([]*models.Node, 0, len(nodes)) - for _, n := range nodes { - switch n.Type { - case models.PostType: - posts = append(posts, n) - case models.TopicType: - topics = append(topics, n) - } - } - - return utils.Render(c, views.Node(node, topics, posts)) -} - -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("/t/%d#post%d", parentID, postID)) - } - - node := &models.Node{ - ID: 0, - Text: "Gorum", - } - if parentID > 0 { - node, err = r.nodeRepo.Get(c.Request().Context(), parentID) - if err != nil { - return err - } - } - - return utils.Render(c, views.NewPost(node)) -} -func (r *Router) NewTopic(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("/t/%d", postID)) - } - - node := &models.Node{ - ID: 0, - Text: "Gorum", - } - if parentID > 0 { - node, err = r.nodeRepo.Get(c.Request().Context(), parentID) - if err != nil { - return err - } - } - - return utils.Render(c, views.NewTopic(node)) -} |