2026-05-14 22:10:40 +00:00
|
|
|
-- name: ListTablosByUser :many
|
2026-05-17 14:27:30 +00:00
|
|
|
SELECT id, user_id, title, description, color, created_at, updated_at, status
|
2026-05-14 22:10:40 +00:00
|
|
|
FROM tablos
|
|
|
|
|
WHERE user_id = $1
|
|
|
|
|
ORDER BY created_at DESC;
|
|
|
|
|
|
2026-05-16 08:15:38 +00:00
|
|
|
-- name: ListTablosByUserWithDiscussionUnread :many
|
|
|
|
|
SELECT tablos.id,
|
|
|
|
|
tablos.user_id,
|
|
|
|
|
tablos.title,
|
|
|
|
|
tablos.description,
|
|
|
|
|
tablos.color,
|
|
|
|
|
tablos.created_at,
|
|
|
|
|
tablos.updated_at,
|
2026-05-17 14:27:30 +00:00
|
|
|
tablos.status,
|
2026-05-16 08:15:38 +00:00
|
|
|
COALESCE(count(unread_messages.id), 0)::bigint AS discussion_unread_count
|
|
|
|
|
FROM tablos
|
|
|
|
|
LEFT JOIN discussion_read_states AS read_state
|
|
|
|
|
ON read_state.tablo_id = tablos.id
|
|
|
|
|
AND read_state.user_id = sqlc.arg(user_id)
|
|
|
|
|
LEFT JOIN discussion_messages AS last_read_message
|
|
|
|
|
ON last_read_message.id = read_state.last_read_message_id
|
|
|
|
|
LEFT JOIN discussion_messages AS unread_messages
|
|
|
|
|
ON unread_messages.tablo_id = tablos.id
|
|
|
|
|
AND (
|
|
|
|
|
read_state.last_read_message_id IS NULL
|
|
|
|
|
OR last_read_message.id IS NULL
|
|
|
|
|
OR unread_messages.created_at > last_read_message.created_at
|
|
|
|
|
)
|
|
|
|
|
WHERE tablos.user_id = sqlc.arg(user_id)
|
2026-05-17 14:27:30 +00:00
|
|
|
GROUP BY tablos.id, tablos.user_id, tablos.title, tablos.description, tablos.color, tablos.created_at, tablos.updated_at, tablos.status
|
2026-05-16 08:15:38 +00:00
|
|
|
ORDER BY tablos.created_at DESC;
|
|
|
|
|
|
2026-05-14 22:10:40 +00:00
|
|
|
-- name: GetTabloByID :one
|
2026-05-17 14:27:30 +00:00
|
|
|
SELECT id, user_id, title, description, color, created_at, updated_at, status
|
2026-05-14 22:10:40 +00:00
|
|
|
FROM tablos
|
2026-05-15 06:30:11 +00:00
|
|
|
WHERE id = $1 AND user_id = $2;
|
2026-05-14 22:10:40 +00:00
|
|
|
|
|
|
|
|
-- name: InsertTablo :one
|
|
|
|
|
INSERT INTO tablos (user_id, title, description, color)
|
|
|
|
|
VALUES ($1, $2, $3, $4)
|
2026-05-17 14:27:30 +00:00
|
|
|
RETURNING id, user_id, title, description, color, created_at, updated_at, status;
|
2026-05-14 22:10:40 +00:00
|
|
|
|
|
|
|
|
-- name: UpdateTablo :one
|
|
|
|
|
UPDATE tablos
|
2026-05-15 06:30:11 +00:00
|
|
|
SET title = $2, description = $3, color = $4, updated_at = now()
|
2026-05-14 22:10:40 +00:00
|
|
|
WHERE id = $1
|
2026-05-17 14:27:30 +00:00
|
|
|
RETURNING id, user_id, title, description, color, created_at, updated_at, status;
|
2026-05-14 22:10:40 +00:00
|
|
|
|
|
|
|
|
-- name: DeleteTablo :exec
|
2026-05-15 06:30:11 +00:00
|
|
|
DELETE FROM tablos WHERE id = $1 AND user_id = $2;
|
2026-05-17 14:27:30 +00:00
|
|
|
|
|
|
|
|
-- name: ListTabloProgressByIDs :many
|
|
|
|
|
-- Batch aggregation: one query for all tablos on the dashboard (D-06).
|
|
|
|
|
-- Counts all tasks regardless of etape assignment.
|
|
|
|
|
SELECT
|
|
|
|
|
tablo_id,
|
|
|
|
|
COUNT(*) FILTER (WHERE status = 'done')::int AS done_tasks,
|
|
|
|
|
COUNT(*)::int AS total_tasks
|
|
|
|
|
FROM tasks
|
|
|
|
|
WHERE tablo_id = ANY(@tablo_ids::uuid[])
|
|
|
|
|
GROUP BY tablo_id;
|