Pārlūkot izejas kodu

Add movies to lists

Gildas Chabot 4 gadi atpakaļ
vecāks
revīzija
3176b39d32
2 mainītis faili ar 44 papildinājumiem un 3 dzēšanām
  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
 		if r.Method == "POST" {
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
 		b, err := json.MarshalIndent(l, "", "  ")
170
 		b, err := json.MarshalIndent(l, "", "  ")
@@ -167,12 +172,20 @@ func List(c *movies.Collection) http.HandlerFunc {
167
 			errs = append(errs, err)
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
 		t.Execute(w, map[string]interface{}{
182
 		t.Execute(w, map[string]interface{}{
171
 			"List": l,
183
 			"List": l,
172
 			"Description": template.HTML(
184
 			"Description": template.HTML(
173
 				blackfriday.Run(
185
 				blackfriday.Run(
174
 					[]byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
186
 					[]byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
175
 					blackfriday.WithExtensions(blackfriday.CommonExtensions))),
187
 					blackfriday.WithExtensions(blackfriday.CommonExtensions))),
188
+			"Movies":   ms,
176
 			"ListJSON": string(b),
189
 			"ListJSON": string(b),
177
 			"Errors":   errs,
190
 			"Errors":   errs,
178
 		})
191
 		})

+ 28 - 0
templates/list.html

@@ -14,11 +14,39 @@
14
 
14
 
15
         {{ .Description }}
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
         <form method="post">
39
         <form method="post">
18
             <p>Update list: <input type="submit" /><br />
40
             <p>Update list: <input type="submit" /><br />
19
                 <input type="text" name="title" value="{{ .List.Title }}" /><br />
41
                 <input type="text" name="title" value="{{ .List.Title }}" /><br />
20
                 <textarea name="description" style="width:90%;height:500px;">{{ .List.Description }}</textarea>
42
                 <textarea name="description" style="width:90%;height:500px;">{{ .List.Description }}</textarea>
21
             </p>
43
             </p>
22
         </form>
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
     </body>
51
     </body>
24
 </html>
52
 </html>