Browse Source

Add files to movies

Gildas Chabot 4 years ago
parent
commit
4d72c98c35
3 changed files with 51 additions and 11 deletions
  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 16
 	Year     string
17 17
 	Runtime  string
18 18
 
19
+	Files []string
20
+
19 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 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 140
 		b, err := json.MarshalIndent(m, "", "  ")
128 141
 		if err != nil {
129 142
 			errs = append(errs, err)
130 143
 		}
131 144
 
132 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 18
             <div>{{ .Movie.Director }}</div>
19 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 43
         <form method="post">
22 44
             <p>Update movie: <input type="submit" /><br />
23 45
                 <textarea name="movie_json" style="width:90%;height:500px;">{{ .MovieJSON }}</textarea>