generated from VLADIMIR/template
110 lines
1.8 KiB
Go
110 lines
1.8 KiB
Go
package users_repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"evening_detective_server/internal/repos"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var (
|
|
ErrUserNotFound = errors.New("Пользователь не найден")
|
|
)
|
|
|
|
type UsersRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewUserRepo(pool *pgxpool.Pool) *UsersRepo {
|
|
return &UsersRepo{
|
|
pool: pool,
|
|
}
|
|
}
|
|
|
|
func (s *UsersRepo) AddUser(
|
|
ctx context.Context,
|
|
username string,
|
|
email string,
|
|
passwordHash string,
|
|
role string,
|
|
) error {
|
|
tag, err := s.pool.Exec(
|
|
ctx,
|
|
`INSERT INTO users (username, email, password_hash, role)
|
|
SELECT $1, $2, $3, $4
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM users
|
|
WHERE username = $5 OR email = $6
|
|
)`,
|
|
username,
|
|
email,
|
|
passwordHash,
|
|
role,
|
|
|
|
username,
|
|
email,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return errors.New("username или email уже используется")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *UsersRepo) UpdateUserPassword(
|
|
ctx context.Context,
|
|
email string,
|
|
passwordHash string,
|
|
) error {
|
|
tag, err := s.pool.Exec(
|
|
ctx,
|
|
`UPDATE users
|
|
SET password_hash = $1
|
|
WHERE email = $2`,
|
|
passwordHash,
|
|
email,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrUserNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *UsersRepo) GetUserByEmail(
|
|
ctx context.Context,
|
|
email string,
|
|
) (*repos.User, error) {
|
|
user := &repos.User{}
|
|
row := s.pool.QueryRow(
|
|
ctx,
|
|
`SELECT id, username, email, password_hash, role, is_active
|
|
FROM users
|
|
WHERE email = $1`,
|
|
email,
|
|
)
|
|
err := row.Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.Email,
|
|
&user.PasswordHash,
|
|
&user.Role,
|
|
&user.IsActive,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return user, nil
|
|
}
|