Browse Source

Add new movie from OMDB JSON on the list page

Gildas Chabot 4 years ago
parent
commit
2c77b26c28
2 changed files with 24 additions and 0 deletions
  1. 11 0
      pages/pages.go
  2. 13 0
      templates/list.html

+ 11 - 0
pages/pages.go

@@ -16,6 +16,17 @@ func List(c *movies.Collection) http.HandlerFunc {
16 16
 			return
17 17
 		}
18 18
 
19
+		if r.Method == "POST" {
20
+			m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
21
+			if err != nil {
22
+				w.WriteHeader(http.StatusBadRequest)
23
+				fmt.Println(err)
24
+				return
25
+			}
26
+
27
+			c.Add(m)
28
+		}
29
+
19 30
 		t.Execute(w, c)
20 31
 	})
21 32
 }

+ 13 - 0
templates/list.html

@@ -58,5 +58,18 @@
58 58
             </li>
59 59
             {{ end }}
60 60
         </ul>
61
+
62
+        <h2>Add a movie</h2>
63
+        <div>
64
+            <form method="get">
65
+                <p>Search by title: <input type="text" name="title" /> <input type="submit" /></p>
66
+            </form>
67
+            <form method="post">
68
+                <p>Add OMBD JSON movie:<br />
69
+                    <textarea name="omdb_json" style="width:500px;height:140px;"></textarea>
70
+                    <input type="submit" />
71
+                </p>
72
+            </form>
73
+        </div>
61 74
     </body>
62 75
 </html>