106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type UserRepository struct {
|
|
db *pgxpool.Pool
|
|
}
|
|
|
|
func NewUserRepository(db *pgxpool.Pool) *UserRepository {
|
|
return &UserRepository{db: db}
|
|
}
|
|
|
|
func (r *UserRepository) IsUserExistsByEmail(ctx context.Context, email string) bool {
|
|
var exists bool
|
|
_ = r.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)`, email).Scan(&exists)
|
|
return exists
|
|
}
|
|
|
|
func (r *UserRepository) CreateUser(ctx context.Context, user models.User) (*models.User, error) {
|
|
err := r.db.QueryRow(ctx, `
|
|
INSERT INTO users (email, password_hash, first_name, second_name, birth_date, sex)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING id, is_admin, created_at, updated_at
|
|
`, user.Email, user.PasswordHash, user.FirstName, user.SecondName, user.BirthDate, user.Sex).
|
|
Scan(&user.ID, &user.IsAdmin, &user.CreatedAt, &user.UpdatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create user: %w", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *UserRepository) UpdateUser(ctx context.Context, user models.User) (*models.User, error) {
|
|
err := r.db.QueryRow(ctx, `
|
|
UPDATE users
|
|
SET first_name = $1,
|
|
second_name = $2,
|
|
birth_date = $3,
|
|
sex = $4,
|
|
password_hash = $5,
|
|
updated_at = now()
|
|
WHERE id = $6
|
|
RETURNING updated_at
|
|
`, user.FirstName, user.SecondName, user.BirthDate, user.Sex, user.PasswordHash, user.ID).
|
|
Scan(&user.UpdatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("update user: %w", err)
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *UserRepository) DeleteUser(ctx context.Context, id uuid.UUID) error {
|
|
_, err := r.db.Exec(ctx, `DELETE FROM users WHERE id = $1`, id)
|
|
return err
|
|
}
|
|
|
|
func (r *UserRepository) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
|
var u models.User
|
|
err := r.db.QueryRow(ctx, `
|
|
SELECT id, email, password_hash, first_name, second_name,
|
|
birth_date, sex, is_admin, created_at, updated_at
|
|
FROM users WHERE email = $1
|
|
`, email).Scan(
|
|
&u.ID, &u.Email, &u.PasswordHash,
|
|
&u.FirstName, &u.SecondName,
|
|
&u.BirthDate, &u.Sex, &u.IsAdmin,
|
|
&u.CreatedAt, &u.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &u, nil
|
|
}
|
|
|
|
func (r *UserRepository) GetUserByID(ctx context.Context, id uuid.UUID) (*models.User, error) {
|
|
var u models.User
|
|
err := r.db.QueryRow(ctx, `
|
|
SELECT id, email, password_hash, first_name, second_name,
|
|
birth_date, sex, is_admin, created_at, updated_at
|
|
FROM users WHERE id = $1
|
|
`, id).Scan(
|
|
&u.ID, &u.Email, &u.PasswordHash,
|
|
&u.FirstName, &u.SecondName,
|
|
&u.BirthDate, &u.Sex, &u.IsAdmin,
|
|
&u.CreatedAt, &u.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &u, nil
|
|
}
|
|
|
|
func (r *UserRepository) CountUsers(ctx context.Context) (int, error) {
|
|
var count int
|
|
if err := r.db.QueryRow(ctx, `SELECT COUNT(*) FROM users`).Scan(&count); err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|