-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
239 lines (224 loc) · 7.66 KB
/
init.sql
File metadata and controls
239 lines (224 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
create table if not exists users
(
id serial primary key,
name text not null,
email text unique not null,
photo text not null default '',
default_address_id int
);
create table if not exists addresses
(
id serial primary key,
user_id int not null references users on delete cascade,
info jsonb not null
);
alter table users
add foreign key (default_address_id) references addresses on delete set null;
create table if not exists categories
(
id serial primary key,
name text not null,
photo text not null
);
create table if not exists tags
(
id serial primary key,
category_id int REFERENCES categories ON DELETE CASCADE,
name text not null
);
create table if not exists requests
(
id serial primary key,
user_id int not null references users on delete cascade,
category_id int not null references categories on delete cascade,
name text not null,
item_name text not null,
description text not null,
photos text[] not null,
price int not null,
quantity int not null,
date date not null default current_date,
finished bool not null default false
);
create table if not exists requests_tags
(
request_id int not null references requests on delete cascade,
tag_id int not null references tags on delete cascade
);
create or replace view requests_view as
SELECT r.id,
r.user_id,
u.name as username,
r.category_id,
r.name,
r.item_name,
r.description,
r.photos,
r.price,
r.quantity,
r.date,
r.finished,
jsonb_agg(jsonb_build_object(
'id', t.id,
'name', t.name
)) as tags
FROM requests r
LEFT JOIN users u on r.user_id = u.id
LEFT JOIN requests_tags rt on r.id = rt.request_id
JOIN tags t on rt.tag_id = t.id
GROUP BY r.id, u.id;
create or replace function search_requests(page int, size int, category int, required_tags int[], price_from int,
price_to int, sort_column text, sort_direction text, search text)
returns setof requests_view as
$$
begin
return query execute format('
SELECT r.id,
r.user_id,
u.name as username,
r.category_id,
r.name,
r.item_name,
r.description,
r.photos,
r.price,
r.quantity,
r.date,
r.finished,
jsonb_agg(jsonb_build_object(
''id'', t.id,
''name'', t.name
)) as tags
FROM requests r
LEFT JOIN users u on r.user_id = u.id
LEFT JOIN requests_tags rt on r.id = rt.request_id
JOIN tags t on rt.tag_id = t.id
WHERE r.finished = false
AND ($1 IS NULL OR r.category_id = $1)
AND ($3 IS NULL OR r.price >= $3)
AND ($4 IS NULL OR r.price <= $4)
AND ($5 = '''' or lower(r.name) LIKE ''%%'' || $5 || ''%%''
or lower(r.item_name) LIKE ''%%'' || $5 || ''%%''
or lower(r.description) LIKE ''%%'' || $5 || ''%%'')
GROUP BY r.id, u.id, date
HAVING array_agg(t.id) @> $2
ORDER BY %I %s LIMIT %s OFFSET %s', sort_column, sort_direction, size, size * page)
USING category, required_tags, price_from, price_to, search;
end;
$$ LANGUAGE plpgsql;
create table if not exists proposals
(
id serial primary key,
user_id int not null references users on delete cascade,
request_id int not null references requests on delete cascade,
description text not null,
photos text[] not null,
price int not null,
quantity int not null,
date date not null default current_date,
bought_by_id int references users on delete set null default null
);
create or replace view proposals_view as
SELECT p.id,
p.user_id,
COALESCE(u.name, '') as username,
p.request_id,
r.name,
r.item_name,
p.description,
p.photos,
p.price,
p.quantity,
p.date,
p.bought_by_id
FROM proposals p
JOIN requests r on p.request_id = r.id
LEFT JOIN users u on u.id = p.user_id;
INSERT INTO categories (id, name, photo)
VALUES (1, 'Недвижимость', '/images/property.png');
INSERT INTO categories (id, name, photo)
VALUES (2, 'Электроника', '/images/electronics.png');
INSERT INTO categories (id, name, photo)
VALUES (3, 'Хобби и отдых', '/images/hobby.png');
INSERT INTO categories (id, name, photo)
VALUES (4, 'Транспорт', '/images/transport.png');
INSERT INTO categories (id, name, photo)
VALUES (5, 'Одежда', '/images/clothes.png');
INSERT INTO categories (id, name, photo)
VALUES (6, 'Животные', '/images/pets.png');
INSERT INTO categories (id, name, photo)
VALUES (7, 'Для дома', '/images/house.png');
INSERT INTO categories (id, name, photo)
VALUES (8, 'Прочее', '/images/other.png');
INSERT INTO tags (category_id, name)
VALUES (1, 'Студия');
INSERT INTO tags (category_id, name)
VALUES (1, 'Однокомнатная');
INSERT INTO tags (category_id, name)
VALUES (1, 'Двухкомнатная');
INSERT INTO tags (category_id, name)
VALUES (1, 'Трёхкомнатная');
INSERT INTO tags (category_id, name)
VALUES (2, 'Ноутбук');
INSERT INTO tags (category_id, name)
VALUES (2, 'Телефон');
INSERT INTO tags (category_id, name)
VALUES (2, 'Наушники');
INSERT INTO tags (category_id, name)
VALUES (2, 'Видеокарта');
INSERT INTO tags (category_id, name)
VALUES (3, 'Лыжи');
INSERT INTO tags (category_id, name)
VALUES (3, 'Ролики');
INSERT INTO tags (category_id, name)
VALUES (3, 'Велосипед');
INSERT INTO tags (category_id, name)
VALUES (3, 'Коньки');
INSERT INTO tags (category_id, name)
VALUES (4, 'Автомобиль');
INSERT INTO tags (category_id, name)
VALUES (4, 'Автобус');
INSERT INTO tags (category_id, name)
VALUES (4, 'Электросамокат');
INSERT INTO tags (category_id, name)
VALUES (4, 'Самолёт');
INSERT INTO tags (category_id, name)
VALUES (5, 'Обувь');
INSERT INTO tags (category_id, name)
VALUES (5, 'Верхняя одежда');
INSERT INTO tags (category_id, name)
VALUES (5, 'Рубашки');
INSERT INTO tags (category_id, name)
VALUES (5, 'Штаны');
INSERT INTO tags (category_id, name)
VALUES (6, 'Игрушка');
INSERT INTO tags (category_id, name)
VALUES (6, 'Одежда');
INSERT INTO tags (category_id, name)
VALUES (6, 'Корм');
INSERT INTO tags (category_id, name)
VALUES (6, 'Ошейник');
INSERT INTO tags (category_id, name)
VALUES (7, 'Стул');
INSERT INTO tags (category_id, name)
VALUES (7, 'Стол');
INSERT INTO tags (category_id, name)
VALUES (7, 'Кровать');
INSERT INTO tags (category_id, name)
VALUES (7, 'Шкаф');
INSERT INTO tags (category_id, name)
VALUES (null, 'Синий');
INSERT INTO tags (category_id, name)
VALUES (null, 'Красный');
INSERT INTO tags (category_id, name)
VALUES (null, 'Зелёный');
INSERT INTO tags (category_id, name)
VALUES (null, 'Белый');
INSERT INTO tags (category_id, name)
VALUES (null, 'Маленький');
INSERT INTO tags (category_id, name)
VALUES (null, 'Средний');
INSERT INTO tags (category_id, name)
VALUES (null, 'Большой');
INSERT INTO tags (category_id, name)
VALUES (null, 'Огромный');