Files
shop10/internal/repository/users.go
T

81 lines
2.0 KiB
Go

package repository
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"shop/internal/models"
)
type UserRepository struct {
pool *pgxpool.Pool
}
func NewUserRepository(pool *pgxpool.Pool) *UserRepository {
return &UserRepository{pool: pool}
}
func (r *UserRepository) Create(ctx context.Context, email, passwordHash, name string) (*models.User, error) {
var u models.User
err := r.pool.QueryRow(ctx, `
INSERT INTO users (email, password_hash, name)
VALUES ($1, $2, $3)
RETURNING id, email, name, created_at`,
email, passwordHash, name,
).Scan(&u.ID, &u.Email, &u.Name, &u.CreatedAt)
if err != nil {
return nil, err
}
return &u, nil
}
func (r *UserRepository) ByID(ctx context.Context, id int) (*models.User, error) {
var u models.User
err := r.pool.QueryRow(ctx, `
SELECT id, email, name, created_at FROM users WHERE id = $1`, id,
).Scan(&u.ID, &u.Email, &u.Name, &u.CreatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
return &u, nil
}
func (r *UserRepository) ByEmailWithHash(ctx context.Context, email string) (*models.User, string, error) {
var u models.User
var hash string
err := r.pool.QueryRow(ctx, `
SELECT id, email, name, created_at, password_hash
FROM users WHERE email = $1`, email,
).Scan(&u.ID, &u.Email, &u.Name, &u.CreatedAt, &hash)
if errors.Is(err, pgx.ErrNoRows) {
return nil, "", nil
}
if err != nil {
return nil, "", err
}
return &u, hash, nil
}
func (r *UserRepository) UpdateName(ctx context.Context, userID int, name string) error {
_, err := r.pool.Exec(ctx, `UPDATE users SET name = $1 WHERE id = $2`, name, userID)
return err
}
func (r *UserRepository) Count(ctx context.Context) (int, error) {
var n int
err := r.pool.QueryRow(ctx, `SELECT COUNT(*) FROM users`).Scan(&n)
return n, err
}
func IsUniqueViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == "23505"
}