refactor: migrate from raw pgx to GORM, unify ErrNoRows, cleanup auth
This commit is contained in:
+49
-48
@@ -5,33 +5,39 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
type UserRepository interface {
|
||||
CreateUser(ctx context.Context, user *User) error
|
||||
FindByEmail(ctx context.Context, email string) (*User, error)
|
||||
FindByID(ctx context.Context, id string) (*User, error)
|
||||
CreateRefreshToken(ctx context.Context, doc *RefreshTokenDoc) error
|
||||
FindRefreshTokenByHash(ctx context.Context, hash string) (*RefreshTokenDoc, error)
|
||||
DeleteRefreshToken(ctx context.Context, id string) error
|
||||
DeleteRefreshTokenByHash(ctx context.Context, hash string) (bool, error)
|
||||
UpdateUserUsername(ctx context.Context, id, username string) error
|
||||
UpdateUserPassword(ctx context.Context, id, passwordHash string) error
|
||||
DeleteExpiredRefreshTokens(ctx context.Context) error
|
||||
}
|
||||
|
||||
func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
return &Repository{pool: pool}
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) CreateUser(ctx context.Context, user *User) error {
|
||||
user.ID = uuid.New().String()
|
||||
user.CreatedAt = time.Now().UTC()
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO users (id, username, email, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
user.ID, user.Username, user.Email, user.PasswordHash, user.CreatedAt,
|
||||
)
|
||||
return err
|
||||
return r.db.WithContext(ctx).Create(user).Error
|
||||
}
|
||||
|
||||
func (r *Repository) FindByEmail(ctx context.Context, email string) (*User, error) {
|
||||
var user User
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, username, email, password_hash, created_at FROM users WHERE email = $1`, email,
|
||||
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt)
|
||||
err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -40,9 +46,7 @@ func (r *Repository) FindByEmail(ctx context.Context, email string) (*User, erro
|
||||
|
||||
func (r *Repository) FindByID(ctx context.Context, id string) (*User, error) {
|
||||
var user User
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, username, email, password_hash, created_at FROM users WHERE id = $1`, id,
|
||||
).Scan(&user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt)
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -52,18 +56,18 @@ func (r *Repository) FindByID(ctx context.Context, id string) (*User, error) {
|
||||
func (r *Repository) CreateRefreshToken(ctx context.Context, doc *RefreshTokenDoc) error {
|
||||
doc.ID = uuid.New().String()
|
||||
doc.CreatedAt = time.Now().UTC()
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO refresh_tokens (id, user_id, token_hash, expires_at, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
doc.ID, doc.UserID, doc.TokenHash, doc.ExpiresAt, doc.CreatedAt,
|
||||
)
|
||||
return err
|
||||
return r.db.WithContext(ctx).Create(doc).Error
|
||||
}
|
||||
|
||||
func (r *Repository) FindRefreshTokenByHash(ctx context.Context, hash string) (*RefreshTokenDoc, error) {
|
||||
func (r *Repository) FindRefreshTokenByHash(
|
||||
ctx context.Context,
|
||||
hash string,
|
||||
) (*RefreshTokenDoc, error) {
|
||||
var doc RefreshTokenDoc
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, user_id, token_hash, expires_at, created_at FROM refresh_tokens WHERE token_hash = $1 AND expires_at > NOW()`, hash,
|
||||
).Scan(&doc.ID, &doc.UserID, &doc.TokenHash, &doc.ExpiresAt, &doc.CreatedAt)
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("token_hash = ? AND expires_at > NOW()", hash).
|
||||
First(&doc).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -71,31 +75,28 @@ func (r *Repository) FindRefreshTokenByHash(ctx context.Context, hash string) (*
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteRefreshToken(ctx context.Context, id string) error {
|
||||
_, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE id = $1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateUserUsername(ctx context.Context, id, username string) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE users SET username = $1 WHERE id = $2`, username, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateUserPassword(ctx context.Context, id, passwordHash string) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE users SET password_hash = $1 WHERE id = $2`, passwordHash, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteExpiredRefreshTokens(ctx context.Context) error {
|
||||
_, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE expires_at <= NOW()`)
|
||||
return err
|
||||
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&RefreshTokenDoc{}).Error
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteRefreshTokenByHash(ctx context.Context, hash string) (bool, error) {
|
||||
tag, err := r.pool.Exec(ctx, `DELETE FROM refresh_tokens WHERE token_hash = $1`, hash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
result := r.db.WithContext(ctx).Where("token_hash = ?", hash).Delete(&RefreshTokenDoc{})
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
return result.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
var ErrNoRows = pgx.ErrNoRows
|
||||
func (r *Repository) UpdateUserUsername(ctx context.Context, id, username string) error {
|
||||
return r.db.WithContext(ctx).Model(&User{}).Where("id = ?", id).
|
||||
Update("username", username).Error
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateUserPassword(ctx context.Context, id, passwordHash string) error {
|
||||
return r.db.WithContext(ctx).Model(&User{}).Where("id = ?", id).
|
||||
Update("password_hash", passwordHash).Error
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteExpiredRefreshTokens(ctx context.Context) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("expires_at <= NOW()").Delete(&RefreshTokenDoc{}).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user