166 lines
5.7 KiB
Go
166 lines
5.7 KiB
Go
package models
|
|
|
|
// Code generated by xo. DO NOT EDIT.
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// QuestionAttempt represents a row from 'question_attempt'.
|
|
type QuestionAttempt struct {
|
|
ID int `json:"id"` // id
|
|
UserID string `json:"user_id"` // user_id
|
|
QuestionID string `json:"question_id"` // question_id
|
|
QuestionPart int `json:"question_part"` // question_part
|
|
Correct bool `json:"correct"` // correct
|
|
PointsAwarded int `json:"points_awarded"` // points_awarded
|
|
Answer string `json:"answer"` // answer
|
|
Code string `json:"code"` // code
|
|
SubmittedAt Time `json:"submitted_at"` // submitted_at
|
|
// xo fields
|
|
_exists, _deleted bool
|
|
}
|
|
|
|
// Exists returns true when the QuestionAttempt exists in the database.
|
|
func (qa *QuestionAttempt) Exists() bool {
|
|
return qa._exists
|
|
}
|
|
|
|
// Deleted returns true when the QuestionAttempt has been marked for deletion from
|
|
// the database.
|
|
func (qa *QuestionAttempt) Deleted() bool {
|
|
return qa._deleted
|
|
}
|
|
|
|
// Insert inserts the QuestionAttempt to the database.
|
|
func (qa *QuestionAttempt) Insert(ctx context.Context, db DB) error {
|
|
switch {
|
|
case qa._exists: // already exists
|
|
return logerror(&ErrInsertFailed{ErrAlreadyExists})
|
|
case qa._deleted: // deleted
|
|
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
|
|
}
|
|
// insert (primary key generated and returned by database)
|
|
const sqlstr = `INSERT INTO question_attempt (` +
|
|
`user_id, question_id, question_part, correct, points_awarded, answer, code, submitted_at` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3, $4, $5, $6, $7, $8` +
|
|
`)`
|
|
// run
|
|
logf(sqlstr, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt)
|
|
res, err := db.ExecContext(ctx, sqlstr, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt)
|
|
if err != nil {
|
|
return logerror(err)
|
|
}
|
|
// retrieve id
|
|
id, err := res.LastInsertId()
|
|
if err != nil {
|
|
return logerror(err)
|
|
} // set primary key
|
|
qa.ID = int(id)
|
|
// set exists
|
|
qa._exists = true
|
|
return nil
|
|
}
|
|
|
|
// Update updates a QuestionAttempt in the database.
|
|
func (qa *QuestionAttempt) Update(ctx context.Context, db DB) error {
|
|
switch {
|
|
case !qa._exists: // doesn't exist
|
|
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
|
|
case qa._deleted: // deleted
|
|
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
|
|
}
|
|
// update with primary key
|
|
const sqlstr = `UPDATE question_attempt SET ` +
|
|
`user_id = $1, question_id = $2, question_part = $3, correct = $4, points_awarded = $5, answer = $6, code = $7, submitted_at = $8 ` +
|
|
`WHERE id = $9`
|
|
// run
|
|
logf(sqlstr, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt, qa.ID)
|
|
if _, err := db.ExecContext(ctx, sqlstr, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt, qa.ID); err != nil {
|
|
return logerror(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Save saves the QuestionAttempt to the database.
|
|
func (qa *QuestionAttempt) Save(ctx context.Context, db DB) error {
|
|
if qa.Exists() {
|
|
return qa.Update(ctx, db)
|
|
}
|
|
return qa.Insert(ctx, db)
|
|
}
|
|
|
|
// Upsert performs an upsert for QuestionAttempt.
|
|
func (qa *QuestionAttempt) Upsert(ctx context.Context, db DB) error {
|
|
switch {
|
|
case qa._deleted: // deleted
|
|
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
|
|
}
|
|
// upsert
|
|
const sqlstr = `INSERT INTO question_attempt (` +
|
|
`id, user_id, question_id, question_part, correct, points_awarded, answer, code, submitted_at` +
|
|
`) VALUES (` +
|
|
`$1, $2, $3, $4, $5, $6, $7, $8, $9` +
|
|
`)` +
|
|
` ON CONFLICT (id) DO ` +
|
|
`UPDATE SET ` +
|
|
`user_id = EXCLUDED.user_id, question_id = EXCLUDED.question_id, question_part = EXCLUDED.question_part, correct = EXCLUDED.correct, points_awarded = EXCLUDED.points_awarded, answer = EXCLUDED.answer, code = EXCLUDED.code, submitted_at = EXCLUDED.submitted_at `
|
|
// run
|
|
logf(sqlstr, qa.ID, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt)
|
|
if _, err := db.ExecContext(ctx, sqlstr, qa.ID, qa.UserID, qa.QuestionID, qa.QuestionPart, qa.Correct, qa.PointsAwarded, qa.Answer, qa.Code, qa.SubmittedAt); err != nil {
|
|
return logerror(err)
|
|
}
|
|
// set exists
|
|
qa._exists = true
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes the QuestionAttempt from the database.
|
|
func (qa *QuestionAttempt) Delete(ctx context.Context, db DB) error {
|
|
switch {
|
|
case !qa._exists: // doesn't exist
|
|
return nil
|
|
case qa._deleted: // deleted
|
|
return nil
|
|
}
|
|
// delete with single primary key
|
|
const sqlstr = `DELETE FROM question_attempt ` +
|
|
`WHERE id = $1`
|
|
// run
|
|
logf(sqlstr, qa.ID)
|
|
if _, err := db.ExecContext(ctx, sqlstr, qa.ID); err != nil {
|
|
return logerror(err)
|
|
}
|
|
// set deleted
|
|
qa._deleted = true
|
|
return nil
|
|
}
|
|
|
|
// QuestionAttemptByID retrieves a row from 'question_attempt' as a QuestionAttempt.
|
|
//
|
|
// Generated from index 'question_attempt_id_pkey'.
|
|
func QuestionAttemptByID(ctx context.Context, db DB, id int) (*QuestionAttempt, error) {
|
|
// query
|
|
const sqlstr = `SELECT ` +
|
|
`id, user_id, question_id, question_part, correct, points_awarded, answer, code, submitted_at ` +
|
|
`FROM question_attempt ` +
|
|
`WHERE id = $1`
|
|
// run
|
|
logf(sqlstr, id)
|
|
qa := QuestionAttempt{
|
|
_exists: true,
|
|
}
|
|
if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&qa.ID, &qa.UserID, &qa.QuestionID, &qa.QuestionPart, &qa.Correct, &qa.PointsAwarded, &qa.Answer, &qa.Code, &qa.SubmittedAt); err != nil {
|
|
return nil, logerror(err)
|
|
}
|
|
return &qa, nil
|
|
}
|
|
|
|
// User returns the User associated with the QuestionAttempt's (UserID).
|
|
//
|
|
// Generated from foreign key 'question_attempt_user_id_fkey'.
|
|
func (qa *QuestionAttempt) User(ctx context.Context, db DB) (*User, error) {
|
|
return UserByID(ctx, db, qa.UserID)
|
|
}
|