Beispiel #1
0
func servePosts(w http.ResponseWriter, r *http.Request) error {
	var opt thesrc.PostListOptions
	if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
		return err
	}

	opt.CodeOnly = true

	if opt.PerPage == 0 {
		opt.PerPage = 60
	}

	posts, err := APIClient.Posts.List(&opt)
	if err != nil {
		return err
	}

	return renderTemplate(w, r, "posts/list.html", http.StatusOK, struct {
		Posts []*thesrc.Post
	}{
		Posts: posts,
	})
}
Beispiel #2
0
func (s *postsStore) List(opt *thesrc.PostListOptions) ([]*thesrc.Post, error) {
	if opt == nil {
		opt = &thesrc.PostListOptions{}
	}

	sql := `SELECT * FROM post`

	var conds []string
	if opt.CodeOnly {
		conds = append(conds, "classification LIKE 'CODE%'")
	}
	if len(conds) > 0 {
		sql += " WHERE (" + strings.Join(conds, ") AND (") + ")"
	}

	sql += " ORDER BY submittedat DESC LIMIT $1 OFFSET $2;"

	var posts []*thesrc.Post
	err := s.dbh.Select(&posts, sql, opt.PerPageOrDefault(), opt.Offset())
	if err != nil {
		return nil, err
	}
	return posts, nil
}