Skip to content

Instantly share code, notes, and snippets.

@mozarik
Created October 6, 2021 12:13
Show Gist options
  • Save mozarik/0cf87b0f36904d5f85505c69e27ee643 to your computer and use it in GitHub Desktop.
Save mozarik/0cf87b0f36904d5f85505c69e27ee643 to your computer and use it in GitHub Desktop.
Rows Scan Gorm
func (repo QuoteRepo) FindUserFavorites(userID int) ([]domain.Quote, error) {
query := `SELECT q.id, q.body, q.author, q.quote_source FROM quotes q
JOIN userfavoritesquotes on (q.id = userfavoritesquotes.quote_id)
JOIN users ON (users.id = userfavoritesquotes.user_id)
WHERE users.id = @userID`
rows, err := repo.DB.Debug().Raw(query, sql.Named("userID", userID)).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
var quotes []domain.Quote
for rows.Next() {
var quote domain.Quote
if err := rows.Scan(&quote.ID, &quote.Body, &quote.Author, &quote.QuoteSource); err != nil {
return quotes, err
}
quotes = append(quotes, quote)
}
if err = rows.Err(); err != nil {
return nil, err
}
return quotes, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment