Browse Source

Add movies to lists

Gildas Chabot 4 years ago
parent
commit
3176b39d32
2 changed files with 44 additions and 3 deletions
  1. 16 3
      pages/pages.go
  2. 28 0
      templates/list.html

+ 16 - 3
pages/pages.go

@@ -157,9 +157,14 @@ func List(c *movies.Collection) http.HandlerFunc {
157 157
 		}
158 158
 
159 159
 		if r.Method == "POST" {
160
-			l.Title = r.FormValue("title")
161
-			l.Description = r.FormValue("description")
162
-			c.UpdateList(l)
160
+			if r.FormValue("title") != "" || r.FormValue("description") != "" {
161
+				l.Title = r.FormValue("title")
162
+				l.Description = r.FormValue("description")
163
+				c.UpdateList(l)
164
+			} else if r.FormValue("imdb_id") != "" {
165
+				l.Movies = append(l.Movies, r.FormValue("imdb_id"))
166
+				c.UpdateList(l)
167
+			}
163 168
 		}
164 169
 
165 170
 		b, err := json.MarshalIndent(l, "", "  ")
@@ -167,12 +172,20 @@ func List(c *movies.Collection) http.HandlerFunc {
167 172
 			errs = append(errs, err)
168 173
 		}
169 174
 
175
+		var ms []*movies.Movie
176
+		for _, imdbID := range l.Movies {
177
+			if m, ok := c.Get(imdbID); ok {
178
+				ms = append(ms, m)
179
+			}
180
+		}
181
+
170 182
 		t.Execute(w, map[string]interface{}{
171 183
 			"List": l,
172 184
 			"Description": template.HTML(
173 185
 				blackfriday.Run(
174 186
 					[]byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
175 187
 					blackfriday.WithExtensions(blackfriday.CommonExtensions))),
188
+			"Movies":   ms,
176 189
 			"ListJSON": string(b),
177 190
 			"Errors":   errs,
178 191
 		})

+ 28 - 0
templates/list.html

@@ -14,11 +14,39 @@
14 14
 
15 15
         {{ .Description }}
16 16
 
17
+        <ul class="movie-list">
18
+            {{ range $m := .Movies }}
19
+            <a href="/{{ $m.IMDBID }}">
20
+                <li class="movie">
21
+                    <div class="poster">
22
+                        <img src="{{ $m.Poster }}" title="{{ $m.Title }}" alt="{{ $m.Title }}" />
23
+                    </div>
24
+                    <div class="title">
25
+                        <h3>{{ $m.Title }}</h3>
26
+                        <div>{{ $m.Director }}</div>
27
+                    </div>
28
+                    <ul class="metadata">
29
+                        <li>{{ $m.Year }}</li>
30
+                        <li style="flex-grow: 2;">{{ $m.Country }}</li>
31
+                        <li>{{ $m.Runtime }}</li>
32
+                    </ul>
33
+                </li>
34
+            </a>
35
+            {{ end }}
36
+        </ul>
37
+
38
+
17 39
         <form method="post">
18 40
             <p>Update list: <input type="submit" /><br />
19 41
                 <input type="text" name="title" value="{{ .List.Title }}" /><br />
20 42
                 <textarea name="description" style="width:90%;height:500px;">{{ .List.Description }}</textarea>
21 43
             </p>
22 44
         </form>
45
+
46
+        <form method="post">
47
+            <p>Add movie (by id): <input type="submit" /><br />
48
+                <input type="text" name="imdb_id" value="" />
49
+            </p>
50
+        </form>
23 51
     </body>
24 52
 </html>