aboutsummaryrefslogtreecommitdiff
path: root/controllers/post.go
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/post.go')
-rw-r--r--controllers/post.go42
1 files changed, 42 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))
+}