hackathon/models/user.xo.go

216 lines
6.3 KiB
Go

package models
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// User represents a row from 'users'.
type User struct {
ID string `json:"id"` // id
Name string `json:"name"` // name
Email string `json:"email"` // email
Picture string `json:"picture"` // picture
GradeLevel sql.NullInt64 `json:"grade_level"` // grade_level
Teacher bool `json:"teacher"` // teacher
Admin bool `json:"admin"` // admin
CreatedAt Time `json:"created_at"` // created_at
// xo fields
_exists, _deleted bool
}
// Exists returns true when the User exists in the database.
func (u *User) Exists() bool {
return u._exists
}
// Deleted returns true when the User has been marked for deletion from
// the database.
func (u *User) Deleted() bool {
return u._deleted
}
// Insert inserts the User to the database.
func (u *User) Insert(ctx context.Context, db DB) error {
switch {
case u._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case u._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (manual)
const sqlstr = `INSERT INTO users (` +
`id, name, email, picture, grade_level, teacher, admin, created_at` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8` +
`)`
// run
logf(sqlstr, u.ID, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt)
if _, err := db.ExecContext(ctx, sqlstr, u.ID, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt); err != nil {
return logerror(err)
}
// set exists
u._exists = true
return nil
}
// Update updates a User in the database.
func (u *User) Update(ctx context.Context, db DB) error {
switch {
case !u._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case u._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE users SET ` +
`name = $1, email = $2, picture = $3, grade_level = $4, teacher = $5, admin = $6, created_at = $7 ` +
`WHERE id = $8`
// run
logf(sqlstr, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt, u.ID)
if _, err := db.ExecContext(ctx, sqlstr, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt, u.ID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the User to the database.
func (u *User) Save(ctx context.Context, db DB) error {
if u.Exists() {
return u.Update(ctx, db)
}
return u.Insert(ctx, db)
}
// Upsert performs an upsert for User.
func (u *User) Upsert(ctx context.Context, db DB) error {
switch {
case u._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO users (` +
`id, name, email, picture, grade_level, teacher, admin, created_at` +
`) VALUES (` +
`$1, $2, $3, $4, $5, $6, $7, $8` +
`)` +
` ON CONFLICT (id) DO ` +
`UPDATE SET ` +
`name = EXCLUDED.name, email = EXCLUDED.email, picture = EXCLUDED.picture, grade_level = EXCLUDED.grade_level, teacher = EXCLUDED.teacher, admin = EXCLUDED.admin, created_at = EXCLUDED.created_at `
// run
logf(sqlstr, u.ID, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt)
if _, err := db.ExecContext(ctx, sqlstr, u.ID, u.Name, u.Email, u.Picture, u.GradeLevel, u.Teacher, u.Admin, u.CreatedAt); err != nil {
return logerror(err)
}
// set exists
u._exists = true
return nil
}
// Delete deletes the User from the database.
func (u *User) Delete(ctx context.Context, db DB) error {
switch {
case !u._exists: // doesn't exist
return nil
case u._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM users ` +
`WHERE id = $1`
// run
logf(sqlstr, u.ID)
if _, err := db.ExecContext(ctx, sqlstr, u.ID); err != nil {
return logerror(err)
}
// set deleted
u._deleted = true
return nil
}
// UserByID retrieves a row from 'users' as a User.
//
// Generated from index 'sqlite_autoindex_users_1'.
func UserByID(ctx context.Context, db DB, id string) (*User, error) {
// query
const sqlstr = `SELECT ` +
`id, name, email, picture, grade_level, teacher, admin, created_at ` +
`FROM users ` +
`WHERE id = $1`
// run
logf(sqlstr, id)
u := User{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.GradeLevel, &u.Teacher, &u.Admin, &u.CreatedAt); err != nil {
return nil, logerror(err)
}
return &u, nil
}
// UserByEmail retrieves a row from 'users' as a User.
//
// Generated from index 'sqlite_autoindex_users_2'.
func UserByEmail(ctx context.Context, db DB, email string) (*User, error) {
// query
const sqlstr = `SELECT ` +
`id, name, email, picture, grade_level, teacher, admin, created_at ` +
`FROM users ` +
`WHERE email = $1`
// run
logf(sqlstr, email)
u := User{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, email).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.GradeLevel, &u.Teacher, &u.Admin, &u.CreatedAt); err != nil {
return nil, logerror(err)
}
return &u, nil
}
// Users runs a custom query, returning results as User.
func Users(ctx context.Context, db DB) ([]*User, error) {
// query
const sqlstr = `SELECT u.* FROM users u`
// run
logf(sqlstr)
rows, err := db.QueryContext(ctx, sqlstr)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// load results
var res []*User
for rows.Next() {
var u User
// scan
if err := rows.Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.GradeLevel, &u.Teacher, &u.Admin, &u.CreatedAt); err != nil {
return nil, logerror(err)
}
res = append(res, &u)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// UserByToken runs a custom query, returning results as User.
func UserByToken(ctx context.Context, db DB, token string) (*User, error) {
// query
const sqlstr = `SELECT ` +
`u.* ` +
`FROM tokens t ` +
`JOIN users u ON t.user_id = u.id ` +
`WHERE t.token = $1`
// run
logf(sqlstr, token)
var u User
if err := db.QueryRowContext(ctx, sqlstr, token).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.GradeLevel, &u.Teacher, &u.Admin, &u.CreatedAt); err != nil {
return nil, logerror(err)
}
return &u, nil
}