aboutsummaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:47:35 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:47:35 +0300
commit96e2ce2e9d363a6296f9411ecb00168520258874 (patch)
tree09aa7fffe10eab84ae0edd39e570355984ba0148 /controllers
parent12ed72e4e1da181a6c87319a50d3b4142788b4c0 (diff)
Отказ от echo
Diffstat (limited to 'controllers')
-rw-r--r--controllers/post.go42
-rw-r--r--controllers/requests.go25
-rw-r--r--controllers/routes.go19
-rw-r--r--controllers/topic.go103
-rw-r--r--controllers/user.go59
5 files changed, 248 insertions, 0 deletions
diff --git a/controllers/post.go b/controllers/post.go
new file mode 100644
index 0000000..aee7c97
--- /dev/null
+++ b/controllers/post.go
@@ -0,0 +1,42 @@
+package controllers
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+
+ "gitrepo.ru/neonxp/gorum/contextlib"
+ "gitrepo.ru/neonxp/gorum/views"
+ "go.neonxp.ru/mux"
+)
+
+func (rt *Router) NewPost(w http.ResponseWriter, r *http.Request) {
+ req := new(postRequest)
+ if err := mux.Bind(r, req); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ user := contextlib.GetUser(r.Context())
+ if user == nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(errors.New("403")))
+ return
+ }
+
+ topic, err := rt.topicRepo.Get(uint64(req.Parent))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+
+ if r.Method == http.MethodPost {
+ postID, err := rt.postRepo.CreatePost(req.Text, user.Email, uint64(req.Parent))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ mux.Redirect(w, 302, fmt.Sprintf("/t/%d#post%d", req.Parent, postID))
+ return
+ }
+
+ mux.Render(w, r, views.NewPost(topic))
+}
diff --git a/controllers/requests.go b/controllers/requests.go
new file mode 100644
index 0000000..487b7cc
--- /dev/null
+++ b/controllers/requests.go
@@ -0,0 +1,25 @@
+package controllers
+
+type loginRequest struct {
+ Email string `form:"email"`
+ Password string `form:"password"`
+ Remember bool `form:"remember"`
+}
+
+type registerRequest struct {
+ Username string `form:"username"`
+ Email string `form:"email"`
+ Password string `form:"password"`
+ Password2 string `form:"password2"`
+}
+
+type topicRequest struct {
+ Parent int `form:"parent"`
+ Topic string `form:"topic"`
+ Text string `form:"text"`
+}
+
+type postRequest struct {
+ Parent int `form:"parent"`
+ Text string `form:"text"`
+}
diff --git a/controllers/routes.go b/controllers/routes.go
new file mode 100644
index 0000000..6e2ab9b
--- /dev/null
+++ b/controllers/routes.go
@@ -0,0 +1,19 @@
+package controllers
+
+import (
+ "gitrepo.ru/neonxp/gorum/repository"
+)
+
+type Router struct {
+ userRepo *repository.User
+ postRepo *repository.Post
+ topicRepo *repository.Topic
+}
+
+func NewRouter(userRepo *repository.User, nodeRepo *repository.Post, topicRepo *repository.Topic) *Router {
+ return &Router{
+ userRepo: userRepo,
+ postRepo: nodeRepo,
+ topicRepo: topicRepo,
+ }
+}
diff --git a/controllers/topic.go b/controllers/topic.go
new file mode 100644
index 0000000..50f65e7
--- /dev/null
+++ b/controllers/topic.go
@@ -0,0 +1,103 @@
+package controllers
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+ "strconv"
+
+ "gitrepo.ru/neonxp/gorum/contextlib"
+ "gitrepo.ru/neonxp/gorum/models"
+ "gitrepo.ru/neonxp/gorum/views"
+ "go.neonxp.ru/mux"
+)
+
+func (rt *Router) Index(w http.ResponseWriter, r *http.Request) {
+ topics, err := rt.topicRepo.List(0)
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+
+ mux.Render(w, r, views.Topics(topics))
+}
+
+func (rt *Router) Topic(w http.ResponseWriter, r *http.Request) {
+ sParentID := r.PathValue("id")
+ parentID, err := strconv.Atoi(sParentID)
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ topic, err := rt.topicRepo.Get(uint64(parentID))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+
+ topics, err := rt.topicRepo.List(uint64(parentID))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ posts, err := rt.postRepo.List(uint64(parentID))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ emailToUser := map[string]*models.User{}
+ for _, p := range posts {
+ u, ok := emailToUser[p.AuthorID]
+ if !ok {
+ u, err = rt.userRepo.Get(p.AuthorID)
+ if err != nil {
+ u = &models.User{Username: "user-not-found"}
+ }
+ }
+ emailToUser[p.AuthorID] = u
+ p.Author = u
+ }
+
+ mux.Render(w, r, views.Posts(topic, topics, posts))
+}
+
+func (rt *Router) NewTopic(w http.ResponseWriter, r *http.Request) {
+ req := new(topicRequest)
+ if err := mux.Bind(r, req); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ user := contextlib.GetUser(r.Context())
+ if user == nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(errors.New("403")))
+ return
+ }
+
+ if r.Method == http.MethodPost {
+ topic, err := rt.topicRepo.Create(req.Topic, req.Text, user.Email, uint64(req.Parent))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ mux.Redirect(w, 302, fmt.Sprintf("/t/%d", topic.ID))
+ return
+ }
+
+ topic := &models.Topic{
+ ID: 0,
+ Topic: "Корень",
+ }
+
+ sTopicID := r.PathValue("topic")
+ if sTopicID != "" {
+ if topicID, err := strconv.Atoi(sTopicID); err == nil {
+ topic, err = rt.topicRepo.Get(uint64(topicID))
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ }
+ }
+
+ mux.Render(w, r, views.NewTopic(topic))
+}
diff --git a/controllers/user.go b/controllers/user.go
new file mode 100644
index 0000000..a605c25
--- /dev/null
+++ b/controllers/user.go
@@ -0,0 +1,59 @@
+package controllers
+
+import (
+ "net/http"
+
+ "gitrepo.ru/neonxp/gorum/models"
+ "gitrepo.ru/neonxp/gorum/views"
+ "go.neonxp.ru/mux"
+ "go.neonxp.ru/mux/middleware/session"
+)
+
+func (rt *Router) Login(w http.ResponseWriter, r *http.Request) {
+ req := new(loginRequest)
+
+ if r.Method == http.MethodPost {
+ if err := mux.Bind(r, req); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ u, err := rt.userRepo.Login(req.Email, req.Password)
+ if err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ sess := session.FromRequest(r)
+
+ (*sess)["user"] = u
+
+ mux.Redirect(w, 302, "/")
+ return
+ }
+
+ mux.Render(w, r, views.Login(req.Email))
+}
+
+func (rt *Router) Logout(w http.ResponseWriter, r *http.Request) {
+ session.Clear(w, r)
+
+ mux.Redirect(w, 302, "/")
+}
+
+func (rt *Router) Register(w http.ResponseWriter, r *http.Request) {
+ req := new(registerRequest)
+
+ if err := mux.Bind(r, req); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ if r.Method == http.MethodPost {
+ if err := rt.userRepo.Create(req.Email, req.Password, req.Username, models.RoleUser); err != nil {
+ mux.Render(w, r, mux.DefaultErrorHandler(err))
+ return
+ }
+ mux.Redirect(w, 302, "/login")
+ return
+ }
+
+ mux.Render(w, r, views.Register(req.Username, req.Email))
+}