aboutsummaryrefslogtreecommitdiff
path: root/db/functions.sql
blob: 995e7e99ed2af2db218b861d85bb8c14c82d562d (plain)
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
drop function if exists webs.cart;
drop function if exists webs.add_to_cart;

delimiter $$
create function webs.cart(user_id int) -- get current order for user_id (cart order id)
returns int
begin
	set @order_id = (select id from webs.order where status = 1 and user = user_id);
	if @order_id is not null then
		return @order_id;
	end if;
	insert into webs.order (`user`) values (user_id);
	set @order_id = (select id from webs.order where status = 1 and user = user_id);
	return @order_id;
end$$

create function webs.add_to_cart(product_id int, user_id int)
returns boolean
begin
	set @orderproduct_id = (select id from webs.orderproduct where product = product_id and `order` = cart(user_id));
	if @orderproduct_id is not null then
		update orderproduct set count = count + 1 where id = @orderproduct_id;
		return true;
	end if;
	insert into webs.orderproduct (`product`, `count`, `order`) values (product_id, 1, cart(user_id));
	return false;
end$$

delimiter ;