aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/node.go75
-rw-r--r--routes/requests.go21
-rw-r--r--routes/routes.go17
-rw-r--r--routes/user.go85
4 files changed, 198 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))
+}
diff --git a/routes/requests.go b/routes/requests.go
new file mode 100644
index 0000000..a0bc13f
--- /dev/null
+++ b/routes/requests.go
@@ -0,0 +1,21 @@
+package routes
+
+import "gitrepo.ru/neonxp/gorum/models"
+
+type loginRequest struct {
+ Email string `form:"email"`
+ Password string `form:"password"`
+ Remember string `form:"remember"`
+}
+
+type registerRequest struct {
+ Username string `form:"username"`
+ Email string `form:"email"`
+ Password string `form:"password"`
+ Password2 string `form:"password2"`
+}
+
+type nodeRequest struct {
+ Type models.NodeType `form:"type"`
+ Text string `form:"text"`
+}
diff --git a/routes/routes.go b/routes/routes.go
new file mode 100644
index 0000000..835991a
--- /dev/null
+++ b/routes/routes.go
@@ -0,0 +1,17 @@
+package routes
+
+import (
+ "gitrepo.ru/neonxp/gorum/repository"
+)
+
+type Router struct {
+ userRepo *repository.User
+ nodeRepo *repository.Node
+}
+
+func NewRouter(userRepo *repository.User, nodeRepo *repository.Node) *Router {
+ return &Router{
+ userRepo: userRepo,
+ nodeRepo: nodeRepo,
+ }
+}
diff --git a/routes/user.go b/routes/user.go
new file mode 100644
index 0000000..95d5717
--- /dev/null
+++ b/routes/user.go
@@ -0,0 +1,85 @@
+package routes
+
+import (
+ "log"
+ "net/http"
+
+ "github.com/gorilla/sessions"
+ "github.com/labstack/echo-contrib/session"
+ "github.com/labstack/echo/v4"
+ "gitrepo.ru/neonxp/gorum/utils"
+ "gitrepo.ru/neonxp/gorum/views"
+)
+
+func (r *Router) Login(c echo.Context) error {
+ req := new(loginRequest)
+ if err := c.Bind(req); err != nil {
+ return err
+ }
+ if c.Request().Method == http.MethodPost {
+ u, err := r.userRepo.Login(c.Request().Context(), req.Email, req.Password)
+ if err != nil {
+ return err
+ }
+ sess, err := session.Get("session", c)
+ if err != nil {
+ return err
+ }
+ maxAge := 0
+ if req.Remember == "on" {
+ maxAge = 86400 * 14
+ }
+ sess.Options = &sessions.Options{
+ Path: "/",
+ MaxAge: maxAge,
+ HttpOnly: true,
+ }
+ sess.Values["user"] = *u
+ if err := sess.Save(c.Request(), c.Response()); err != nil {
+ return err
+ }
+
+ return c.Redirect(302, "/")
+ }
+
+ return utils.Render(c, views.Login(req.Email))
+}
+
+func (r *Router) Logout(c echo.Context) error {
+ sess, err := session.Get("session", c)
+ if err != nil {
+ return err
+ }
+ sess.Options = &sessions.Options{
+ Path: "/",
+ MaxAge: -1,
+ HttpOnly: true,
+ }
+ sess.Values["user"] = nil
+ if err := sess.Save(c.Request(), c.Response()); err != nil {
+ return err
+ }
+ if err := sess.Save(c.Request(), c.Response()); err != nil {
+ return err
+ }
+
+ return c.Redirect(302, "/")
+}
+
+func (r *Router) Register(c echo.Context) error {
+ req := new(registerRequest)
+ if err := c.Bind(req); err != nil {
+ return err
+ }
+ if c.Request().Method == http.MethodPost {
+ uid, err := r.userRepo.Create(c.Request().Context(), req.Email, req.Password, req.Username)
+ if err != nil {
+ return err
+ }
+ log.Println(uid)
+
+ return c.Redirect(302, "/login")
+ }
+
+ return utils.Render(c, views.Register(req.Username, req.Email))
+}