-- Deploy camper:order_campsite_carousel to pg
-- requires: roles
-- requires: campsite_carousel
-- requires: campsite

begin;

set search_path to camper, public;

create or replace function order_campsite_carousel(label text, company_id integer, positions integer[]) returns void as
$$
	update campsite_carousel
	set position = cast(temp.position as integer)
	from unnest(positions) with ordinality as temp(media_id, position)
		join campsite on campsite.label = order_campsite_carousel.label
		             and campsite.company_id = order_campsite_carousel.company_id
	where campsite_carousel.campsite_id = campsite.campsite_id
	  and campsite_carousel.media_id = temp.media_id
	;
$$
	language sql
;

revoke execute on function order_campsite_carousel(text, integer, integer[]) from public;
grant execute on function order_campsite_carousel(text, integer, integer[]) to admin;

commit;
