浏览代码

Add files to movies

Gildas Chabot 4 年之前
父节点
当前提交
4d72c98c35
共有 3 个文件被更改,包括 51 次插入11 次删除
  1. 2 0
      movies.go
  2. 27 11
      pages/pages.go
  3. 22 0
      templates/movie.html

+ 2 - 0
movies.go

@@ -16,6 +16,8 @@ type Movie struct {
16
 	Year     string
16
 	Year     string
17
 	Runtime  string
17
 	Runtime  string
18
 
18
 
19
+	Files []string
20
+
19
 	OMDB OMDBMovie
21
 	OMDB OMDBMovie
20
 }
22
 }
21
 
23
 

+ 27 - 11
pages/pages.go

@@ -113,26 +113,42 @@ func Movie(c *movies.Collection) http.HandlerFunc {
113
 		}
113
 		}
114
 
114
 
115
 		if r.Method == "POST" {
115
 		if r.Method == "POST" {
116
-			updated, err := movies.Unmarshal([]byte(r.FormValue("movie_json")))
117
-			if err != nil {
118
-				errs = append(errs, err)
119
-			} else if imdbID != updated.IMDBID {
120
-				errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
121
-			} else {
122
-				m = updated
123
-				c.Update(updated)
116
+			if movieJSON := r.FormValue("movie_json"); movieJSON != "" {
117
+				updated, err := movies.Unmarshal([]byte(movieJSON))
118
+				if err != nil {
119
+					errs = append(errs, err)
120
+				} else if imdbID != updated.IMDBID {
121
+					errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
122
+				} else {
123
+					m = updated
124
+					c.Update(updated)
125
+				}
126
+			}
127
+
128
+			if files := r.FormValue("files"); files != "" {
129
+				m.Files = strings.Split(files, "\n")
130
+				c.Update(m)
124
 			}
131
 			}
125
 		}
132
 		}
126
 
133
 
134
+		fileQuery := r.URL.Query().Get("file_query")
135
+		var fileResults string
136
+		if fileQuery != "" {
137
+			fileResults = strings.Join(c.SearchFiles(fileQuery), "\n")
138
+		}
139
+
127
 		b, err := json.MarshalIndent(m, "", "  ")
140
 		b, err := json.MarshalIndent(m, "", "  ")
128
 		if err != nil {
141
 		if err != nil {
129
 			errs = append(errs, err)
142
 			errs = append(errs, err)
130
 		}
143
 		}
131
 
144
 
132
 		t.Execute(w, map[string]interface{}{
145
 		t.Execute(w, map[string]interface{}{
133
-			"Movie":     m,
134
-			"MovieJSON": string(b),
135
-			"Errors":    errs,
146
+			"Movie":       m,
147
+			"MovieJSON":   string(b),
148
+			"Files":       strings.Join(m.Files, "\n"),
149
+			"FileQuery":   fileQuery,
150
+			"FileResults": fileResults,
151
+			"Errors":      errs,
136
 		})
152
 		})
137
 	})
153
 	})
138
 }
154
 }

+ 22 - 0
templates/movie.html

@@ -18,6 +18,28 @@
18
             <div>{{ .Movie.Director }}</div>
18
             <div>{{ .Movie.Director }}</div>
19
         </div>
19
         </div>
20
 
20
 
21
+        <ul class="file-list">
22
+            {{ range $f := .Movie.Files }}
23
+            <li>{{ $f }}</li>
24
+            {{ end }}
25
+        </ul>
26
+
27
+        <div>
28
+            <form method="post">
29
+                <p>Update file list: <input type="submit" /><br />
30
+                    <textarea name="files" style="width:500px;height:140px;">{{ .Files }}</textarea>
31
+                </p>
32
+            </form>
33
+
34
+            <form method="get">
35
+                <p>
36
+                    Search by filename: <input type="text" name="file_query" value="{{ .FileQuery }}" />
37
+                    <input type="submit" />
38
+                </p>
39
+            </form>
40
+            <textarea style="width:500px;height:140px;">{{ .FileResults }}</textarea>
41
+        </div>
42
+
21
         <form method="post">
43
         <form method="post">
22
             <p>Update movie: <input type="submit" /><br />
44
             <p>Update movie: <input type="submit" /><br />
23
                 <textarea name="movie_json" style="width:90%;height:500px;">{{ .MovieJSON }}</textarea>
45
                 <textarea name="movie_json" style="width:90%;height:500px;">{{ .MovieJSON }}</textarea>