64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package models
|
|
|
|
// Code generated by xo. DO NOT EDIT.
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// AllPartsData represents a row from 'all_parts_data'.
|
|
type AllPartsData struct {
|
|
QuestionID string `json:"question_id"` // question_id
|
|
P1Awarded sql.NullInt64 `json:"p1awarded"` // p1awarded
|
|
P2Awarded sql.NullInt64 `json:"p2awarded"` // p2awarded
|
|
}
|
|
|
|
// AllQuestionPartsData runs a custom query, returning results as AllPartsData.
|
|
func AllQuestionPartsData(ctx context.Context, db DB, user_id string) ([]*AllPartsData, error) {
|
|
// query
|
|
const sqlstr = `WITH attempts AS ( ` +
|
|
`SELECT * ` +
|
|
`FROM question_attempt ` +
|
|
`WHERE correct = true ` +
|
|
`) ` +
|
|
`SELECT DISTINCT ` +
|
|
`a.question_id as question_id, ` +
|
|
`MAX(p1.points_awarded) AS p1_awarded, ` +
|
|
`MAX(p2.points_awarded) AS p2_awarded ` +
|
|
`FROM attempts a ` +
|
|
` ` +
|
|
`LEFT JOIN attempts AS p1 ` +
|
|
`ON p1.id = a.id ` +
|
|
`AND p1.question_part = 1 ` +
|
|
` ` +
|
|
`LEFT JOIN attempts AS p2 ` +
|
|
`ON p2.id = a.id ` +
|
|
`AND p2.question_part = 2 ` +
|
|
` ` +
|
|
`WHERE a.user_id = $1 ` +
|
|
` ` +
|
|
`GROUP BY a.user_id, a.question_id`
|
|
// run
|
|
logf(sqlstr, user_id)
|
|
rows, err := db.QueryContext(ctx, sqlstr, user_id)
|
|
if err != nil {
|
|
return nil, logerror(err)
|
|
}
|
|
defer rows.Close()
|
|
// load results
|
|
var res []*AllPartsData
|
|
for rows.Next() {
|
|
var apd AllPartsData
|
|
// scan
|
|
if err := rows.Scan(&apd.QuestionID, &apd.P1Awarded, &apd.P2Awarded); err != nil {
|
|
return nil, logerror(err)
|
|
}
|
|
res = append(res, &apd)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, logerror(err)
|
|
}
|
|
return res, nil
|
|
}
|