2021-08-29 16:37:20 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.snrd.de/Spaenny/goddit"
|
2021-08-29 17:09:48 +02:00
|
|
|
"github.com/alexedwards/scs/v2"
|
2021-08-29 16:37:20 +02:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/google/uuid"
|
2021-08-29 16:55:23 +02:00
|
|
|
"github.com/gorilla/csrf"
|
2021-08-29 16:37:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type PostHandler struct {
|
2021-08-29 17:09:48 +02:00
|
|
|
store goddit.Store
|
|
|
|
sessions *scs.SessionManager
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PostHandler) Create() http.HandlerFunc {
|
|
|
|
type data struct {
|
2021-08-29 17:43:45 +02:00
|
|
|
SessionData
|
|
|
|
|
2021-08-29 16:55:23 +02:00
|
|
|
CSRF template.HTML
|
2021-08-29 16:37:20 +02:00
|
|
|
Thread goddit.Thread
|
|
|
|
}
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/post_create.html"))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
idStr := chi.URLParam(r, "id")
|
|
|
|
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := h.store.Thread(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:55:23 +02:00
|
|
|
tmpl.Execute(w, data{
|
2021-08-29 17:43:45 +02:00
|
|
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
|
|
|
CSRF: csrf.TemplateField(r),
|
|
|
|
Thread: t,
|
2021-08-29 16:55:23 +02:00
|
|
|
})
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PostHandler) Show() http.HandlerFunc {
|
|
|
|
type data struct {
|
2021-08-29 17:43:45 +02:00
|
|
|
SessionData
|
2021-08-29 16:55:23 +02:00
|
|
|
CSRF template.HTML
|
2021-08-29 16:37:20 +02:00
|
|
|
Thread goddit.Thread
|
|
|
|
Post goddit.Post
|
|
|
|
Comments []goddit.Comment
|
|
|
|
}
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/post.html"))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
postIDStr := chi.URLParam(r, "postID")
|
|
|
|
threadIDStr := chi.URLParam(r, "threadID")
|
|
|
|
|
|
|
|
postID, err := uuid.Parse(postIDStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threadID, err := uuid.Parse(threadIDStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := h.store.Post(postID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, err := h.store.CommentsByPost(p.ID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := h.store.Thread(threadID)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:55:23 +02:00
|
|
|
tmpl.Execute(w, data{
|
2021-08-29 17:43:45 +02:00
|
|
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
|
|
|
CSRF: csrf.TemplateField(r),
|
|
|
|
Thread: t,
|
|
|
|
Post: p,
|
|
|
|
Comments: cc,
|
2021-08-29 16:55:23 +02:00
|
|
|
})
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PostHandler) Store() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2021-08-29 18:22:08 +02:00
|
|
|
form := CreatePostForm{
|
|
|
|
Title: r.FormValue("title"),
|
|
|
|
Content: r.FormValue("content"),
|
|
|
|
}
|
|
|
|
if !form.Validate() {
|
|
|
|
h.sessions.Put(r.Context(), "form", form)
|
|
|
|
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2021-08-29 16:37:20 +02:00
|
|
|
|
|
|
|
idStr := chi.URLParam(r, "id")
|
|
|
|
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := h.store.Thread(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &goddit.Post{
|
|
|
|
ID: uuid.New(),
|
|
|
|
ThreadID: t.ID,
|
2021-08-29 18:22:08 +02:00
|
|
|
Title: form.Title,
|
|
|
|
Content: form.Content,
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.store.CreatePost(p); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:43:45 +02:00
|
|
|
h.sessions.Put(r.Context(), "flash", "Your Post has been created.")
|
|
|
|
|
2021-08-29 16:37:20 +02:00
|
|
|
http.Redirect(w, r, "/threads/"+t.ID.String()+"/"+p.ID.String(), http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PostHandler) Vote() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
idStr := chi.URLParam(r, "postID")
|
|
|
|
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := h.store.Post(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := r.URL.Query().Get("dir")
|
|
|
|
if dir == "up" {
|
|
|
|
p.Votes++
|
|
|
|
} else if dir == "down" {
|
|
|
|
p.Votes--
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.store.UpdatePost(&p); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|