Add FindUser method.

Returns the user by id.
This commit is contained in:
Victor Vrantchan
2021-02-20 21:50:50 -05:00
parent b69f92e907
commit 2f601dd30d
2 changed files with 43 additions and 0 deletions

View File

@@ -62,6 +62,26 @@ func (d *Postgres) ConfirmUser(ctx context.Context, confirmation string) error {
return nil
}
func (d *Postgres) FindUser(ctx context.Context, id string) (*User, error) {
u := &User{}
q := fmt.Sprintf(`SELECT %s FROM users WHERE id = $1;`, strings.Join(columns(), `, `))
if err := d.db.QueryRow(ctx, q, id).Scan(
&u.ID,
&u.Username,
&u.Email,
&u.Password,
&u.Salt,
&u.ConfirmationHash,
&u.CreatedAt,
&u.UpdatedAt,
); err != nil {
return nil, err
}
return u, nil
}
func (d *Postgres) FindUserByEmail(ctx context.Context, email string) (*User, error) {
if email == "" {
return nil, Error{invalid: constraints["chk_email_not_empty"]}

View File

@@ -88,6 +88,29 @@ func (d *SQLite) ConfirmUser(ctx context.Context, confirmation string) error {
return nil
}
func (d *SQLite) FindUser(ctx context.Context, id string) (*User, error) {
conn := d.db.Get(ctx)
if conn == nil {
return nil, context.Canceled
}
defer d.db.Put(conn)
stmt := conn.Prep(fmt.Sprintf(
`SELECT %s FROM users WHERE id = $id;`, strings.Join(columns(), `, `)))
stmt.SetText("$id", id)
if found, err := stmt.Step(); err != nil {
return nil, err
} else if !found {
return nil, fmt.Errorf("did not find user with id %q", id)
}
usr, err := sqliteUser(stmt)
if err != nil {
return nil, err
}
return usr, stmt.Reset()
}
func (d *SQLite) FindUserByEmail(ctx context.Context, email string) (*User, error) {
conn := d.db.Get(ctx)
if conn == nil {