diff --git a/internal/data/user/user_postgres.go b/internal/data/user/user_postgres.go index bb889155..646e68b6 100644 --- a/internal/data/user/user_postgres.go +++ b/internal/data/user/user_postgres.go @@ -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"]} diff --git a/internal/data/user/user_sqlite.go b/internal/data/user/user_sqlite.go index 37784a63..089e02c7 100644 --- a/internal/data/user/user_sqlite.go +++ b/internal/data/user/user_sqlite.go @@ -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 {