Yimaru-BackEnd/db/query/notification.sql

113 lines
2.8 KiB
SQL

-- name: CreateNotification :one
INSERT INTO notifications (
user_id,
receiver_type,
type,
level,
channel,
title,
message,
payload
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
RETURNING *;
-- name: GetNotification :one
SELECT *
FROM notifications
WHERE id = $1
LIMIT 1;
-- name: GetAllNotifications :many
SELECT *
FROM notifications
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;
-- name: GetTotalNotificationCount :one
SELECT COUNT(*)
FROM notifications;
-- name: GetUserNotifications :many
SELECT *
FROM notifications
WHERE user_id = $1
ORDER BY created_at DESC
LIMIT $2 OFFSET $3;
-- name: GetUserNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1;
-- name: CountUnreadNotifications :one
SELECT COUNT(*)
FROM notifications
WHERE user_id = $1
AND is_read = FALSE;
-- name: MarkNotificationAsRead :one
UPDATE notifications
SET is_read = TRUE,
read_at = NOW()
WHERE id = $1
RETURNING *;
-- name: MarkAllUserNotificationsAsRead :exec
UPDATE notifications
SET is_read = TRUE,
read_at = NOW()
WHERE user_id = $1
AND is_read = FALSE;
-- name: MarkNotificationAsUnread :one
UPDATE notifications
SET is_read = FALSE,
read_at = NULL
WHERE id = $1
RETURNING *;
-- name: MarkAllUserNotificationsAsUnread :exec
UPDATE notifications
SET is_read = FALSE,
read_at = NULL
WHERE user_id = $1
AND is_read = TRUE;
-- name: DeleteUserNotifications :exec
DELETE FROM notifications
WHERE user_id = $1;
-- name: GetFilteredNotifications :many
SELECT *
FROM notifications
WHERE
(sqlc.narg('filter_channel')::text IS NULL OR channel = sqlc.narg('filter_channel'))
AND (sqlc.narg('filter_type')::text IS NULL OR type = sqlc.narg('filter_type'))
AND (sqlc.narg('filter_user_id')::bigint IS NULL OR user_id = sqlc.narg('filter_user_id'))
AND (sqlc.narg('filter_is_read')::boolean IS NULL OR is_read = sqlc.narg('filter_is_read'))
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR created_at >= sqlc.narg('filter_after'))
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR created_at <= sqlc.narg('filter_before'))
ORDER BY created_at DESC
LIMIT @page_limit OFFSET @page_offset;
-- name: GetFilteredNotificationCount :one
SELECT COUNT(*)
FROM notifications
WHERE
(sqlc.narg('filter_channel')::text IS NULL OR channel = sqlc.narg('filter_channel'))
AND (sqlc.narg('filter_type')::text IS NULL OR type = sqlc.narg('filter_type'))
AND (sqlc.narg('filter_user_id')::bigint IS NULL OR user_id = sqlc.narg('filter_user_id'))
AND (sqlc.narg('filter_is_read')::boolean IS NULL OR is_read = sqlc.narg('filter_is_read'))
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR created_at >= sqlc.narg('filter_after'))
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR created_at <= sqlc.narg('filter_before'));