Exemplo n.º 1
0
// Comments return slice of comments that match given query criteria.
func Comments(s pg.Selector, o CommentsOpts) ([]*Comment, error) {
	if o.Offset < 0 {
		o.Offset = 0
	}
	q := qb.Q("SELECT * FROM comments").Limit(o.Limit, o.Offset).OrderBy("created DESC")
	if o.TopicID != 0 {
		q.Where("topic_id = ?", o.TopicID)
	}
	if o.AuthorID != "" {
		q.Where("author_id = ?", o.AuthorID)
	}
	query, args := q.Build()
	query = sqlx.Rebind(sqlx.DOLLAR, query)
	var comments []*Comment
	if err := s.Select(&comments, query, args...); err != nil {
		return nil, pg.CastErr(err)
	}

	return comments, nil
}
Exemplo n.º 2
0
// Topics return slice of topics that match given query criteria.
func Topics(s pg.Selector, o TopicsOpts) ([]*TopicWithAuthor, error) {
	if o.Offset < 0 {
		o.Offset = 0
	}
	q := qb.Q(`
		SELECT t.*, a.*
		FROM topics t LEFT JOIN accounts a ON t.author_id = a.account_id
	`).Limit(o.Limit, o.Offset).OrderBy("t.created ASC")

	if len(o.Tags) > 0 {
		// TODO
	}
	if !o.OlderThan.IsZero() {
		q.Where("t.created < ?", o.OlderThan)
	}

	query, args := q.Build()
	query = sqlx.Rebind(sqlx.DOLLAR, query)
	var topics []*TopicWithAuthor
	if err := s.Select(&topics, query, args...); err != nil {
		return nil, pg.CastErr(err)
	}
	return topics, nil
}