Initial commit: VPN panel on Go, PostgreSQL 17, Docker, Xray-core
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"vpn-panel/internal/models"
|
||||
)
|
||||
|
||||
var ErrAdminExists = errors.New("администратор уже зарегистрирован")
|
||||
|
||||
type UserStore struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserStore(pool *pgxpool.Pool) *UserStore {
|
||||
return &UserStore{pool: pool}
|
||||
}
|
||||
|
||||
func (s *UserStore) HasAdmin(ctx context.Context) (bool, error) {
|
||||
var n int
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT COUNT(*) FROM users WHERE role = 'admin'`,
|
||||
).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (s *UserStore) CreateAdmin(ctx context.Context, email, passwordHash string) (*models.User, error) {
|
||||
has, err := s.HasAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if has {
|
||||
return nil, ErrAdminExists
|
||||
}
|
||||
|
||||
var u models.User
|
||||
err = s.pool.QueryRow(ctx, `
|
||||
INSERT INTO users (email, password_hash, role)
|
||||
VALUES ($1, $2, 'admin')
|
||||
RETURNING id, email, password_hash, role, created_at
|
||||
`, email, passwordHash).Scan(
|
||||
&u.ID, &u.Email, &u.PasswordHash, &u.Role, &u.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
var u models.User
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT id, email, password_hash, role, created_at
|
||||
FROM users WHERE email = $1
|
||||
`, email).Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Role, &u.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *UserStore) CountUsers(ctx context.Context) (int, error) {
|
||||
var n int
|
||||
err := s.pool.QueryRow(ctx, `SELECT COUNT(*) FROM users`).Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetAdminID(ctx context.Context) (uuid.UUID, bool, error) {
|
||||
var id uuid.UUID
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT id FROM users WHERE role = 'admin' LIMIT 1`,
|
||||
).Scan(&id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return uuid.Nil, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, false, err
|
||||
}
|
||||
return id, true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user