Skip to content

Commit

Permalink
Update selab_script.sql (#33)
Browse files Browse the repository at this point in the history
* Update selab_script.sql

nr engels, snake_case azureobjectid -> uid

* Update backend/selab_script.sql

Co-authored-by: Xander Bil <[email protected]>

* Update backend/selab_script.sql

Co-authored-by: Xander Bil <[email protected]>

---------

Co-authored-by: Xander Bil <[email protected]>
Co-authored-by: Xander Bil <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 5617f9f commit 4e9efb2
Showing 1 changed file with 80 additions and 101 deletions.
181 changes: 80 additions & 101 deletions backend/selab_script.sql
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 4e9efb2

Please sign in to comment.