252 lines
4.5 KiB
SQL
252 lines
4.5 KiB
SQL
-- name: IsUserPending :one
|
|
SELECT
|
|
CASE WHEN status = 'PENDING' THEN true ELSE false END AS is_pending
|
|
FROM users
|
|
WHERE user_name = $1
|
|
LIMIT 1;
|
|
|
|
-- name: IsUserNameUnique :one
|
|
SELECT
|
|
CASE WHEN COUNT(*) = 0 THEN true ELSE false END AS is_unique
|
|
FROM users
|
|
WHERE user_name = $1;
|
|
|
|
|
|
-- name: CreateUser :one
|
|
INSERT INTO users (
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
password,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
preferred_language,
|
|
updated_at
|
|
)
|
|
VALUES (
|
|
$1, -- first_name
|
|
$2, -- last_name
|
|
$3, -- user_name
|
|
$4, -- email
|
|
$5, -- phone_number
|
|
$6, -- role
|
|
$7, -- password (BYTEA)
|
|
$8, -- age
|
|
$9, -- education_level
|
|
$10, -- country
|
|
$11, -- region
|
|
$12, -- email_verified
|
|
$13, -- phone_verified
|
|
$14, -- status (PENDING | ACTIVE)
|
|
$15, -- profile_completed
|
|
$16, -- preferred_language
|
|
CURRENT_TIMESTAMP
|
|
)
|
|
RETURNING
|
|
id,
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
preferred_language,
|
|
created_at,
|
|
updated_at;
|
|
|
|
-- name: GetUserByID :one
|
|
SELECT *
|
|
FROM users
|
|
WHERE id = $1;
|
|
|
|
-- name: GetAllUsers :many
|
|
SELECT
|
|
COUNT(*) OVER () AS total_count,
|
|
id,
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
preferred_language,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE (
|
|
role = $1 OR $1 IS NULL
|
|
)
|
|
AND (
|
|
first_name ILIKE '%' || sqlc.narg('query') || '%'
|
|
OR last_name ILIKE '%' || sqlc.narg('query') || '%'
|
|
OR phone_number ILIKE '%' || sqlc.narg('query') || '%'
|
|
OR email ILIKE '%' || sqlc.narg('query') || '%'
|
|
OR sqlc.narg('query') IS NULL
|
|
)
|
|
AND (
|
|
created_at >= sqlc.narg('created_after')
|
|
OR sqlc.narg('created_after') IS NULL
|
|
)
|
|
AND (
|
|
created_at <= sqlc.narg('created_before')
|
|
OR sqlc.narg('created_before') IS NULL
|
|
)
|
|
LIMIT sqlc.narg('limit')
|
|
OFFSET sqlc.narg('offset');
|
|
|
|
-- name: GetTotalUsers :one
|
|
SELECT COUNT(*)
|
|
FROM users
|
|
WHERE (role = $1 OR $1 IS NULL);
|
|
|
|
-- name: SearchUserByNameOrPhone :many
|
|
SELECT
|
|
id,
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE (
|
|
first_name ILIKE '%' || $1 || '%'
|
|
OR last_name ILIKE '%' || $1 || '%'
|
|
OR phone_number ILIKE '%' || $1 || '%'
|
|
OR email ILIKE '%' || $1 || '%'
|
|
)
|
|
AND (
|
|
role = sqlc.narg('role')
|
|
OR sqlc.narg('role') IS NULL
|
|
);
|
|
|
|
-- name: UpdateUser :exec
|
|
UPDATE users
|
|
SET
|
|
first_name = $1,
|
|
last_name = $2,
|
|
status = $3,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $4;
|
|
|
|
-- name: DeleteUser :exec
|
|
DELETE FROM users
|
|
WHERE id = $1;
|
|
|
|
-- name: CheckPhoneEmailExist :one
|
|
SELECT
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM users u1
|
|
WHERE u1.phone_number = $1
|
|
) AS phone_exists,
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM users u2
|
|
WHERE u2.email = $2
|
|
) AS email_exists;
|
|
|
|
-- name: GetUserByUserName :one
|
|
SELECT
|
|
id,
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
password,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
last_login,
|
|
profile_picture_url,
|
|
preferred_language,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE user_name = $1 AND $1 IS NOT NULL
|
|
LIMIT 1;
|
|
|
|
-- name: GetUserByEmailPhone :one
|
|
SELECT
|
|
id,
|
|
first_name,
|
|
last_name,
|
|
user_name,
|
|
email,
|
|
phone_number,
|
|
role,
|
|
password,
|
|
age,
|
|
education_level,
|
|
country,
|
|
region,
|
|
email_verified,
|
|
phone_verified,
|
|
status,
|
|
profile_completed,
|
|
last_login,
|
|
profile_picture_url,
|
|
preferred_language,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE (email = $1 AND $1 IS NOT NULL)
|
|
OR (phone_number = $2 AND $2 IS NOT NULL)
|
|
LIMIT 1;
|
|
|
|
-- name: UpdatePassword :exec
|
|
UPDATE users
|
|
SET
|
|
password = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE email = $2 OR phone_number = $3;
|
|
|
|
-- name: UpdateUserStatus :exec
|
|
UPDATE users
|
|
SET
|
|
status = $1,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $2;
|