diff --git a/migrations/20241204225128_create_role_table.down.sql b/migrations/20241204225128_create_role_table.down.sql index c5b3463..d8be615 100644 --- a/migrations/20241204225128_create_role_table.down.sql +++ b/migrations/20241204225128_create_role_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_role"; DROP TABLE IF EXISTS "role"; \ No newline at end of file diff --git a/migrations/20241204225128_create_role_table.up.sql b/migrations/20241204225128_create_role_table.up.sql index a6e7d53..67cd1b2 100644 --- a/migrations/20241204225128_create_role_table.up.sql +++ b/migrations/20241204225128_create_role_table.up.sql @@ -6,4 +6,57 @@ CREATE TABLE IF NOT EXISTS "role"( INSERT INTO "role"(id, name) VALUES (0, 'Ahmet Kaan Gümüş') ON CONFLICT(id) DO UPDATE SET "name" = 'Ahmet Kaan Gümüş'; INSERT INTO "role"(id, name) VALUES (1, 'Founder') ON CONFLICT(id) DO UPDATE SET "name" = 'Founder'; -INSERT INTO "role"(id, name) VALUES (2, 'Normal') ON CONFLICT(id) DO UPDATE SET "name" = 'Normal'; +INSERT INTO "role"(id, name) VALUES (2, 'Admin') ON CONFLICT(id) DO UPDATE SET "name" = 'Admin'; +INSERT INTO "role"(id, name) VALUES (10, 'Normal') ON CONFLICT(id) DO UPDATE SET "name" = 'Normal'; +INSERT INTO "role"(id, name) VALUES (-1, 'Banned') ON CONFLICT(id) DO UPDATE SET "name" = 'Banned'; + +CREATE TABLE IF NOT EXISTS "permission_role"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204225135_create_user_table.down.sql b/migrations/20241204225135_create_user_table.down.sql index 9e27c5d..52874e9 100644 --- a/migrations/20241204225135_create_user_table.down.sql +++ b/migrations/20241204225135_create_user_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_user"; DROP TABLE IF EXISTS "user"; \ No newline at end of file diff --git a/migrations/20241204225135_create_user_table.up.sql b/migrations/20241204225135_create_user_table.up.sql index 5402bd4..04c2c58 100644 --- a/migrations/20241204225135_create_user_table.up.sql +++ b/migrations/20241204225135_create_user_table.up.sql @@ -8,4 +8,55 @@ CREATE TABLE IF NOT EXISTS "user"( email VARCHAR(255) NOT NULL UNIQUE, role_id BIGSERIAL NOT NULL REFERENCES "role"(id), creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW() -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_user"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_user"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_user"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_user"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_user"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_user"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204225143_create_post_table.down.sql b/migrations/20241204225143_create_post_table.down.sql index 1cc5c25..53d2ab1 100644 --- a/migrations/20241204225143_create_post_table.down.sql +++ b/migrations/20241204225143_create_post_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_post"; DROP TABLE IF EXISTS "post"; \ No newline at end of file diff --git a/migrations/20241204225143_create_post_table.up.sql b/migrations/20241204225143_create_post_table.up.sql index c7be5f0..d0f41a7 100644 --- a/migrations/20241204225143_create_post_table.up.sql +++ b/migrations/20241204225143_create_post_table.up.sql @@ -3,4 +3,55 @@ CREATE TABLE IF NOT EXISTS "post"( creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL DEFAULT NOW(), poster_id BIGSERIAL NOT NULL REFERENCES "user"(id), post VARCHAR NOT NULL UNIQUE -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_post"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_post"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_post"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204225151_create_comment_table.down.sql b/migrations/20241204225151_create_comment_table.down.sql index 057acd1..cf86527 100644 --- a/migrations/20241204225151_create_comment_table.down.sql +++ b/migrations/20241204225151_create_comment_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_comment"; DROP TABLE IF EXISTS "comment"; \ No newline at end of file diff --git a/migrations/20241204225151_create_comment_table.up.sql b/migrations/20241204225151_create_comment_table.up.sql index 0339b36..98e7243 100644 --- a/migrations/20241204225151_create_comment_table.up.sql +++ b/migrations/20241204225151_create_comment_table.up.sql @@ -4,4 +4,55 @@ CREATE TABLE IF NOT EXISTS "comment"( creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(), commenter_id BIGSERIAL NOT NULL REFERENCES "user"(id), comment VARCHAR NOT NULL -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_comment"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_comment"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_comment"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204225155_create_interaction_table.down.sql b/migrations/20241204225155_create_interaction_table.down.sql index 5e308d5..c195115 100644 --- a/migrations/20241204225155_create_interaction_table.down.sql +++ b/migrations/20241204225155_create_interaction_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_interaction"; DROP TABLE IF EXISTS "interaction"; \ No newline at end of file diff --git a/migrations/20241204225155_create_interaction_table.up.sql b/migrations/20241204225155_create_interaction_table.up.sql index 51b002b..b4359e9 100644 --- a/migrations/20241204225155_create_interaction_table.up.sql +++ b/migrations/20241204225155_create_interaction_table.up.sql @@ -2,4 +2,55 @@ CREATE TABLE IF NOT EXISTS "interaction"( id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, name VARCHAR(10) UNIQUE NOT NULL -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_interaction"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204230240_create_post_interaction_table.down.sql b/migrations/20241204230240_create_post_interaction_table.down.sql index 2c62083..18b600f 100644 --- a/migrations/20241204230240_create_post_interaction_table.down.sql +++ b/migrations/20241204230240_create_post_interaction_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_post_interaction"; DROP TABLE IF EXISTS "post_interaction"; \ No newline at end of file diff --git a/migrations/20241204230240_create_post_interaction_table.up.sql b/migrations/20241204230240_create_post_interaction_table.up.sql index fc12e83..e274032 100644 --- a/migrations/20241204230240_create_post_interaction_table.up.sql +++ b/migrations/20241204230240_create_post_interaction_table.up.sql @@ -4,4 +4,55 @@ CREATE TABLE IF NOT EXISTS "post_interaction"( interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), interactor_id BIGSERIAL NOT NULL REFERENCES "user"(id), interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW() -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_post_interaction"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_post_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_post_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_post_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/migrations/20241204230248_create_comment_interaction_table.down.sql b/migrations/20241204230248_create_comment_interaction_table.down.sql index c5e2766..10c0ad4 100644 --- a/migrations/20241204230248_create_comment_interaction_table.down.sql +++ b/migrations/20241204230248_create_comment_interaction_table.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here +DROP TABLE IF EXISTS "permission_comment_interaction"; DROP TABLE IF EXISTS "comment_interaction"; \ No newline at end of file diff --git a/migrations/20241204230248_create_comment_interaction_table.up.sql b/migrations/20241204230248_create_comment_interaction_table.up.sql index 1742b20..8ba9960 100644 --- a/migrations/20241204230248_create_comment_interaction_table.up.sql +++ b/migrations/20241204230248_create_comment_interaction_table.up.sql @@ -4,4 +4,55 @@ CREATE TABLE IF NOT EXISTS "comment_interaction"( interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), interactor_id BIGSERIAL NOT NULL REFERENCES "user"(id), interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW() -); \ No newline at end of file +); + +CREATE TABLE IF NOT EXISTS "permission_comment_interaction"( + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE REFERENCES "role"(id), + create_self BOOLEAN NOT NULL, + read_self BOOLEAN NOT NULL, + update_self BOOLEAN NOT NULL, + delete_self BOOLEAN NOT NULL, + create_other BOOLEAN NOT NULL, + read_other BOOLEAN NOT NULL, + update_other BOOLEAN NOT NULL, + delete_other BOOLEAN NOT NULL, + create_lower BOOLEAN NOT NULL, + read_lower BOOLEAN NOT NULL, + update_lower BOOLEAN NOT NULL, + delete_lower BOOLEAN NOT NULL +); + +INSERT INTO "permission_comment_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (0, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (1, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (2, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = TRUE, "read_self" = TRUE, "update_self" = TRUE, "delete_self" = TRUE, +"create_other" = TRUE, "read_other" = TRUE, "update_other" = TRUE, "delete_other" = TRUE, +"create_lower" = TRUE, "read_lower" = TRUE, "update_lower" = TRUE, "delete_lower" = TRUE; + +INSERT INTO "permission_comment_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (10, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = TRUE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; + +INSERT INTO "permission_comment_interaction"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) +VALUES (-1, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) +ON CONFLICT(role_id) DO UPDATE SET +"create_self" = FALSE, "read_self" = FALSE, "update_self" = FALSE, "delete_self" = FALSE, +"create_other" = FALSE, "read_other" = FALSE, "update_other" = FALSE, "delete_other" = FALSE, +"create_lower" = FALSE, "read_lower" = FALSE, "update_lower" = FALSE, "delete_lower" = FALSE; \ No newline at end of file diff --git a/src/database/user.rs b/src/database/user.rs index e4ac816..a57dd20 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -6,7 +6,7 @@ use crate::feature::user::User; pub async fn create( name: &String, surname: &String, - gender: bool, + gender: &bool, birth_date: &NaiveDate, email: &String, database_connection: &Pool, @@ -29,29 +29,26 @@ pub async fn create( .await } -pub async fn read( - email: &String, - database_connection: &Pool, -) -> Result { +pub async fn read(id: &i64, database_connection: &Pool) -> Result { sqlx::query_as!( User, r#" - SELECT * FROM "user" WHERE "email" = $1 + SELECT * FROM "user" WHERE "id" = $1 "#, - email + id ) .fetch_one(database_connection) .await } pub async fn update( - id: i64, + id: &i64, name: &String, surname: &String, gender: &bool, birth_date: &NaiveDate, email: &String, - role_id: i64, + role_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!(User, @@ -61,7 +58,7 @@ pub async fn update( "#, name, surname, gender, birth_date, email, role_id, id).fetch_one(database_connection).await } -pub async fn delete(id: i64, database_connection: &Pool) -> Result { +pub async fn delete(id: &i64, database_connection: &Pool) -> Result { sqlx::query_as!( User, r#" @@ -85,8 +82,68 @@ pub async fn read_all(database_connection: &Pool) -> Result, .await } +pub async fn read_with_email( + email: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "email" = $1 + "#, + email + ) + .fetch_one(database_connection) + .await +} + +pub async fn read_all_for_name( + name: &String, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "name" = $1 + "#, + name + ) + .fetch_all(database_connection) + .await +} + +pub async fn read_all_for_surname( + surname: &String, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "surname" = $1 + "#, + surname + ) + .fetch_all(database_connection) + .await +} + +pub async fn read_all_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "birth_date" = $1 + "#, + birth_date + ) + .fetch_all(database_connection) + .await +} + pub async fn read_all_for_role( - role_id: i64, + role_id: &i64, database_connection: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( @@ -99,3 +156,200 @@ pub async fn read_all_for_role( .fetch_all(database_connection) .await } + +pub async fn read_all_for_gender( + gender: &bool, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "gender" = $1 + "#, + gender + ) + .fetch_all(database_connection) + .await +} + +pub async fn read_id_with_email( + email: &String, + database_connection: &Pool, +) -> Result { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "email" = $1 + "#, + email + ) + .fetch_one(database_connection) + .await? + .id) +} + +pub async fn read_all_id(database_connection: &Pool) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" + "#, + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn read_all_id_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn read_all_id_for_gender( + gender: &bool, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "gender" = $1 + "#, + gender + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn read_all_id_for_name( + name: &String, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "name" = $1 + "#, + name + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn read_all_id_for_surname( + surname: &String, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "surname" = $1 + "#, + surname + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + +pub async fn count_all(database_connection: &Pool) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" + "#, + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} + +pub async fn count_all_for_gender( + gender: &bool, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "gender" = $1 + "#, + gender + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} + +pub async fn count_all_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} + +pub async fn count_all_for_name( + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "name" = $1 + "#, + name + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} + +pub async fn count_all_for_surname( + surname: &String, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "surname" = $1 + "#, + surname + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +}