Учесть замечания ревью: читать timestamptz в UTC, встроить списки колонок, не требовать каноническую PHC-строку, уточнить замеры отказов

This commit is contained in:
2026-10-21 15:08:17 +03:00
parent 956b84f2d7
commit 7aa86d36ba
7 changed files with 37 additions and 23 deletions
+3 -6
View File
@@ -16,8 +16,6 @@ import (
"messenger/internal/user"
)
const userColumns = `id, username, display_name, password_hash, created_at`
type Users struct {
pool *pgxpool.Pool
}
@@ -32,7 +30,7 @@ func NewUsers(pool *pgxpool.Pool) *Users {
// concurrent registrations of one username cannot both succeed.
func (r *Users) Create(ctx context.Context, u user.User) error {
_, err := r.pool.Exec(ctx,
`INSERT INTO users (`+userColumns+`) VALUES ($1, $2, $3, $4, $5)`,
`INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`,
u.ID, u.Username, u.DisplayName, u.PasswordHash, u.CreatedAt)
if postgres.IsUniqueViolation(err, "users_username_key") {
return user.ErrUsernameTaken
@@ -44,11 +42,11 @@ func (r *Users) Create(ctx context.Context, u user.User) error {
}
func (r *Users) GetByID(ctx context.Context, id uuid.UUID) (user.User, error) {
return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE id = $1`, id)
return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1`, id)
}
func (r *Users) GetByUsername(ctx context.Context, username string) (user.User, error) {
return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE username = $1`, username)
return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1`, username)
}
func (r *Users) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
@@ -72,6 +70,5 @@ func (r *Users) one(ctx context.Context, query string, arg any) (user.User, erro
if err != nil {
return user.User{}, fmt.Errorf("select user: %w", postgres.Classify(err))
}
u.CreatedAt = u.CreatedAt.UTC()
return u, nil
}