Browse Source

Support "N/A" in Note and Votes

Gildas Chabot 4 years ago
parent
commit
07659d1608
3 changed files with 8 additions and 15 deletions
  1. 5 11
      movies.go
  2. 2 3
      pages/pages.go
  3. 1 1
      templates/list.html

+ 5 - 11
movies.go

@@ -2,7 +2,6 @@ package movies
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"fmt"
6 5
 	"strconv"
7 6
 	"strings"
8 7
 )
@@ -86,19 +85,14 @@ func (m *Movie) FillFromOMDB() error {
86 85
 	}
87 86
 
88 87
 	if m.Rating == nil {
89
-		note, err := strconv.ParseFloat(m.OMDB.IMDBRating, 64)
90
-		if err != nil {
91
-			return fmt.Errorf("cannot convert %q to float64: %w", m.OMDB.IMDBRating, err)
92
-		}
88
+		m.Rating = &Rating{}
93 89
 
94
-		votes, err := strconv.ParseInt(strings.ReplaceAll(m.OMDB.IMDBVotes, ",", ""), 10, 64)
95
-		if err != nil {
96
-			return fmt.Errorf("cannot convert %q to int: %w", m.OMDB.IMDBVotes, err)
90
+		if note, err := strconv.ParseFloat(m.OMDB.IMDBRating, 64); err == nil {
91
+			m.Rating.Note = note
97 92
 		}
98 93
 
99
-		m.Rating = &Rating{
100
-			Note:  note,
101
-			Votes: votes,
94
+		if votes, err := strconv.ParseInt(strings.ReplaceAll(m.OMDB.IMDBVotes, ",", ""), 10, 64); err == nil {
95
+			m.Rating.Votes = votes
102 96
 		}
103 97
 	}
104 98
 

+ 2 - 3
pages/pages.go

@@ -24,10 +24,9 @@ func List(c *movies.Collection) http.HandlerFunc {
24 24
 			if err != nil {
25 25
 				w.WriteHeader(http.StatusBadRequest)
26 26
 				errs = append(errs, err)
27
-				return
27
+			} else {
28
+				c.Add(m)
28 29
 			}
29
-
30
-			c.Add(m)
31 30
 		}
32 31
 
33 32
 		var omdbString string

+ 1 - 1
templates/list.html

@@ -71,7 +71,7 @@
71 71
                     <input type="submit" />
72 72
                 </p>
73 73
             </form>
74
-            <form method="post">
74
+            <form method="post" action="/">
75 75
                 <p>Add OMBD JSON movie:<br />
76 76
                     <textarea name="omdb_json" style="width:500px;height:140px;">{{ .OMDBString }}</textarea>
77 77
                     <input type="submit" />