hackathon/models/partsdata.xo.go

51 lines
1.4 KiB
Go

package models
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// PartsData represents a row from 'parts_data'.
type PartsData struct {
P1Awarded int `json:"p1awarded"` // p1awarded
P1Answer string `json:"p1answer"` // p1answer
P2Awarded int `json:"p2awarded"` // p2awarded
P2Answer string `json:"p2answer"` // p2answer
}
// 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 ` +
`COALESCE(p1.points_awarded, 0) AS p1_awarded, ` +
`COALESCE(p1.answer, '') AS p1_answer, ` +
`COALESCE(p2.points_awarded, 0) AS p2_awarded, ` +
`COALESCE(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
}