52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package models
|
|
|
|
// Code generated by xo. DO NOT EDIT.
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// PartsData represents a row from 'parts_data'.
|
|
type PartsData struct {
|
|
P1Awarded sql.NullInt64 `json:"p1_awarded"` // p1_awarded
|
|
P1Answer sql.NullString `json:"p1_answer"` // p1_answer
|
|
P2Awarded sql.NullInt64 `json:"p2_awarded"` // p2_awarded
|
|
P2Answer sql.NullString `json:"p2_answer"` // p2_answer
|
|
}
|
|
|
|
// QuestionPartsData runs a custom query, returning results as PartsData.
|
|
func QuestionPartsData(ctx context.Context, db DB, question_id, user_id string) (*PartsData, error) {
|
|
// query
|
|
const sqlstr = `WITH attempts AS ( ` +
|
|
`SELECT * ` +
|
|
`FROM question_attempt ` +
|
|
`WHERE question_id = $1 ` +
|
|
`AND correct = true ` +
|
|
`) ` +
|
|
`SELECT ` +
|
|
`p1.points_awarded AS p1_awarded, ` +
|
|
`p1.answer AS p1_answer, ` +
|
|
`p2.points_awarded AS p2_awarded, ` +
|
|
`p2.answer AS p2_answer ` +
|
|
`FROM users u ` +
|
|
` ` +
|
|
`LEFT JOIN attempts AS p1 ` +
|
|
`ON p1.user_id = u.id ` +
|
|
`AND p1.question_part = 1 ` +
|
|
` ` +
|
|
`LEFT JOIN attempts AS p2 ` +
|
|
`ON p2.user_id = u.id ` +
|
|
`AND p2.question_part = 2 ` +
|
|
` ` +
|
|
`WHERE ` +
|
|
`u.id = $2`
|
|
// run
|
|
logf(sqlstr, question_id, user_id)
|
|
var pd PartsData
|
|
if err := db.QueryRowContext(ctx, sqlstr, question_id, user_id).Scan(&pd.P1Awarded, &pd.P1Answer, &pd.P2Awarded, &pd.P2Answer); err != nil {
|
|
return nil, logerror(err)
|
|
}
|
|
return &pd, nil
|
|
}
|