From 4e9efb2ce61cf57582dd45c2400f7ef72097d4f5 Mon Sep 17 00:00:00 2001 From: drieshuybens Date: Mon, 4 Mar 2024 14:33:22 +0100 Subject: [PATCH 1/3] Update selab_script.sql (#33) * Update selab_script.sql nr engels, snake_case azureobjectid -> uid * Update backend/selab_script.sql Co-authored-by: Xander Bil <47951455+xerbalind@users.noreply.github.com> * Update backend/selab_script.sql Co-authored-by: Xander Bil <47951455+xerbalind@users.noreply.github.com> --------- Co-authored-by: Xander Bil <47951455+xerbalind@users.noreply.github.com> Co-authored-by: Xander Bil --- backend/selab_script.sql | 181 +++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 101 deletions(-) diff --git a/backend/selab_script.sql b/backend/selab_script.sql index 0bfcd8f3..67ff9516 100644 --- a/backend/selab_script.sql +++ b/backend/selab_script.sql @@ -1,139 +1,118 @@ -DROP TABLE IF EXISTS Indiening CASCADE; -DROP TABLE IF EXISTS Status CASCADE; -DROP TABLE IF EXISTS Project CASCADE; -DROP TABLE IF EXISTS StudentGroep CASCADE; -DROP TABLE IF EXISTS Groep CASCADE; -DROP TABLE IF EXISTS StudentVak CASCADE; -DROP TABLE IF EXISTS LesgeverVak CASCADE; -DROP TABLE IF EXISTS Vak CASCADE; -DROP TABLE IF EXISTS WebsiteUser CASCADE; -DROP TABLE IF EXISTS Bestand CASCADE; -DROP TABLE IF EXISTS Status CASCADE; - - -CREATE TABLE WebsiteUser( - azureObjectId text PRIMARY KEY, - is_admin boolean +DROP TRIGGER IF EXISTS trg_check_submission_before_deadline ON submission; +DROP FUNCTION IF EXISTS check_submission_before_deadline(); + +DROP TABLE IF EXISTS submission CASCADE; +DROP TABLE IF EXISTS status CASCADE; +DROP TABLE IF EXISTS project 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 teacher_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, + is_admin BOOLEAN NOT NULL DEFAULT FALSE, + given_name TEXT NOT NULL, + mail TEXT NOT NULL ); -CREATE TABLE Vak ( - vak_id TEXT PRIMARY KEY, - Naam TEXT NOT NULL +CREATE TABLE subject ( + id BIGSERIAL PRIMARY KEY, + name TEXT NOT NULL ); -CREATE TABLE StudentVak ( - azureObjectId TEXT NOT NULL, - vak_id TEXT NOT NULL, - PRIMARY KEY (azureObjectId, vak_id), - FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, - FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE CASCADE +CREATE TABLE student_subject ( + uid TEXT NOT NULL, + subject_id BIGSERIAL NOT NULL, + PRIMARY KEY (uid, subject_id), + FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, + FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE CASCADE ); -CREATE TABLE LesgeverVak ( - azureObjectId TEXT NOT NULL, - vak_id TEXT NOT NULL, - PRIMARY KEY (azureObjectId, vak_id), - FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, - FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE CASCADE +CREATE TABLE teacher_subject ( + uid TEXT NOT NULL, + subject_id BIGSERIAL NOT NULL, + PRIMARY KEY (uid, subject_id), + FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, + FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE CASCADE ); - -CREATE TABLE Project ( +CREATE TABLE project ( id BIGSERIAL PRIMARY KEY, - deadline DATE, - naam TEXT NOT NULL, - vak_id TEXT NOT NULL, - FOREIGN KEY (vak_id) REFERENCES Vak(vak_id) ON DELETE SET NULL + deadline DATE NOT NULL, + name TEXT NOT NULL, + subject_id BIGSERIAL NOT NULL, + FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE SET NULL ); -CREATE TABLE Groep ( - groep_id BIGSERIAL PRIMARY KEY, - groep TEXT NOT NULL, - score BIGINT, - project_id BIGINT, - FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE CASCADE +CREATE TABLE team ( + id BIGSERIAL PRIMARY KEY, + team_name TEXT NOT NULL, + score BIGINT NOT NULL, + project_id BIGINT NOT NULL, + FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE, + CONSTRAINT score_check CHECK (score BETWEEN 0 AND 20) ); - -CREATE TABLE StudentGroep ( - azureObjectId TEXT NOT NULL, - groep_id BIGINT NOT NULL, - PRIMARY KEY (azureObjectId, groep_id), - FOREIGN KEY (azureObjectId) REFERENCES WebsiteUser(azureObjectId) ON DELETE CASCADE, - FOREIGN KEY (groep_id) REFERENCES Groep(groep_id) ON DELETE CASCADE +CREATE TABLE student_group ( + uid TEXT NOT NULL, + team_id BIGINT NOT NULL, + PRIMARY KEY (uid, team_id), + FOREIGN KEY (uid) REFERENCES website_user(uid) ON DELETE CASCADE, + FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE ); -CREATE TABLE Status ( - status_id BIGSERIAL PRIMARY KEY, +CREATE TABLE status ( + id BIGSERIAL PRIMARY KEY, status_name TEXT NOT NULL UNIQUE ); -INSERT INTO Status (status_name) VALUES ('inProgress'); -INSERT INTO Status (status_name) VALUES ('Accepted'); -INSERT INTO Status (status_name) VALUES ('Denied'); - +INSERT INTO status (status_name) VALUES ('InProgress'); +INSERT INTO status (status_name) VALUES ('Accepted'); +INSERT INTO status (status_name) VALUES ('Denied'); -CREATE TABLE Indiening ( - indiening_id BIGSERIAL PRIMARY KEY, - datum TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - groep_id BIGINT NOT NULL, +CREATE TABLE submission ( + id BIGSERIAL PRIMARY KEY, + date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + team_id BIGINT NOT NULL, project_id BIGINT NOT NULL, - status_id BIGINT NOT NULL DEFAULT 1, -- Default to 'inProgress' - FOREIGN KEY (groep_id) REFERENCES Groep(groep_id) ON DELETE CASCADE, - FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE CASCADE, - FOREIGN KEY (status_id) REFERENCES Status(status_id) ON DELETE RESTRICT + status_id BIGINT NOT NULL DEFAULT 1, -- Default to 'InProgress' + FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE, + FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE, + FOREIGN KEY (status_id) REFERENCES status(id) ON DELETE RESTRICT ); -CREATE TABLE Bestand ( - bestand_id BIGSERIAL PRIMARY KEY, - indiening_id BIGINT, -- mag null zijn -> kunnen linken aan zowel indiening of project of standalone - project_id BIGINT, -- same - -- TODO nog effectief bestand columns toevoegen ie file type etc.. not sure about exact implementatie rn - FOREIGN KEY (indiening_id) REFERENCES Indiening(indiening_id) ON DELETE SET NULL, - FOREIGN KEY (project_id) REFERENCES Project(id) ON DELETE SET NULL +CREATE TABLE file ( + id BIGSERIAL PRIMARY KEY, + submission_id BIGINT, -- Optional, can be linked to a submission, project, or standalone + project_id BIGINT, -- Optional, same as above + FOREIGN KEY (submission_id) REFERENCES submission(id) ON DELETE SET NULL, + 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); ---checks - -ALTER TABLE Project -ADD CONSTRAINT deadline_check CHECK (deadline >= CURRENT_DATE); - -ALTER TABLE Groep ADD CONSTRAINT score_check CHECK (score BETWEEN 0 AND 20); - -ALTER TABLE WebsiteUser ALTER COLUMN is_admin SET DEFAULT FALSE; - ---zorgt ervoor dat er geen indieningen na deadline kunnen gebeuren -CREATE OR REPLACE FUNCTION check_indiening_voor_deadline() +-- Trigger to prevent submissions after the project deadline +CREATE OR REPLACE FUNCTION check_submission_before_deadline() RETURNS TRIGGER AS $$ DECLARE project_deadline DATE; BEGIN - SELECT deadline INTO project_deadline - FROM Project - WHERE id = NEW.project_id; + SELECT deadline INTO project_deadline FROM project WHERE id = NEW.project_id; IF CURRENT_TIMESTAMP > project_deadline THEN - RAISE EXCEPTION 'Indiening is niet meer toegelaten na deadline'; + RAISE EXCEPTION 'Submissions are not allowed after the deadline.'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; -CREATE TRIGGER trg_check_indiening_voor_deadline -BEFORE INSERT ON Indiening +CREATE TRIGGER trg_check_submission_before_deadline +BEFORE INSERT ON submission FOR EACH ROW -EXECUTE FUNCTION check_indiening_voor_deadline(); - - - - - - - - - - - - +EXECUTE FUNCTION check_submission_before_deadline(); From 6ed1acd06361904803ccf0fdca3caefa4f16d8fa Mon Sep 17 00:00:00 2001 From: drieshuybens Date: Mon, 4 Mar 2024 16:43:06 +0100 Subject: [PATCH 2/3] Update selab_script.sql Gwn description aan project toevoegen, kan mss wel nice zijn haha --- backend/selab_script.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/selab_script.sql b/backend/selab_script.sql index 67ff9516..e1cda129 100644 --- a/backend/selab_script.sql +++ b/backend/selab_script.sql @@ -45,6 +45,7 @@ CREATE TABLE project ( deadline DATE NOT NULL, name TEXT NOT NULL, subject_id BIGSERIAL NOT NULL, + description TEXT, FOREIGN KEY (subject_id) REFERENCES subject(id) ON DELETE SET NULL ); From 5979c66de6611b8dbe496a716c541601ba88254b Mon Sep 17 00:00:00 2001 From: drieshuybens Date: Tue, 5 Mar 2024 21:49:40 +0100 Subject: [PATCH 3/3] Update selab_script.sql team size + check vooraleer toevoegen --- backend/selab_script.sql | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) 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();