diff --git a/server/Entity relations.drawio b/server/Entity relations.drawio index 13b7e96..605d3dd 100644 --- a/server/Entity relations.drawio +++ b/server/Entity relations.drawio @@ -1,17 +1,17 @@ - + - + - - + + @@ -34,7 +34,7 @@ - + @@ -78,11 +78,24 @@ - - + + + + + + + + + + + + + + + - + @@ -90,12 +103,12 @@ - - + + - + @@ -103,12 +116,12 @@ - - + + - + @@ -116,12 +129,12 @@ - - + + - + @@ -129,12 +142,12 @@ - - + + - + @@ -142,8 +155,21 @@ - - + + + + + + + + + + + + + + + @@ -152,6 +178,306 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/src/migrations/index.ts b/server/src/migrations/index.ts index 9abf46d..b753201 100644 --- a/server/src/migrations/index.ts +++ b/server/src/migrations/index.ts @@ -4,7 +4,8 @@ dotenv.config(); import { client } from "../db"; client.query(` -DROP TABLE IF EXISTS public.bookings; +DROP TABLE IF EXISTS public.trainers; +DROP TABLE IF EXISTS public.centers; DROP TABLE IF EXISTS public.users; DROP TYPE IF EXISTS public."UserType"; @@ -17,22 +18,30 @@ CREATE TYPE public."UserType" AS ENUM CREATE TABLE public.users ( - id serial NOT NULL, + id serial NOT NULL PRIMARY KEY, first_name text NOT NULL, last_name text NOT NULL, email text NOT NULL UNIQUE, password_hash text NOT NULL, user_type "UserType" NOT NULL DEFAULT 'User', - PRIMARY KEY (id) + email_verified boolean NOT NULL DEFAULT false ); -CREATE TABLE public.bookings +CREATE TABLE public.centers ( - id serial NOT NULL, - trainer_id int references users(id) NOT NULL, - user_id int references users(id) NOT NULL, - time timestamp with time zone NOT NULL, - duration interval NOT NULL + id serial NOT NULL PRIMARY KEY, + name text NOT NULL, + city text NOT NULL, + zip_code int NOT NULL, + address text NOT NULL +); + +CREATE TABLE public.trainers +( + id serial NOT NULL PRIMARY KEY, + user_id int REFERENCES users(id) UNIQUE NOT NULL, + center_id int REFERENCES centers(id) NOT NULL, + hourly_price int NOT NULL ); `) .then(()=>{ diff --git a/server/src/migrations/populate.ts b/server/src/migrations/populate.ts index dcee47b..044a83a 100644 --- a/server/src/migrations/populate.ts +++ b/server/src/migrations/populate.ts @@ -3,13 +3,40 @@ dotenv.config(); import { client } from "../db"; -client.query(` -INSERT INTO users (first_name, last_name, email, password_hash, user_type) -VALUES ('Filip', 'B P', 'fbp@example.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin'), +async function main() { + + + const users = await client.query(` +INSERT INTO users (first_name, last_name, email, password_hash, user_type) VALUES +('Filip', 'B P', 'fbp@example.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin'), ('User1', 'Lastname', 'u1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'User'), +('User2', 'Lastname', 'u2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'User'), ('Trainer1', 'Lastname', 't1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Trainer'), -('Admin1', 'Lastname', 'a1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin'); -`) -.then(()=>{ - return client.end(); -}) \ No newline at end of file +('Trainer2', 'Lastname', 't2@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Trainer'), +('Admin1', 'Lastname', 'a1@test.com', '$2b$10$zxqwqZXo3DrLVTFx.hkQQ.uKeqhHMnok.G/4.Ivq1g647RaqYtgKC', 'Admin') +RETURNING id, email; + `); + + const centers = await client.query(` +INSERT INTO centers (name, city, zip_code, address) VALUES +('Herning Fitness', 'Herning', '7400', 'Vej 123'), +('Aarhus Fitness', 'Aarhus', '8000', 'Alle 321') +RETURNING id, name; + `); + + const trainers = await client.query(` +INSERT INTO trainers (user_id, center_id, hourly_price) VALUES +($1, $2, 10000), +($3, $4, 20000) +RETURNING id, user_id; + `, [ + users.rows.find(user => user.email === "t1@test.com").id, + centers.rows[0].id, + users.rows.find(user => user.email === "t2@test.com").id, + centers.rows[1].id, + ]); + + client.end(); +} + +main(); \ No newline at end of file