@@ -25,12 +25,31 @@ CREATE TABLE IF NOT EXISTS public.quiz_history (
2525 created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
2626);
2727
28+ -- Create bookmarked_questions table to store user's saved questions
29+ CREATE TABLE IF NOT EXISTS public .bookmarked_questions (
30+ id UUID DEFAULT gen_random_uuid() PRIMARY KEY ,
31+ user_id UUID REFERENCES auth .users (id) ON DELETE CASCADE NOT NULL ,
32+ question TEXT NOT NULL ,
33+ options JSONB NOT NULL , -- array of answer options
34+ correct_answer TEXT NOT NULL ,
35+ explanation TEXT ,
36+ category TEXT NOT NULL ,
37+ difficulty TEXT NOT NULL ,
38+ notes TEXT , -- user's personal notes about the question
39+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
40+ -- Ensure uniqueness: same user cannot bookmark the same question twice
41+ UNIQUE(user_id, question, correct_answer)
42+ );
43+
2844-- Enable RLS on profiles table
2945ALTER TABLE public .profiles ENABLE ROW LEVEL SECURITY;
3046
3147-- Enable RLS on quiz_history table
3248ALTER TABLE public .quiz_history ENABLE ROW LEVEL SECURITY;
3349
50+ -- Enable RLS on bookmarked_questions table
51+ ALTER TABLE public .bookmarked_questions ENABLE ROW LEVEL SECURITY;
52+
3453-- Create policies for profiles table
3554CREATE POLICY " Users can view own profile" ON public .profiles
3655 FOR SELECT USING (auth .uid () = id);
@@ -48,6 +67,19 @@ CREATE POLICY "Users can view own quiz history" ON public.quiz_history
4867CREATE POLICY " Users can insert own quiz history" ON public .quiz_history
4968 FOR INSERT WITH CHECK (auth .uid () = user_id);
5069
70+ -- Create policies for bookmarked_questions table
71+ CREATE POLICY " Users can view own bookmarks" ON public .bookmarked_questions
72+ FOR SELECT USING (auth .uid () = user_id);
73+
74+ CREATE POLICY " Users can insert own bookmarks" ON public .bookmarked_questions
75+ FOR INSERT WITH CHECK (auth .uid () = user_id);
76+
77+ CREATE POLICY " Users can update own bookmarks" ON public .bookmarked_questions
78+ FOR UPDATE USING (auth .uid () = user_id);
79+
80+ CREATE POLICY " Users can delete own bookmarks" ON public .bookmarked_questions
81+ FOR DELETE USING (auth .uid () = user_id);
82+
5183-- Create function to automatically create profile on user signup
5284CREATE OR REPLACE FUNCTION public .handle_new_user()
5385RETURNS TRIGGER AS $$
@@ -68,3 +100,8 @@ CREATE TRIGGER on_auth_user_created
68100CREATE INDEX IF NOT EXISTS idx_quiz_history_user_id ON public .quiz_history (user_id);
69101CREATE INDEX IF NOT EXISTS idx_quiz_history_created_at ON public .quiz_history (created_at DESC );
70102CREATE INDEX IF NOT EXISTS idx_quiz_history_category ON public .quiz_history (category);
103+
104+ -- Create indexes for bookmarked_questions
105+ CREATE INDEX IF NOT EXISTS idx_bookmarked_questions_user_id ON public .bookmarked_questions (user_id);
106+ CREATE INDEX IF NOT EXISTS idx_bookmarked_questions_created_at ON public .bookmarked_questions (created_at DESC );
107+ CREATE INDEX IF NOT EXISTS idx_bookmarked_questions_category ON public .bookmarked_questions (category);
0 commit comments