diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000000000000000000000000000000000000..f83739fbdd27e45946e6ee1b7d3b43c7d8fd7a4d --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="SqlDialectMappings"> + <file url="file://$PROJECT_DIR$/src/main/resources/db/sql/dbschema.sql" dialect="H2" /> + </component> +</project> \ No newline at end of file diff --git a/src/main/resources/db/sql/dbschema.sql b/src/main/resources/db/sql/dbschema.sql new file mode 100644 index 0000000000000000000000000000000000000000..58c7fdea1b06a24a5dfd4dc789c4b5914e789646 --- /dev/null +++ b/src/main/resources/db/sql/dbschema.sql @@ -0,0 +1,134 @@ +CREATE TABLE users ( + username VARCHAR(50) NOT NULL PRIMARY KEY, + password VARCHAR(500) NOT NULL, + enabled BOOLEAN NOT NULL +); + +CREATE TABLE authorities ( + username VARCHAR(50) NOT NULL, + authority VARCHAR(50) NOT NULL, + CONSTRAINT fk_authorities_users FOREIGN KEY (username) REFERENCES users (username) +); + +CREATE UNIQUE INDEX ix_auth_username ON authorities (username, authority); + +CREATE TABLE profiles ( + id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50) NOT NULL UNIQUE, + first_name VARCHAR(50) NOT NULL, + last_name VARCHAR(50) NOT NULL, + email VARCHAR(50) NOT NULL UNIQUE, + birthdate DATE NOT NULL, + creation_time TIMESTAMP NOT NULL DEFAULT current_timestamp, + group_id BIGINT NOT NULL, + FOREIGN KEY (group_id) REFERENCES groups (group_id), + CONSTRAINT fk_profile_user FOREIGN KEY (username) REFERENCES users (username) +); + +CREATE UNIQUE INDEX ix_profiles_username ON profiles (username, id); + +CREATE TABLE groups( + group_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + group_name VARCHAR(50) NOT NULL, + level INT NOT NULL DEFAULT 0, + points INT NOT NULL DEFAULT 0 +); + +CREATE TABLE messages ( + message_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + senderName VARCHAR(50) NOT NULL, + group_id BIGINT NOT NULL, + message VARCHAR(255) NOT NULL, + message_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + is_deleted BOOLEAN NOT NULL DEFAULT FALSE, + FOREIGN KEY (group_id) REFERENCES groups (group_id), + FOREIGN KEY (senderName) REFERENCES users (username) +); + +CREATE TABLE allergy ( + allergy_name VARCHAR(50) NOT NULL PRIMARY KEY, + allergy_description VARCHAR(255) NOT NULL +); + + +CREATE TABLE product ( + ean BIGINT NOT NULL PRIMARY KEY, + item_name VARCHAR(255) NOT NULL UNIQUE, + description VARCHAR(255) NOT NULL +); + +CREATE TABLE items_allergies ( + ean BIGINT NOT NULL, + allergy_name VARCHAR(50) NOT NULL, + CONSTRAINT PK_item_allergy PRIMARY KEY (ean, allergy_name), + FOREIGN KEY (ean) REFERENCES product (ean), + FOREIGN KEY (allergy_name) REFERENCES allergy (allergy_name) +); + +CREATE TABLE users_allergies ( + username BIGINT NOT NULL, + allergy_name VARCHAR(50) NOT NULL, + CONSTRAINT PK_user_category PRIMARY KEY (username, allergy_name), + FOREIGN KEY (username) REFERENCES users (username), + FOREIGN KEY (allergy_name) REFERENCES allergy (allergy_name) +); + +CREATE TABLE FRIDGE ( + fridge_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + group_id BIGINT NOT NULL, + FOREIGN KEY (group_id) REFERENCES groups (group_id) +); + +CREATE TABLE FRIDGE_PRODUCT ( + fridge_id BIGINT NOT NULL, + ean BIGINT NOT NULL, + CONSTRAINT PK_fridge_group PRIMARY KEY (fridge_id,ean), + FOREIGN KEY (fridge_id) REFERENCES FRIDGE(fridge_id), + FOREIGN KEY (ean) REFERENCES product(ean) +); + +CREATE TABLE shopping_list ( + shopping_list_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + group_id BIGINT NOT NULL, + FOREIGN KEY (group_id) REFERENCES groups (group_id) +); + +CREATE TABLE shopping_list_product( + shopping_list_id BIGINT NOT NULL, + ean BIGINT NOT NULL, + CONSTRAINT PK_shopping_list_group PRIMARY KEY (shopping_list_id,ean), + FOREIGN KEY (shopping_list_id) REFERENCES FRIDGE(fridge_id), + FOREIGN KEY (ean) REFERENCES product(ean) +); + +CREATE TABLE recipe( + recipe_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + recipe_name VARCHAR(50) NOT NULL, + description VARCHAR(500) NOT NULL +); + +CREATE TABLE recipes_products( + recipe_id BIGINT NOT NULL, + ean BIGINT NOT NULL, + CONSTRAINT PK_recipe_product PRIMARY KEY (recipe_id,ean), + FOREIGN KEY (recipe_id) REFERENCES recipe(recipe_id), + FOREIGN KEY (ean) REFERENCES product(ean) +); + +CREATE TABLE favorite( + recipe_id BIGINT NOT NULL, + username varchar(50) NOT NULL, + CONSTRAINT PK_recipe_user PRIMARY KEY (recipe_id,username), + FOREIGN KEY (recipe_id) REFERENCES recipe(recipe_id), + FOREIGN KEY (username) REFERENCES users(username) +); + +CREATE TABLE WASTES( + waste_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + group_id BIGINT NOT NULL, + ean BIGINT NOT NULL, + timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + amount DOUBLE NOT NULL, + FOREIGN KEY (group_id) references groups(group_id), + FOREIGN KEY (ean) references product(ean) +) \ No newline at end of file diff --git a/src/main/resources/db/sql/testdata.sql b/src/main/resources/db/sql/testdata.sql new file mode 100644 index 0000000000000000000000000000000000000000..fecf04bcb3b946c33c5b4b2bfae8715d56d932c8 --- /dev/null +++ b/src/main/resources/db/sql/testdata.sql @@ -0,0 +1,38 @@ +INSERT INTO users (username, password, enabled) +VALUES + ('john', '{noop}password123', true), + ('jane', '{noop}password456', true), + ('bob', '{noop}password789', true), + ('alice', '{noop}password111', true), + ('tom', '{noop}password222', true), + ('sarah', '{noop}password333', true), + ('david', '{noop}password444', true), + ('emily', '{noop}password555', true), + ('mike', '{noop}password666', true), + ('olivia', '{noop}password777', true); + +INSERT INTO authorities (username, authority) +VALUES + ('john', 'USER'), + ('jane', 'ADMIN'), + ('bob', 'USER'), + ('alice', 'USER'), + ('tom', 'USER'), + ('sarah', 'USER'), + ('david', 'ADMIN'), + ('emily', 'USER'), + ('mike', 'USER'), + ('olivia', 'ADMIN'); + +INSERT INTO profiles (username, first_name, last_name,email, birthdate) +VALUES + ('john','John','Smith','johnSmith@gmail.com','1998-05-07'), + ('jane','jane','Lee','HappyJane@gmail.com','1973-02-14'), + ('bob','Bob','Garcia','BigGarcia@gmail.com','1958-12-23'), + ('alice','Alice','Lee','johnSmit2h@gmail.com','2002-01-04'), + ('tom','Tom','Johnson','johnSmith3@gmail.com','1993-11-21'), + ('sarah', 'Sarah', 'Taylor', 'sarahTaylor@gmail.com', '1987-09-18'), + ('david', 'David', 'Miller', 'davidMiller@gmail.com', '1979-07-25'), + ('emily', 'Emily', 'Chen', 'emilyChen@gmail.com', '1990-03-12'), + ('mike', 'Mike', 'Johnson', 'mikeJohnson@gmail.com', '1985-06-30'), + ('olivia', 'Olivia', 'Davis', 'oliviaDavis@gmail.com', '2000-10-15');