|
|
@@ -14,6 +14,7 @@ import (
|
|
14
|
14
|
func Router(c *movies.Collection) http.HandlerFunc {
|
|
15
|
15
|
homeHandler := Home(c)
|
|
16
|
16
|
movieHandler := Movie(c)
|
|
|
17
|
+ listHandler := List(c)
|
|
17
|
18
|
|
|
18
|
19
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
19
|
20
|
path := r.URL.Path
|
|
|
@@ -24,6 +25,9 @@ func Router(c *movies.Collection) http.HandlerFunc {
|
|
24
|
25
|
case path == "/style.css":
|
|
25
|
26
|
http.ServeFile(w, r, "templates/style.css")
|
|
26
|
27
|
return
|
|
|
28
|
+ case strings.HasPrefix(path, "/l/"):
|
|
|
29
|
+ listHandler(w, r)
|
|
|
30
|
+ return
|
|
27
|
31
|
case strings.HasPrefix(path, "/tt"):
|
|
28
|
32
|
movieHandler(w, r)
|
|
29
|
33
|
return
|
|
|
@@ -42,12 +46,20 @@ func Home(c *movies.Collection) http.HandlerFunc {
|
|
42
|
46
|
var errs []error
|
|
43
|
47
|
|
|
44
|
48
|
if r.Method == "POST" {
|
|
45
|
|
- m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
|
|
46
|
|
- if err != nil {
|
|
47
|
|
- w.WriteHeader(http.StatusBadRequest)
|
|
48
|
|
- errs = append(errs, err)
|
|
49
|
|
- } else {
|
|
50
|
|
- c.Add(m)
|
|
|
49
|
+ if r.FormValue("omdb_json") != "" {
|
|
|
50
|
+ m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
|
|
|
51
|
+ if err != nil {
|
|
|
52
|
+ w.WriteHeader(http.StatusBadRequest)
|
|
|
53
|
+ errs = append(errs, err)
|
|
|
54
|
+ } else {
|
|
|
55
|
+ c.Add(m)
|
|
|
56
|
+ }
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ if r.FormValue("list-id") != "" {
|
|
|
60
|
+ if err := c.AddList(r.FormValue("list-id"), r.FormValue("list-title")); err != nil {
|
|
|
61
|
+ errs = append(errs, err)
|
|
|
62
|
+ }
|
|
51
|
63
|
}
|
|
52
|
64
|
}
|
|
53
|
65
|
|
|
|
@@ -118,3 +130,52 @@ func Movie(c *movies.Collection) http.HandlerFunc {
|
|
118
|
130
|
})
|
|
119
|
131
|
})
|
|
120
|
132
|
}
|
|
|
133
|
+
|
|
|
134
|
+func List(c *movies.Collection) http.HandlerFunc {
|
|
|
135
|
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
136
|
+ t, err := template.ParseFiles("templates/list.html")
|
|
|
137
|
+ if err != nil {
|
|
|
138
|
+ fmt.Println(err)
|
|
|
139
|
+ return
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ var errs []error
|
|
|
143
|
+
|
|
|
144
|
+ splitted := strings.Split(r.URL.Path, "/")
|
|
|
145
|
+ if len(splitted) < 3 {
|
|
|
146
|
+ w.WriteHeader(http.StatusNotFound)
|
|
|
147
|
+ return
|
|
|
148
|
+ }
|
|
|
149
|
+
|
|
|
150
|
+ listID := splitted[2]
|
|
|
151
|
+
|
|
|
152
|
+ l, ok := c.GetList(listID)
|
|
|
153
|
+ if !ok {
|
|
|
154
|
+ w.WriteHeader(http.StatusNotFound)
|
|
|
155
|
+ return
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ if r.Method == "POST" {
|
|
|
159
|
+ updated, err := movies.UnmarshalList([]byte(r.FormValue("list_json")))
|
|
|
160
|
+ if err != nil {
|
|
|
161
|
+ errs = append(errs, err)
|
|
|
162
|
+ } else if listID != updated.ID {
|
|
|
163
|
+ errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
|
|
|
164
|
+ } else {
|
|
|
165
|
+ l = updated
|
|
|
166
|
+ c.UpdateList(updated)
|
|
|
167
|
+ }
|
|
|
168
|
+ }
|
|
|
169
|
+
|
|
|
170
|
+ b, err := json.MarshalIndent(l, "", " ")
|
|
|
171
|
+ if err != nil {
|
|
|
172
|
+ errs = append(errs, err)
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ t.Execute(w, map[string]interface{}{
|
|
|
176
|
+ "List": l,
|
|
|
177
|
+ "ListJSON": string(b),
|
|
|
178
|
+ "Errors": errs,
|
|
|
179
|
+ })
|
|
|
180
|
+ })
|
|
|
181
|
+}
|