Files
panelhosting/internal/repository/user.go
T

45 lines
1.2 KiB
Go

package repository
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/panelhosting/panel/internal/models"
)
type UserRepository struct {
pool *pgxpool.Pool
}
func NewUserRepository(pool *pgxpool.Pool) *UserRepository {
return &UserRepository{pool: pool}
}
func (r *UserRepository) Count(ctx context.Context) (int64, error) {
var count int64
err := r.pool.QueryRow(ctx, `SELECT COUNT(*) FROM users`).Scan(&count)
if err != nil {
return 0, fmt.Errorf("count users: %w", err)
}
return count, nil
}
func (r *UserRepository) Create(ctx context.Context, email, username, passwordHash string, role models.UserRole, status models.UserStatus) (*models.User, error) {
const q = `
INSERT INTO users (email, username, password_hash, role, status)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, uuid, email, username, role, status, locale, timezone, last_login_at, created_at, updated_at
`
var u models.User
err := r.pool.QueryRow(ctx, q, email, username, passwordHash, role, status).Scan(
&u.ID, &u.UUID, &u.Email, &u.Username, &u.Role, &u.Status,
&u.Locale, &u.Timezone, &u.LastLoginAt, &u.CreatedAt, &u.UpdatedAt,
)
if err != nil {
return nil, fmt.Errorf("create user: %w", err)
}
return &u, nil
}