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 ThreadHandler 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 *ThreadHandler) List() http.HandlerFunc {
|
|
|
|
type data struct {
|
2021-08-29 17:43:45 +02:00
|
|
|
SessionData
|
|
|
|
|
2021-08-29 16:37:20 +02:00
|
|
|
Threads []goddit.Thread
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/threads.html"))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tt, err := h.store.Threads()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:43:45 +02:00
|
|
|
tmpl.Execute(w, data{
|
|
|
|
SessionData: GetSessionData(h.sessions, r.Context()),
|
|
|
|
Threads: tt,
|
|
|
|
})
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ThreadHandler) Create() http.HandlerFunc {
|
2021-08-29 16:55:23 +02:00
|
|
|
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
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/thread_create.html"))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
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),
|
2021-08-29 16:55:23 +02:00
|
|
|
})
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ThreadHandler) 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
|
|
|
|
Posts []goddit.Post
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/thread.html"))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
idStr := chi.URLParam(r, "id")
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t, err := h.store.Thread(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pp, err := h.store.PostsByThread(t.ID)
|
|
|
|
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,
|
|
|
|
Posts: pp,
|
2021-08-29 16:55:23 +02:00
|
|
|
})
|
2021-08-29 16:37:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ThreadHandler) Store() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := r.FormValue("title")
|
|
|
|
description := r.FormValue("description")
|
|
|
|
|
|
|
|
if err := h.store.CreateThread(&goddit.Thread{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Title: title,
|
|
|
|
Description: description,
|
|
|
|
}); 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 new thread has been created.")
|
|
|
|
|
2021-08-29 16:37:20 +02:00
|
|
|
http.Redirect(w, r, "/threads", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ThreadHandler) Delete() http.HandlerFunc {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.store.DeleteThread(id); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:43:45 +02:00
|
|
|
h.sessions.Put(r.Context(), "flash", "The thread has been deleted.")
|
|
|
|
|
2021-08-29 16:37:20 +02:00
|
|
|
http.Redirect(w, r, "/threads", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|