hackathon/models/token.xo.go

236 lines
5.9 KiB
Go

package models
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
)
// Token represents a row from 'tokens'.
type Token struct {
ID int `json:"id"` // id
Token string `json:"token"` // token
UserID string `json:"user_id"` // user_id
CreatedAt Time `json:"created_at"` // created_at
// xo fields
_exists, _deleted bool
}
// Exists returns true when the Token exists in the database.
func (t *Token) Exists() bool {
return t._exists
}
// Deleted returns true when the Token has been marked for deletion from
// the database.
func (t *Token) Deleted() bool {
return t._deleted
}
// Insert inserts the Token to the database.
func (t *Token) Insert(ctx context.Context, db DB) error {
switch {
case t._exists: // already exists
return logerror(&ErrInsertFailed{ErrAlreadyExists})
case t._deleted: // deleted
return logerror(&ErrInsertFailed{ErrMarkedForDeletion})
}
// insert (primary key generated and returned by database)
const sqlstr = `INSERT INTO tokens (` +
`token, user_id, created_at` +
`) VALUES (` +
`$1, $2, $3` +
`)`
// run
logf(sqlstr, t.Token, t.UserID, t.CreatedAt)
res, err := db.ExecContext(ctx, sqlstr, t.Token, t.UserID, t.CreatedAt)
if err != nil {
return logerror(err)
}
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
} // set primary key
t.ID = int(id)
// set exists
t._exists = true
return nil
}
// Update updates a Token in the database.
func (t *Token) Update(ctx context.Context, db DB) error {
switch {
case !t._exists: // doesn't exist
return logerror(&ErrUpdateFailed{ErrDoesNotExist})
case t._deleted: // deleted
return logerror(&ErrUpdateFailed{ErrMarkedForDeletion})
}
// update with primary key
const sqlstr = `UPDATE tokens SET ` +
`token = $1, user_id = $2, created_at = $3 ` +
`WHERE id = $4`
// run
logf(sqlstr, t.Token, t.UserID, t.CreatedAt, t.ID)
if _, err := db.ExecContext(ctx, sqlstr, t.Token, t.UserID, t.CreatedAt, t.ID); err != nil {
return logerror(err)
}
return nil
}
// Save saves the Token to the database.
func (t *Token) Save(ctx context.Context, db DB) error {
if t.Exists() {
return t.Update(ctx, db)
}
return t.Insert(ctx, db)
}
// Upsert performs an upsert for Token.
func (t *Token) Upsert(ctx context.Context, db DB) error {
switch {
case t._deleted: // deleted
return logerror(&ErrUpsertFailed{ErrMarkedForDeletion})
}
// upsert
const sqlstr = `INSERT INTO tokens (` +
`id, token, user_id, created_at` +
`) VALUES (` +
`$1, $2, $3, $4` +
`)` +
` ON CONFLICT (id) DO ` +
`UPDATE SET ` +
`token = EXCLUDED.token, user_id = EXCLUDED.user_id, created_at = EXCLUDED.created_at `
// run
logf(sqlstr, t.ID, t.Token, t.UserID, t.CreatedAt)
if _, err := db.ExecContext(ctx, sqlstr, t.ID, t.Token, t.UserID, t.CreatedAt); err != nil {
return logerror(err)
}
// set exists
t._exists = true
return nil
}
// Delete deletes the Token from the database.
func (t *Token) Delete(ctx context.Context, db DB) error {
switch {
case !t._exists: // doesn't exist
return nil
case t._deleted: // deleted
return nil
}
// delete with single primary key
const sqlstr = `DELETE FROM tokens ` +
`WHERE id = $1`
// run
logf(sqlstr, t.ID)
if _, err := db.ExecContext(ctx, sqlstr, t.ID); err != nil {
return logerror(err)
}
// set deleted
t._deleted = true
return nil
}
// TokenByToken retrieves a row from 'tokens' as a Token.
//
// Generated from index 'sqlite_autoindex_tokens_1'.
func TokenByToken(ctx context.Context, db DB, token string) (*Token, error) {
// query
const sqlstr = `SELECT ` +
`id, token, user_id, created_at ` +
`FROM tokens ` +
`WHERE token = $1`
// run
logf(sqlstr, token)
t := Token{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, token).Scan(&t.ID, &t.Token, &t.UserID, &t.CreatedAt); err != nil {
return nil, logerror(err)
}
return &t, nil
}
// TokenByID retrieves a row from 'tokens' as a Token.
//
// Generated from index 'tokens_id_pkey'.
func TokenByID(ctx context.Context, db DB, id int) (*Token, error) {
// query
const sqlstr = `SELECT ` +
`id, token, user_id, created_at ` +
`FROM tokens ` +
`WHERE id = $1`
// run
logf(sqlstr, id)
t := Token{
_exists: true,
}
if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&t.ID, &t.Token, &t.UserID, &t.CreatedAt); err != nil {
return nil, logerror(err)
}
return &t, nil
}
// TokensByUserID retrieves a row from 'tokens' as a Token.
//
// Generated from index 'user_tokens'.
func TokensByUserID(ctx context.Context, db DB, userID string) ([]*Token, error) {
// query
const sqlstr = `SELECT ` +
`id, token, user_id, created_at ` +
`FROM tokens ` +
`WHERE user_id = $1`
// run
logf(sqlstr, userID)
rows, err := db.QueryContext(ctx, sqlstr, userID)
if err != nil {
return nil, logerror(err)
}
defer rows.Close()
// process
var res []*Token
for rows.Next() {
t := Token{
_exists: true,
}
// scan
if err := rows.Scan(&t.ID, &t.Token, &t.UserID, &t.CreatedAt); err != nil {
return nil, logerror(err)
}
res = append(res, &t)
}
if err := rows.Err(); err != nil {
return nil, logerror(err)
}
return res, nil
}
// User returns the User associated with the Token's (UserID).
//
// Generated from foreign key 'tokens_user_id_fkey'.
func (t *Token) User(ctx context.Context, db DB) (*User, error) {
return UserByID(ctx, db, t.UserID)
}
// DeleteToken runs a custom query as a sql.Result.
func DeleteToken(ctx context.Context, db DB, token string) (sql.Result, error) {
// query
const sqlstr = `DELETE FROM tokens ` +
`WHERE token = $1`
// run
logf(sqlstr, token)
return db.ExecContext(ctx, sqlstr, token)
}
// DeleteTokens runs a custom query as a sql.Result.
func DeleteTokens(ctx context.Context, db DB, user_id string) (sql.Result, error) {
// query
const sqlstr = `DELETE FROM tokens ` +
`WHERE user_id = $1`
// run
logf(sqlstr, user_id)
return db.ExecContext(ctx, sqlstr, user_id)
}