-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.sql
More file actions
59 lines (54 loc) · 2.14 KB
/
Copy pathdatabase.sql
File metadata and controls
59 lines (54 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- Run this in the Supabase SQL Editor
-- 1. Create a table for crowdsourced ratings
CREATE TABLE IF NOT EXISTS public.ratings (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
location_name TEXT NOT NULL,
lat DOUBLE PRECISION NOT NULL,
lon DOUBLE PRECISION NOT NULL,
noise_score INTEGER NOT NULL CHECK (noise_score >= 1 AND noise_score <= 5),
air_score INTEGER NOT NULL CHECK (air_score >= 1 AND air_score <= 5),
water_score INTEGER NOT NULL CHECK (water_score >= 1 AND water_score <= 5),
safety_score INTEGER NOT NULL CHECK (safety_score >= 1 AND safety_score <= 5),
note TEXT,
-- We can round lat/lon to 3 decimal places to group ratings by approximate ~100m areas
location_hash TEXT GENERATED ALWAYS AS (
location_name || '_' || ROUND(lat::numeric, 3)::text || '_' || ROUND(lon::numeric, 3)::text
) STORED
);
-- 2. Set up Row Level Security (RLS)
ALTER TABLE public.ratings ENABLE ROW LEVEL SECURITY;
-- Allow anyone to read ratings
CREATE POLICY "Allow public read access on ratings"
ON public.ratings
FOR SELECT
USING (true);
-- Allow authenticated users to insert their own ratings
CREATE POLICY "Allow authenticated users to insert ratings"
ON public.ratings
FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- 3. Create a view or function to get average ratings for a location area
CREATE OR REPLACE FUNCTION get_location_averages(p_lat DOUBLE PRECISION, p_lon DOUBLE PRECISION)
RETURNS TABLE (
avg_noise DOUBLE PRECISION,
avg_air DOUBLE PRECISION,
avg_water DOUBLE PRECISION,
avg_safety DOUBLE PRECISION,
rating_count BIGINT
) AS $$
BEGIN
RETURN QUERY
SELECT
AVG(noise_score)::DOUBLE PRECISION,
AVG(air_score)::DOUBLE PRECISION,
AVG(water_score)::DOUBLE PRECISION,
AVG(safety_score)::DOUBLE PRECISION,
COUNT(*)
FROM public.ratings
-- Match ratings within roughly ~1km radius (approx 0.01 degrees)
WHERE lat BETWEEN p_lat - 0.01 AND p_lat + 0.01
AND lon BETWEEN p_lon - 0.01 AND p_lon + 0.01;
END;
$$ LANGUAGE plpgsql;