hackathon/models/partsdata.xo.go

48 lines
1.2 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
P2Awarded sql.NullInt64 `json:"p2_awarded"` // p2_awarded
}
// 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, ` +
`p2.points_awarded AS p2_awarded ` +
`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.P2Awarded); err != nil {
return nil, logerror(err)
}
return &pd, nil
}