Created
September 29, 2019 14:32
-
-
Save vianhanif/a205b574a3162e94388977975b4cce3a to your computer and use it in GitHub Desktop.
sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GroupByUserAndType : grouping cashback by userId and type | |
func (service *App) GroupByUserAndType(ctx context.Context, params ...QueryFilter) ([]*SumCashback, error) { | |
queryable, ok := data.QueryableFromContext(ctx) | |
if !ok { | |
queryable = service.queryable | |
} | |
where, args := BuildFilter(params...) | |
statement := fmt.Sprintf(` | |
SELECT "userId", SUM(amount) AS "amount", "type" | |
FROM "%s" %s GROUP BY "userId", "type"; | |
`, TableName, where) | |
rows, errSelect := queryable.Query(statement, args...) | |
if errSelect != nil { | |
return nil, errSelect | |
} | |
defer rows.Close() | |
var records []*SumCashback | |
for rows.Next() { | |
sumCashback := &SumCashback{} | |
err := rows.Scan(&sumCashback.UserID, &sumCashback.Amount, &sumCashback.Type) | |
if err != nil { | |
return nil, err | |
} | |
records = append(records, sumCashback) | |
} | |
return records, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment