-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththread_handlers.go
110 lines (95 loc) · 2.63 KB
/
thread_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"log"
"net/http"
"text/template"
"github.com/SrijanSriv/gorum/data"
)
type ThreadsPosts struct {
Username string
Thread data.Thread
Post []data.Post
}
func readThread(w http.ResponseWriter, r *http.Request) {
uuid := r.URL.Query().Get("id")
stuff := ThreadsPosts{}
post := data.GetPosts(uuid)
thread, err := data.ThreadByUuid(uuid)
_, stuff.Username, _ = session(w, r)
stuff.Thread = thread
stuff.Post = post
if err != nil {
log.Fatal(err)
} else {
_, _, err := session(w, r)
var templates *template.Template
public_thread_files := []string{"templates/public.layout.html", "templates/public.navbar.html", "templates/public.thread.html"}
private_thread_files := []string{"templates/private.layout.html", "templates/private.navbar.html", "templates/private.thread.html"}
if err != nil {
// fmt.Println(stuff)
templates = template.Must(template.ParseFiles(public_thread_files...))
} else {
templates = template.Must(template.ParseFiles(private_thread_files...))
}
templates.Execute(w, stuff)
}
}
func newThread(w http.ResponseWriter, r *http.Request) {
_, _, err := session(w, r)
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
} else {
var templates *template.Template
thread_files := []string{"templates/private.layout.html", "templates/new.thread.html", "templates/private.navbar.html"}
templates = template.Must(template.ParseFiles(thread_files...))
templates.Execute(w, nil) /*add structs instead of nil*/
}
}
func createThread(w http.ResponseWriter, r *http.Request) {
sess, _, err := session(w, r)
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
} else {
err = r.ParseForm()
if err != nil {
log.Fatal(err)
}
user, err := data.UserByUuid(sess.Uuid)
if err != nil {
log.Fatal(err)
}
topic := r.PostFormValue("topic")
body := r.PostFormValue("body")
if err := data.CreateThread(topic, body, user.Name); err != nil {
log.Fatal(err)
}
http.Redirect(w, r, "/", http.StatusFound)
}
}
func postThread(w http.ResponseWriter, r *http.Request) {
sess, _, err := session(w, r)
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
} else {
err = r.ParseForm()
if err != nil {
log.Fatal(err)
}
user, err := data.UserByUuid(sess.Uuid)
if err != nil {
log.Fatal(err)
}
body := r.PostFormValue("body")
uuid := r.PostFormValue("uuid")
thread, err := data.ThreadByUuid(uuid)
if err != nil {
log.Fatal(err)
}
if _, err := user.CreatePost(thread, body); err != nil {
log.Fatal(err)
}
url := fmt.Sprint("/thread/read?id=", uuid)
http.Redirect(w, r, url, http.StatusFound)
}
}