hiling_go/db/queries/locations.sql

60 lines
1.7 KiB
SQL

-- name: GetListLocations :many
SELECT * FROM locations;
-- name: GetListRecentLocationsWithRatings :many
SELECT
l.id,
name,
thumbnail,
COALESCE(re.regency_name, '') as regency_name,
COALESCE(pr.province_name, '') as province_name,
(SELECT COALESCE(SUM(score), 0) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_score,
(SELECT COUNT(id) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_count,
(SELECT COALESCE(SUM(score), 0) from reviews re where re.is_from_critic = false and re.location_id = l.id) as user_score,
(SELECT COUNT(id) from reviews re where re.is_from_critic = false and re.location_id = l.id) as user_count
FROM locations l
JOIN regencies re on re.id = l.regency_id
JOIN provinces pr on re.province_id = pr.id
WHERE approved_by IS NOT NULL
ORDER BY l.created_at ASC
LIMIT $1;
-- name: GetLocation :one
SELECT
l.id,
l.name,
l.address,
COALESCE(l.google_maps_link, '') as google_maps_link,
l.thumbnail,
l.submitted_by,
COALESCE(r.regency_name, '') as regency_name,
COALESCE(p.province_name, '') as province_name,
COALESCE(r2.region_name, '') as region_name,
u.username as submitted_by_user
FROM locations l
JOIN regencies r on r.id = l.regency_id
JOIN provinces p on p.id = r.province_id
JOIN regions r2 on r2.id = p.region_id
JOIN users u on l.approved_by = u.id
WHERE l.id = $1;
-- name: CreateLocation :exec
INSERT INTO locations(
address,
name,
submitted_by,
regency_id,
google_maps_link
) values (
$1, $2, $3, $4, $5
);
-- name: GetLocationTag :many
SELECT
name
FROM tags
WHERE tags_type = 'location'
AND
target_id = $1
AND
approved_by IS NOT NULL;