@@ -223,25 +223,81 @@ CREATE TABLE IF NOT EXISTS user_solved_problems (
223223 solved_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
224224 UNIQUE(user_id, problem_id)
225225);
226-
227226-- Create RLS policies for user_solved_problems table
228227ALTER TABLE user_solved_problems ENABLE ROW LEVEL SECURITY;
229-
230228-- Users can read all solved problems (needed for displaying statistics)
231229CREATE POLICY " Anyone can read solved problems" ON user_solved_problems FOR
232230SELECT USING (true);
233-
234231-- Users can only mark their own problems as solved
235232CREATE POLICY " Users can mark their own solved problems" ON user_solved_problems FOR
236233INSERT WITH CHECK (auth .uid () = user_id);
237-
238234-- Users can only update their own solved problems
239235CREATE POLICY " Users can update their own solved problems" ON user_solved_problems FOR
240236UPDATE USING (auth .uid () = user_id);
241-
242237-- Users can only delete their own solved problems
243- CREATE POLICY " Users can delete their own solved problems" ON user_solved_problems FOR
244- DELETE USING (auth .uid () = user_id);
245-
238+ CREATE POLICY " Users can delete their own solved problems" ON user_solved_problems FOR DELETE USING (auth .uid () = user_id);
246239-- Grant access to authenticated users
247240GRANT ALL ON user_solved_problems TO authenticated;
241+ -- Create contests table to track programming contests
242+ CREATE TABLE IF NOT EXISTS contests (
243+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
244+ name TEXT NOT NULL ,
245+ url TEXT NOT NULL ,
246+ type TEXT ,
247+ duration_seconds INTEGER NOT NULL ,
248+ difficulty INTEGER ,
249+ added_by TEXT NOT NULL ,
250+ added_by_url TEXT NOT NULL ,
251+ date_added TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
252+ likes INTEGER DEFAULT 0 ,
253+ dislikes INTEGER DEFAULT 0
254+ );
255+ -- Create RLS policies for contests table
256+ ALTER TABLE contests ENABLE ROW LEVEL SECURITY;
257+ -- Allow anyone to read contests
258+ CREATE POLICY " Anyone can read contests" ON contests FOR
259+ SELECT USING (true);
260+ -- Only admins can insert contests
261+ CREATE POLICY " Only admins can insert contests" ON contests FOR
262+ INSERT WITH CHECK (
263+ EXISTS (
264+ SELECT 1
265+ FROM user_roles
266+ WHERE user_id = auth .uid ()
267+ AND role = ' admin'
268+ )
269+ );
270+ -- Only admins can update contests
271+ CREATE POLICY " Only admins can update contests" ON contests FOR
272+ UPDATE USING (
273+ EXISTS (
274+ SELECT 1
275+ FROM user_roles
276+ WHERE user_id = auth .uid ()
277+ AND role = ' admin'
278+ )
279+ );
280+ -- Create user_contest_participation table to track which contests users have participated in
281+ CREATE TABLE IF NOT EXISTS user_contest_participation (
282+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
283+ user_id UUID NOT NULL REFERENCES auth .users (id) ON DELETE CASCADE ,
284+ contest_id UUID NOT NULL REFERENCES contests(id) ON DELETE CASCADE ,
285+ participated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
286+ UNIQUE(user_id, contest_id)
287+ );
288+ -- Create RLS policies for user_contest_participation table
289+ ALTER TABLE user_contest_participation ENABLE ROW LEVEL SECURITY;
290+ -- Users can read all contest participation data
291+ CREATE POLICY " Anyone can read contest participation" ON user_contest_participation FOR
292+ SELECT USING (true);
293+ -- Users can only register themselves for contests
294+ CREATE POLICY " Users can register for contests" ON user_contest_participation FOR
295+ INSERT WITH CHECK (auth .uid () = user_id);
296+ -- Users can only update their own participation data
297+ CREATE POLICY " Users can update their own participation" ON user_contest_participation FOR
298+ UPDATE USING (auth .uid () = user_id);
299+ -- Users can only delete their own participation data
300+ CREATE POLICY " Users can delete their own participation" ON user_contest_participation FOR DELETE USING (auth .uid () = user_id);
301+ -- Grant access to authenticated users
302+ GRANT ALL ON contests TO authenticated;
303+ GRANT ALL ON user_contest_participation TO authenticated;
0 commit comments