| 1 |
|
|---|
| 2 | --user management and roles ---------------------------------------------------------------------------------------------------------
|
|---|
| 3 | create table Permission (
|
|---|
| 4 | permission_id serial primary key,
|
|---|
| 5 | permission_name text not null unique ,
|
|---|
| 6 | description text
|
|---|
| 7 | );
|
|---|
| 8 |
|
|---|
| 9 | create table Role (
|
|---|
| 10 | role_id serial primary key,
|
|---|
| 11 | role_name text not null unique
|
|---|
| 12 | );
|
|---|
| 13 |
|
|---|
| 14 | create table Role_Permission (
|
|---|
| 15 | role_permission_id serial primary key,
|
|---|
| 16 | role_id int,
|
|---|
| 17 | permission_id int,
|
|---|
| 18 | constraint fk_role foreign key (role_id) references Role(role_id),
|
|---|
| 19 | constraint fk_permission foreign key (permission_id) references Permission(permission_id)
|
|---|
| 20 | );
|
|---|
| 21 |
|
|---|
| 22 | create table User_Role (
|
|---|
| 23 | user_role_id serial primary key,
|
|---|
| 24 | role_id int,
|
|---|
| 25 | user_id int,
|
|---|
| 26 | constraint fk_role foreign key (role_id) references Role(role_id),
|
|---|
| 27 | constraint fk_user foreign key (user_id) references "User"(user_id),
|
|---|
| 28 | unique (user_id, role_id)
|
|---|
| 29 | );
|
|---|
| 30 |
|
|---|
| 31 | -- user ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 32 |
|
|---|
| 33 | create table "User" (
|
|---|
| 34 | user_id serial primary key,
|
|---|
| 35 | first_name text not null,
|
|---|
| 36 | last_name text not null,
|
|---|
| 37 | username text not null unique,
|
|---|
| 38 | email text not null,
|
|---|
| 39 | password_hash text not null unique,
|
|---|
| 40 | constraint chk_email check (email like '%@%')
|
|---|
| 41 | );
|
|---|
| 42 |
|
|---|
| 43 | create table Notification (
|
|---|
| 44 | notification_id serial primary key,
|
|---|
| 45 | message text not null,
|
|---|
| 46 | is_read bool not null,
|
|---|
| 47 | created_at timestamp not null ,
|
|---|
| 48 | user_id int,
|
|---|
| 49 | constraint fk_user foreign key (user_id) references "User"(user_id)
|
|---|
| 50 | );
|
|---|
| 51 |
|
|---|
| 52 | create table Location (
|
|---|
| 53 | location_id serial primary key,
|
|---|
| 54 | city text not null,
|
|---|
| 55 | region text not null,
|
|---|
| 56 | address text not null
|
|---|
| 57 | );
|
|---|
| 58 |
|
|---|
| 59 | -- worker ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 60 | -- dali tuka fk_calendar ili dovolno e vo calendar
|
|---|
| 61 | create table Worker (
|
|---|
| 62 | worker_id serial primary key,
|
|---|
| 63 | bio text,
|
|---|
| 64 | works_remote bool not null,
|
|---|
| 65 | location_id int,
|
|---|
| 66 | user_id int,
|
|---|
| 67 | calendar_id int,
|
|---|
| 68 | constraint fk_location foreign key (location_id) references Location(location_id),
|
|---|
| 69 | constraint fk_user foreign key (user_id) references "User"(user_id),
|
|---|
| 70 | constraint fk_calendar foreign key (calendar_id) references Calendar(calendar_id)
|
|---|
| 71 | );
|
|---|
| 72 |
|
|---|
| 73 | create table Calendar (
|
|---|
| 74 | calendar_id serial primary key,
|
|---|
| 75 | start_time timestamp not null,
|
|---|
| 76 | end_time timestamp not null,
|
|---|
| 77 | worker_id int,
|
|---|
| 78 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 79 | constraint chk_valid_dates CHECK (start_time < end_time)
|
|---|
| 80 | );
|
|---|
| 81 |
|
|---|
| 82 | create table Calendar_Event (
|
|---|
| 83 | event_id serial primary key,
|
|---|
| 84 | title text not null,
|
|---|
| 85 | description text,
|
|---|
| 86 | start_time timestamp not null,
|
|---|
| 87 | end_time timestamp not null,
|
|---|
| 88 | calendar_id int,
|
|---|
| 89 | constraint fk_calendar foreign key (calendar_id) references Calendar(calendar_id),
|
|---|
| 90 | constraint chk_valid_dates CHECK (start_time < end_time)
|
|---|
| 91 | );
|
|---|
| 92 |
|
|---|
| 93 | create table Certificate (
|
|---|
| 94 | certificate_id serial primary key,
|
|---|
| 95 | certificate_name text not null,
|
|---|
| 96 | issuer text not null,
|
|---|
| 97 | worker_id int,
|
|---|
| 98 | constraint fk_worker foreign key (worker_id) references Worker(worker_id)
|
|---|
| 99 | );
|
|---|
| 100 |
|
|---|
| 101 | create table Specialty (
|
|---|
| 102 | specialty_id serial primary key,
|
|---|
| 103 | specialty_name text not null unique
|
|---|
| 104 | );
|
|---|
| 105 |
|
|---|
| 106 | create table Worker_Specialty (
|
|---|
| 107 | worker_id int,
|
|---|
| 108 | specialty_id int,
|
|---|
| 109 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 110 | constraint fk_specialty foreign key (specialty_id) references Specialty(specialty_id)
|
|---|
| 111 | );
|
|---|
| 112 |
|
|---|
| 113 | -- post ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 114 | create table Post_Status (
|
|---|
| 115 | status_id serial primary key,
|
|---|
| 116 | status_name text not null unique
|
|---|
| 117 | );
|
|---|
| 118 |
|
|---|
| 119 | create table Post (
|
|---|
| 120 | post_id serial primary key,
|
|---|
| 121 | title text not null,
|
|---|
| 122 | description text,
|
|---|
| 123 | created_at timestamp,
|
|---|
| 124 | user_id int,
|
|---|
| 125 | location_id int,
|
|---|
| 126 | status_id int,
|
|---|
| 127 | constraint fk_user foreign key (user_id) references "User"(user_id),
|
|---|
| 128 | constraint fk_location foreign key (location_id) references Location(location_id),
|
|---|
| 129 | constraint fk_status foreign key (status_id) references Post_Status(status_id),
|
|---|
| 130 | constraint chk_created_at check (created_at <= now())
|
|---|
| 131 | );
|
|---|
| 132 |
|
|---|
| 133 | create table Post_Image (
|
|---|
| 134 | image_id serial primary key,
|
|---|
| 135 | image_url text not null,
|
|---|
| 136 | post_id int,
|
|---|
| 137 | constraint fk_post foreign key (post_id) references Post(post_id)
|
|---|
| 138 | );
|
|---|
| 139 |
|
|---|
| 140 | create table Post_Specialty (
|
|---|
| 141 | post_id int,
|
|---|
| 142 | specialty_id int,
|
|---|
| 143 | constraint fk_post foreign key (post_id) references Post(post_id),
|
|---|
| 144 | constraint fk_specialty foreign key (specialty_id) references Specialty(specialty_id)
|
|---|
| 145 | );
|
|---|
| 146 |
|
|---|
| 147 | -- application ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 148 | create table Application_Status (
|
|---|
| 149 | status_id serial primary key,
|
|---|
| 150 | status_name text not null unique
|
|---|
| 151 | );
|
|---|
| 152 |
|
|---|
| 153 | create table Application (
|
|---|
| 154 | application_id serial primary key,
|
|---|
| 155 | message text,
|
|---|
| 156 | needed_time time,
|
|---|
| 157 | expected_price float,
|
|---|
| 158 | created_at timestamp not null ,
|
|---|
| 159 | worker_id int,
|
|---|
| 160 | post_id int,
|
|---|
| 161 | status_id int,
|
|---|
| 162 | constraint fk_post foreign key (post_id) references Post(post_id),
|
|---|
| 163 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 164 | constraint fk_status foreign key (status_id) references Application_Status(status_id),
|
|---|
| 165 | constraint chk_needed_time check (needed_time > '00:00:00')
|
|---|
| 166 | );
|
|---|
| 167 |
|
|---|
| 168 | -- reviews and ratings ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 169 |
|
|---|
| 170 | create table Rating_Per_Specialty (
|
|---|
| 171 | rating_per_specialty_id serial primary key,
|
|---|
| 172 | rating_avg float,
|
|---|
| 173 | ranking_score int,
|
|---|
| 174 | worker_id int,
|
|---|
| 175 | specialty_id int,
|
|---|
| 176 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 177 | constraint fk_specialty foreign key (specialty_id) references Specialty(specialty_id)
|
|---|
| 178 | );
|
|---|
| 179 |
|
|---|
| 180 | create table Review (
|
|---|
| 181 | review_id serial primary key,
|
|---|
| 182 | grade int not null,
|
|---|
| 183 | created_at timestamp not null,
|
|---|
| 184 | reviewer_id int,
|
|---|
| 185 | rating_per_specialty_id int,
|
|---|
| 186 | constraint fk_reviewer foreign key (reviewer_id) references "User"(user_id),
|
|---|
| 187 | constraint fk_rating_specialty foreign key (rating_per_specialty_id) references Rating_Per_Specialty(rating_per_specialty_id),
|
|---|
| 188 | constraint chk_grade check (grade>=0 and grade <=5)
|
|---|
| 189 | );
|
|---|
| 190 |
|
|---|
| 191 | create table Review_Comment (
|
|---|
| 192 | comment_id serial primary key,
|
|---|
| 193 | review_id int,
|
|---|
| 194 | constraint fk_review foreign key (review_id) references Review(review_id)
|
|---|
| 195 | );
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 | -- payments and premium workers ------------------------------------------------------------------------------------------------------------------------
|
|---|
| 199 | create table Payment_Method (
|
|---|
| 200 | method_id serial primary key,
|
|---|
| 201 | method_name text not null unique,
|
|---|
| 202 | max_amount float not null,
|
|---|
| 203 | tax float not null,
|
|---|
| 204 | constraint chk_tax check (tax >= 0 and tax <= 1)
|
|---|
| 205 | );
|
|---|
| 206 | -- foreign keys da se PK ili da ima vesticki?????
|
|---|
| 207 | create table Worker_Method (
|
|---|
| 208 | worker_method_id serial primary key,
|
|---|
| 209 | worker_id int,
|
|---|
| 210 | method_id int,
|
|---|
| 211 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 212 | constraint fk_method foreign key (method_id) references Payment_Method(method_id),
|
|---|
| 213 | unique (worker_id, method_id)
|
|---|
| 214 | );
|
|---|
| 215 |
|
|---|
| 216 | create table Payment_Status (
|
|---|
| 217 | status_id serial primary key,
|
|---|
| 218 | status_name text not null unique
|
|---|
| 219 | );
|
|---|
| 220 |
|
|---|
| 221 | create table Payment (
|
|---|
| 222 | payment_id serial primary key,
|
|---|
| 223 | amount float not null,
|
|---|
| 224 | payment_date timestamp not null,
|
|---|
| 225 | worker_method_id int,
|
|---|
| 226 | payment_status int,
|
|---|
| 227 | constraint fk_worker_method foreign key (worker_method_id) references Worker_Method(worker_method_id),
|
|---|
| 228 | constraint fk_status foreign key (worker_method_id) references Payment_Status(status_id)
|
|---|
| 229 | );
|
|---|
| 230 |
|
|---|
| 231 | create table Premium_Plan (
|
|---|
| 232 | plan_id serial primary key,
|
|---|
| 233 | name text not null unique,
|
|---|
| 234 | price float not null,
|
|---|
| 235 | duration_days int not null,
|
|---|
| 236 | description text not null,
|
|---|
| 237 | constraint chk_price check (price>=0),
|
|---|
| 238 | constraint chk_duration_days check (duration_days>0)
|
|---|
| 239 | );
|
|---|
| 240 |
|
|---|
| 241 | create table Premium_Status (
|
|---|
| 242 | status_id serial primary key,
|
|---|
| 243 | status_name text not null unique
|
|---|
| 244 | );
|
|---|
| 245 |
|
|---|
| 246 | create table Premium_Worker (
|
|---|
| 247 | premium_id serial primary key,
|
|---|
| 248 | start_date timestamp not null,
|
|---|
| 249 | end_date timestamp not null,
|
|---|
| 250 | status_id int,
|
|---|
| 251 | plan_id int,
|
|---|
| 252 | worker_id int,
|
|---|
| 253 | payment_id int,
|
|---|
| 254 | constraint fk_status foreign key (status_id) references Premium_Status(status_id),
|
|---|
| 255 | constraint fk_worker foreign key (worker_id) references Worker(worker_id),
|
|---|
| 256 | constraint fk_plan foreign key (plan_id) references Premium_Plan(plan_id),
|
|---|
| 257 | constraint fk_payment foreign key (payment_id) references Payment(payment_id),
|
|---|
| 258 | constraint chk_valid_dates CHECK (start_date < end_date)
|
|---|
| 259 | );
|
|---|