Exemple #1
0
func TestReverse(t *testing.T) {
	var entry1, entry2, entry3, entry4 vsafe.Entry
	var s []*vsafe.Entry
	vsafedb.Reverse(s)
	s = []*vsafe.Entry{&entry1}
	vsafedb.Reverse(s)
	if s[0] != &entry1 {
		t.Error("reverse with 1 entry wrong.")
	}
	s = []*vsafe.Entry{&entry1, &entry2}
	vsafedb.Reverse(s)
	if s[0] != &entry2 || s[1] != &entry1 {
		t.Error("reverse with 2 entries wrong.")
	}
	s = []*vsafe.Entry{&entry1, &entry2, &entry3}
	vsafedb.Reverse(s)
	if s[0] != &entry3 || s[1] != &entry2 || s[2] != &entry1 {
		t.Error("reverse with 3 entries wrong.")
	}
	s = []*vsafe.Entry{&entry1, &entry2, &entry3, &entry4}
	vsafedb.Reverse(s)
	if s[0] != &entry4 || s[1] != &entry3 || s[2] != &entry2 || s[3] != &entry1 {
		t.Error("reverse with 4 entries wrong.")
	}
}
Exemple #2
0
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	session := common.GetUserSession(r)
	sortBy := r.Form.Get("sort")
	id, _ := strconv.ParseInt(r.Form.Get("id"), 10, 64)
	entries, err := vsafedb.Entries(h.Store, session.Key().Id, r.Form.Get("q"))
	if err != nil {
		http_util.ReportError(w, "Error reading database", err)
		return
	}
	switch sortBy {
	case "newest":
		vsafedb.Reverse(entries)
	default:
		vsafedb.SortByTitle(entries)
	}
	http_util.WriteTemplate(
		w,
		kTemplate,
		&view{
			Values:  http_util.Values{r.Form},
			Name:    session.User.Name,
			Entries: entries,
			Url:     r.URL,
			Id:      id})
}