Skip to content

Commit

Permalink
Update selab_script.sql
Browse files Browse the repository at this point in the history
team size + check vooraleer toevoegen
  • Loading branch information
DRIESASTER authored Mar 5, 2024
1 parent da450ef commit 5979c66
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions backend/selab_script.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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();

0 comments on commit 5979c66

Please sign in to comment.