Yimaru-BackEnd/db/query/company.sql

56 lines
1.5 KiB
SQL

-- name: CreateCompany :one
INSERT INTO companies (
name,
slug,
admin_id,
wallet_id,
deducted_percentage,
is_active
)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
-- name: GetAllCompanies :many
SELECT *
FROM companies_details
WHERE (
name ILIKE '%' || sqlc.narg('query') || '%'
OR admin_first_name ILIKE '%' || sqlc.narg('query') || '%'
OR admin_last_name ILIKE '%' || sqlc.narg('query') || '%'
OR admin_phone_number ILIKE '%' || sqlc.narg('query') || '%'
OR sqlc.narg('query') IS NULL
)
AND (
created_at > sqlc.narg('created_before')
OR sqlc.narg('created_before') IS NULL
)
AND (
created_at < sqlc.narg('created_after')
OR sqlc.narg('created_after') IS NULL
);
-- name: GetCompanyByID :one
SELECT *
FROM companies_details
WHERE id = $1;
-- name: GetCompanyUsingSlug :one
SELECT *
FROM companies
WHERE slug = $1;
-- name: SearchCompanyByName :many
SELECT *
FROM companies_details
WHERE name ILIKE '%' || $1 || '%';
-- name: UpdateCompany :exec
UPDATE companies
SET name = COALESCE(sqlc.narg(name), name),
admin_id = COALESCE(sqlc.narg(admin_id), admin_id),
is_active = COALESCE(sqlc.narg(is_active), is_active),
deducted_percentage = COALESCE(
sqlc.narg(deducted_percentage),
deducted_percentage
),
slug = COALESCE(sqlc.narg(slug), slug),
updated_at = CURRENT_TIMESTAMP
WHERE id = $1;
-- name: DeleteCompany :exec
DELETE FROM companies
WHERE id = $1;