diff --git a/backend/selab_script.sql b/backend/selab_script.sql index e1cda129..a944d666 100644 --- a/backend/selab_script.sql +++ b/backend/selab_script.sql @@ -1,16 +1,17 @@ DROP TRIGGER IF EXISTS trg_check_submission_before_deadline ON submission; DROP FUNCTION IF EXISTS check_submission_before_deadline(); +DROP FUNCTION IF EXISTS check_team_size_before_joining(); DROP TABLE IF EXISTS submission CASCADE; DROP TABLE IF EXISTS status CASCADE; -DROP TABLE IF EXISTS project CASCADE; +DROP TABLE IF EXISTS file CASCADE; DROP TABLE IF EXISTS student_group CASCADE; DROP TABLE IF EXISTS team CASCADE; -DROP TABLE IF EXISTS student_subject CASCADE; +DROP TABLE IF EXISTS project CASCADE; DROP TABLE IF EXISTS teacher_subject CASCADE; +DROP TABLE IF EXISTS student_subject CASCADE; DROP TABLE IF EXISTS subject CASCADE; DROP TABLE IF EXISTS website_user CASCADE; -DROP TABLE IF EXISTS file CASCADE; CREATE TABLE website_user ( uid TEXT PRIMARY KEY, @@ -46,7 +47,9 @@ CREATE TABLE project ( name TEXT NOT NULL, subject_id BIGSERIAL NOT NULL, description TEXT, - FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE SET NULL + max_team_size INT NOT NULL DEFAULT 4, -- Added column for maximum team size + FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE SET NULL, + CONSTRAINT deadline_check CHECK (deadline >= CURRENT_DATE) ); CREATE TABLE team ( @@ -94,10 +97,7 @@ CREATE TABLE file ( FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE SET NULL ); --- Constraints and checks -ALTER TABLE project ADD CONSTRAINT deadline_check CHECK (deadline >= CURRENT_DATE); - --- Trigger to prevent submissions after the project deadline +-- Function to check submission deadline CREATE OR REPLACE FUNCTION check_submission_before_deadline() RETURNS TRIGGER AS $$ DECLARE @@ -117,3 +117,26 @@ CREATE TRIGGER trg_check_submission_before_deadline BEFORE INSERT ON submission FOR EACH ROW EXECUTE FUNCTION check_submission_before_deadline(); + +-- Function to check team size before adding a new member +CREATE OR REPLACE FUNCTION check_team_size_before_joining() +RETURNS TRIGGER AS $$ +DECLARE + current_team_size INT; + max_size INT; +BEGIN + SELECT COUNT(*) INTO current_team_size FROM student_group WHERE team_id = NEW.team_id; + SELECT max_team_size INTO max_size FROM project JOIN team ON team.project_id = project.id WHERE team.id = NEW.team_id; + + IF current_team_size >= max_size THEN + RAISE EXCEPTION 'The team is already at its maximum capacity of % members.', max_size; + END IF; + + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER trg_check_team_size_before_joining +BEFORE INSERT ON student_group +FOR EACH ROW +EXECUTE FUNCTION check_team_size_before_joining();