65 lines
1.8 KiB
SQL
65 lines
1.8 KiB
SQL
-- name: InsertEvent :one
|
|
INSERT INTO "event" (
|
|
name, description, total_amount, default_currency, owner_id, created_at, updated_at
|
|
) VALUES ( $1, $2, $3, $4, $5, $6, $7 )
|
|
RETURNING *;
|
|
|
|
-- name: ListEventsByUserID :many
|
|
SELECT
|
|
e.id,
|
|
e.name,
|
|
e.description,
|
|
e.created_at,
|
|
json_build_object(
|
|
'id', o.id,
|
|
'first_name', o.first_name,
|
|
'last_name', o.last_name
|
|
) AS owner
|
|
FROM "event" e
|
|
JOIN "participation" p ON p.event_id = e.id -- participation linked with the event
|
|
JOIN "user" o ON o.id = e.owner_id -- get the owner info
|
|
WHERE e.id IN (
|
|
SELECT pt.event_id FROM participation pt WHERE pt.user_id = $1 -- consider the events participated by user_id
|
|
)
|
|
GROUP BY
|
|
e.id, e.name, e.description, e.created_at,
|
|
o.id, o.first_name, o.last_name;
|
|
|
|
-- name: GetEventByID :one
|
|
SELECT
|
|
e.id,
|
|
e.name,
|
|
e.description,
|
|
e.total_amount,
|
|
e.default_currency,
|
|
e.created_at,
|
|
e.updated_at,
|
|
json_build_object(
|
|
'id', o.id,
|
|
'first_name', o.first_name,
|
|
'last_name', o.last_name
|
|
) AS owner,
|
|
json_agg(
|
|
json_build_object(
|
|
'id', u.id,
|
|
'first_name', u.first_name,
|
|
'last_name', u.last_name
|
|
)
|
|
) AS users -- Aggregation for users in the event
|
|
FROM "event" e
|
|
JOIN "participation" p ON p.event_id = e.id -- participation linked with the event
|
|
JOIN "user" u ON u.id = p.user_id -- and the query user
|
|
JOIN "user" o ON o.id = e.owner_id -- get the owner info
|
|
WHERE e.id = $1
|
|
GROUP BY
|
|
e.id, e.name, e.description, e.created_at, e.updated_at,
|
|
e.total_amount, e.default_currency,
|
|
o.id, o.first_name, o.last_name;
|
|
|
|
-- name: UpdateEventByID :exec
|
|
UPDATE "event"
|
|
SET name = $2, description = $3, updated_at = $4
|
|
WHERE id = $1;
|
|
|
|
|