Jelajahi Sumber

Step 03.1: New form, GET.

Frédéric G. MARAND 6 bulan lalu
induk
melakukan
64eaea45f0

+ 22 - 2
contactsapp/app.go

@@ -48,6 +48,25 @@ func contacts(cs *ContactsStore) http.HandlerFunc {
 	}
 }
 
+/*
+@app.route("/contacts/new", methods=['GET']) (1)
+def contacts_new_get():
+
+	return render_template("new.html", contact=Contact()
+*/
+func contactsNewGet(cs *ContactsStore) http.HandlerFunc {
+	tpl := makeTemplate(
+		"layout",
+		"new",
+	)
+	return func(w http.ResponseWriter, r *http.Request) {
+		c := cs.New()
+		if err := tpl.ExecuteTemplate(w, "layout.html", c); err != nil {
+			http.Error(w, err.Error(), http.StatusInternalServerError)
+		}
+	}
+}
+
 func makeTemplate(first string, others ...string) *template.Template {
 	paths := append([]string{first}, others...)
 	for i, path := range paths {
@@ -63,8 +82,9 @@ func setupRoutes(mux *http.ServeMux, cs *ContactsStore) {
 	mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 		http.ServeFile(w, r, "./static/img/favicon.ico")
 	})
-	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
-	mux.Handle("/contacts", contacts(cs))
+	mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
+	mux.Handle("GET /contacts", contacts(cs))
+	mux.Handle("GET /contacts/new", contactsNewGet(cs))
 }
 
 func main() {

+ 7 - 1
contactsapp/contacts_model.go

@@ -97,11 +97,17 @@ func (cs *ContactsStore) Load() error {
 	return nil
 }
 
+func (*ContactsStore) New() *Contact {
+	return &Contact{
+		Errors: make(map[string]string),
+	}
+}
+
 type Contact struct {
 	ID     int    `json:"id"`
 	First  string `json:"first"`
 	Last   string `json:"last"`
 	Phone  string `json:"phone"`
 	Email  string `json:"email"`
-	errors map[string]string
+	Errors map[string]string
 }

+ 2 - 1
contactsapp/static/site.css

@@ -70,6 +70,7 @@ all-caps {
 sub-title {
     text-transform: capitalize;
 }
-a:link {
+a:link,
+a:visited {
     color: #5AA791;
 }

+ 34 - 0
contactsapp/templates/new.gohtml

@@ -0,0 +1,34 @@
+{{ define "new.html" }}
+    {{ block "content" . }}
+      <form action="/contacts/new" method="post">
+        <fieldset>
+          <legend>Contact Values</legend>
+          <p>
+            <label for="email">Email</label>
+            <input name="email" id="email" type="email" placeholder="Email" value="{{ .Email }}">
+            <span class="error">{{ .Errors.email }}</span>
+          </p>
+          <p>
+            <label for="first_name">First Name</label>
+            <input name="first_name" id="first_name" type="text" placeholder="First Name" value="{{ .First }}">
+            <span class="error">{{ .Errors.First }}</span>
+          </p>
+          <p>
+            <label for="last_name">Last Name</label>
+            <input name="last_name" id="last_name" type="text" placeholder="Last Name" value="{{ .Last }}">
+            <span class="error">{{ .Errors.Last }}</span>
+          </p>
+          <p>
+            <label for="phone">Phone</label>
+            <input name="phone" id="phone" type="text" placeholder="Phone" value="{{ .Phone }}">
+            <span class="error">{{ .Errors.Phone }}</span>
+          </p>
+          <button>Save</button>
+        </fieldset>
+      </form>
+
+      <p>
+        <a href="/contacts">Back</a>
+      </p>
+    {{ end }}
+{{ end }}