1. Miki Tebeka
  2. go-appengine-demo

Commits

Miki Tebeka  committed bc3970a

User urls

  • Participants
  • Parent commits 5f1b336
  • Branches default

Comments (0)

Files changed (2)

File shortie/html.go

View file
  • Ignore whitespace
 
         {{ if .Error }}
         <div class="error">
-            ERROR: {{ html .Error }}
+            ERROR: {{ .Error | html}}
         </div>
         {{ end }}
 
             <input type="submit" value="Shorten">
         </form>
 
+		{{ with .URLs }}
+        <b>Your Urls</b>
+        <table class="urls">
+            <tr><th>URL</th><th>Created</th><th>Hits</th></tr>
+			{{ range . }}
+			<!-- FIXME: odd/even -->
+            <tr>
+                <td title="{{ .Long | html }}">
+                    <a href="{{ .Long }}">{{ .Short }}</a>
+                </td>
+				<!-- FIXME: format -->
+                <td>{{ .Created }}</td>
+                <td>{{ .Hits }}</td>
+            </tr>
+            {{ end }}
+        </table>
+        {{ end }}
+
         <div class="footer">
             G'day {{ .User }}, we've shortened {{ .Count }} urls so far.
         </div>

File shortie/shortie.go

View file
  • Ignore whitespace
 }
 
 type URL struct {
+	Short   string
 	Long    string
 	User    string
 	Created datastore.Time
 			return err
 		}
 
-		url.Hits ++
+		url.Hits++
 		key := urlKey(ctx, short)
 		if _, err := datastore.Put(ctx, key, url); err != nil {
 			return err
 }
 
 func userURLs(ctx appengine.Context, user string) ([]*URL, os.Error) {
-	 query := datastore.NewQuery(urlKind).
+	query := datastore.NewQuery(urlKind).
 		Filter("User =", user).
 		Order("-Created").
 		Limit(1000)
 	Count      int64
 	Error      string
 	ShortURL   string
-	URLs	   []*URL
+	URLs       []*URL
 }
 
 func rootHandler(w http.ResponseWriter, r *http.Request) {
 		params.User = u.String()
 		params.LoginTitle = "Logout"
 		params.LoginURL, err = user.LogoutURL(ctx, r.URL.String())
+
+		params.URLs, err = userURLs(ctx, params.User)
+		if err != nil {
+			return
+		}
 	} else {
 		params.LoginTitle = "Login"
 		params.LoginURL, err = user.LoginURL(ctx, r.URL.String())
 	}
 
-
 	if r.Method == "POST" {
 		longURL := strings.TrimSpace(r.FormValue("url"))
 		if len(longURL) == 0 {
 		}
 
 		url := &URL{
-			Long: longURL,
-			User: params.User,
+			Short:   id,
+			Long:    longURL,
+			User:    params.User,
 			Created: datastore.SecondsToTime(time.Seconds()),
-			Hits: 0,
+			Hits:    0,
 		}
 		key := datastore.NewKey(ctx, urlKind, id, 0, nil)
 		_, err = datastore.Put(ctx, key, url)