Browse Source

Listing 15: adding comments - create form.

Frederic G. MARAND 8 years ago
parent
commit
c5878c602d
2 changed files with 38 additions and 30 deletions
  1. 6 2
      public/scripts/tutorial.js
  2. 32 28
      server.go

+ 6 - 2
public/scripts/tutorial.js

@@ -36,8 +36,12 @@ let CommentForm = React.createClass({
   render: function () {
     return (
       <div className="commentForm">
-        Hello, world! I am a CommentForm.
-        </div>
+        <form className="commentForm">
+          <input type="text" placeholder="Your name" />
+          <input type="text" placeholder="Say something..." />
+          <input type="submit" value="Post" />
+        </form>
+      </div>
     );
   }
 });

+ 32 - 28
server.go

@@ -35,6 +35,36 @@ const dataFile = "./comments.json"
 
 var commentMutex = new(sync.Mutex)
 
+func updateComments(w http.ResponseWriter, r *http.Request, commentData []byte, fi os.FileInfo) {
+	// Decode the JSON data
+	var comments []comment
+	if err := json.Unmarshal(commentData, &comments); err != nil {
+		http.Error(w, fmt.Sprintf("Unable to Unmarshal comments from data file (%s): %s", dataFile, err), http.StatusInternalServerError)
+		return
+	}
+
+	// Add a new comment to the in memory slice of comments
+	comments = append(comments, comment{ID: time.Now().UnixNano() / 1000000, Author: r.FormValue("author"), Text: r.FormValue("text")})
+
+	// Marshal the comments to indented json.
+	commentData, err := json.MarshalIndent(comments, "", "    ")
+	if err != nil {
+		http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError)
+		return
+	}
+
+	// Write out the comments to the file, preserving permissions.
+	err = ioutil.WriteFile(dataFile, commentData, fi.Mode())
+	if err != nil {
+		http.Error(w, fmt.Sprintf("Unable to write comments to data file (%s): %s", dataFile, err), http.StatusInternalServerError)
+		return
+	}
+
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Cache-Control", "no-cache")
+	io.Copy(w, bytes.NewReader(commentData))
+}
+
 // Handle comments
 func handleComments(w http.ResponseWriter, r *http.Request) {
 	// Since multiple requests could come in at once, ensure we have a lock
@@ -58,33 +88,7 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
 
 	switch r.Method {
 	case "POST":
-		// Decode the JSON data
-		var comments []comment
-		if err := json.Unmarshal(commentData, &comments); err != nil {
-			http.Error(w, fmt.Sprintf("Unable to Unmarshal comments from data file (%s): %s", dataFile, err), http.StatusInternalServerError)
-			return
-		}
-
-		// Add a new comment to the in memory slice of comments
-		comments = append(comments, comment{ID: time.Now().UnixNano() / 1000000, Author: r.FormValue("author"), Text: r.FormValue("text")})
-
-		// Marshal the comments to indented json.
-		commentData, err = json.MarshalIndent(comments, "", "    ")
-		if err != nil {
-			http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError)
-			return
-		}
-
-		// Write out the comments to the file, preserving permissions
-		err := ioutil.WriteFile(dataFile, commentData, fi.Mode())
-		if err != nil {
-			http.Error(w, fmt.Sprintf("Unable to write comments to data file (%s): %s", dataFile, err), http.StatusInternalServerError)
-			return
-		}
-
-		w.Header().Set("Content-Type", "application/json")
-		w.Header().Set("Cache-Control", "no-cache")
-		io.Copy(w, bytes.NewReader(commentData))
+		updateComments(w, r, commentData, fi);
 
 	case "GET":
 		w.Header().Set("Content-Type", "application/json")
@@ -93,7 +97,7 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
 		io.Copy(w, bytes.NewReader(commentData))
 
 	default:
-		// Don't know the method, so error
+		// Don't know the method, so error.
 		http.Error(w, fmt.Sprintf("Unsupported method: %s", r.Method), http.StatusMethodNotAllowed)
 	}
 }