hackathon/models/userinfo.xo.go

153 lines
4.0 KiB
Go

package models
// Code generated by xo. DO NOT EDIT.
import (
"context"
)
// UserInfo represents a row from 'user_info'.
type UserInfo struct {
UserID string `json:"user_id"` // user_id
GradeLevel int `json:"grade_level"` // grade_level
SkillLevel int `json:"skill_level"` // skill_level
// xo fields
_exists, _deleted bool
}
// Exists returns true when the UserInfo exists in the database.
func (ui *UserInfo) Exists() bool {
return ui._exists
}
// Deleted returns true when the UserInfo has been marked for deletion from
// the database.
func (ui *UserInfo) Deleted() bool {
return ui._deleted
}
// Insert inserts the UserInfo to the database.
func (ui *UserInfo) Insert(ctx context.Context, db DB) error {
switch {
case ui._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case ui._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO user_info (` +
`user_id, grade_level, skill_level` +
`) VALUES (` +
`$1, $2, $3` +
`)`
// run
logf(sqlstr, ui.UserID, ui.GradeLevel, ui.SkillLevel)
if _, err := db.ExecContext(ctx, sqlstr, ui.UserID, ui.GradeLevel, ui.SkillLevel); err != nil {
return logerror(err)
}
// set exists
ui._exists = true
return nil
}
// Update updates a UserInfo in the database.
func (ui *UserInfo) Update(ctx context.Context, db DB) error {
switch {
case !ui._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case ui._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE user_info SET ` +
`grade_level = $1, skill_level = $2 ` +
`WHERE user_id = $3`
// run
logf(sqlstr, ui.GradeLevel, ui.SkillLevel, ui.UserID)
if _, err := db.ExecContext(ctx, sqlstr, ui.GradeLevel, ui.SkillLevel, ui.UserID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the UserInfo to the database.
func (ui *UserInfo) Save(ctx context.Context, db DB) error {
if ui.Exists() {
return ui.Update(ctx, db)
}
return ui.Insert(ctx, db)
}
// Upsert performs an upsert for UserInfo.
func (ui *UserInfo) Upsert(ctx context.Context, db DB) error {
switch {
case ui._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO user_info (` +
`user_id, grade_level, skill_level` +
`) VALUES (` +
`$1, $2, $3` +
`)` +
` ON CONFLICT (user_id) DO ` +
`UPDATE SET ` +
`grade_level = EXCLUDED.grade_level, skill_level = EXCLUDED.skill_level `
// run
logf(sqlstr, ui.UserID, ui.GradeLevel, ui.SkillLevel)
if _, err := db.ExecContext(ctx, sqlstr, ui.UserID, ui.GradeLevel, ui.SkillLevel); err != nil {
return logerror(err)
}
// set exists
ui._exists = true
return nil
}
// Delete deletes the UserInfo from the database.
func (ui *UserInfo) Delete(ctx context.Context, db DB) error {
switch {
case !ui._exists: // doesn't exist
return nil
case ui._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM user_info ` +
`WHERE user_id = $1`
// run
logf(sqlstr, ui.UserID)
if _, err := db.ExecContext(ctx, sqlstr, ui.UserID); err != nil {
return logerror(err)
}
// set deleted
ui._deleted = true
return nil
}
// UserInfoByUserID retrieves a row from 'user_info' as a UserInfo.
//
// Generated from index 'sqlite_autoindex_user_info_1'.
func UserInfoByUserID(ctx context.Context, db DB, userID string) (*UserInfo, error) {
// query
const sqlstr = `SELECT ` +
`user_id, grade_level, skill_level ` +
`FROM user_info ` +
`WHERE user_id = $1`
// run
logf(sqlstr, userID)
ui := UserInfo{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, userID).Scan(&ui.UserID, &ui.GradeLevel, &ui.SkillLevel); err != nil {
return nil, logerror(err)
}
return &ui, nil
}
// User returns the User associated with the UserInfo's (UserID).
//
// Generated from foreign key 'user_info_user_id_fkey'.
func (ui *UserInfo) User(ctx context.Context, db DB) (*User, error) {
return UserByID(ctx, db, ui.UserID)
}