refactor: migrate from raw pgx to GORM, unify ErrNoRows, cleanup auth
This commit is contained in:
+36
-42
@@ -5,18 +5,24 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var ErrNoRows = pgx.ErrNoRows
|
||||
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
type OrgRepository interface {
|
||||
Create(ctx context.Context, org *Organization) error
|
||||
FindByID(ctx context.Context, id string) (*Organization, error)
|
||||
FindAll(ctx context.Context, limit, offset int) ([]Organization, error)
|
||||
Count(ctx context.Context) (int, error)
|
||||
Update(ctx context.Context, org *Organization) error
|
||||
Delete(ctx context.Context, id string) (bool, 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) Create(ctx context.Context, org *Organization) error {
|
||||
@@ -24,54 +30,42 @@ func (r *Repository) Create(ctx context.Context, org *Organization) error {
|
||||
now := time.Now().UTC()
|
||||
org.CreatedAt = now
|
||||
org.UpdatedAt = now
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO organizations (id, name, slug, created_at, updated_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
org.ID, org.Name, org.Slug, org.CreatedAt, org.UpdatedAt,
|
||||
)
|
||||
return err
|
||||
return r.db.WithContext(ctx).Create(org).Error
|
||||
}
|
||||
|
||||
func (r *Repository) FindByID(ctx context.Context, id string) (*Organization, error) {
|
||||
var org Organization
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, name, slug, created_at, updated_at FROM organizations WHERE id = $1`, id,
|
||||
).Scan(&org.ID, &org.Name, &org.Slug, &org.CreatedAt, &org.UpdatedAt)
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&org).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &org, nil
|
||||
}
|
||||
|
||||
func (r *Repository) FindAll(ctx context.Context) ([]Organization, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, name, slug, created_at, updated_at FROM organizations ORDER BY created_at DESC`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
func (r *Repository) FindAll(ctx context.Context, limit, offset int) ([]Organization, error) {
|
||||
var orgs []Organization
|
||||
for rows.Next() {
|
||||
var org Organization
|
||||
if err := rows.Scan(&org.ID, &org.Name, &org.Slug, &org.CreatedAt, &org.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orgs = append(orgs, org)
|
||||
}
|
||||
return orgs, rows.Err()
|
||||
err := r.db.WithContext(ctx).
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&orgs).Error
|
||||
return orgs, err
|
||||
}
|
||||
|
||||
func (r *Repository) Count(ctx context.Context) (int, error) {
|
||||
var total int64
|
||||
err := r.db.WithContext(ctx).Model(&Organization{}).Count(&total).Error
|
||||
return int(total), err
|
||||
}
|
||||
|
||||
func (r *Repository) Update(ctx context.Context, org *Organization) error {
|
||||
org.UpdatedAt = time.Now().UTC()
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`UPDATE organizations SET name = $1, updated_at = $2 WHERE id = $3`,
|
||||
org.Name, org.UpdatedAt, org.ID,
|
||||
)
|
||||
return err
|
||||
return r.db.WithContext(ctx).Model(org).Update("name", org.Name).Error
|
||||
}
|
||||
|
||||
func (r *Repository) Delete(ctx context.Context, id string) error {
|
||||
_, err := r.pool.Exec(ctx, `DELETE FROM organizations WHERE id = $1`, id)
|
||||
return err
|
||||
func (r *Repository) Delete(ctx context.Context, id string) (bool, error) {
|
||||
result := r.db.WithContext(ctx).Delete(&Organization{}, "id = ?", id)
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return result.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user