-- Deploy camper:order_campsite_type_carousel to pg
-- requires: schema_camper
-- requires: roles
-- requires: campsite_type
-- requires: campsite_type_carousel

begin;

set search_path to camper, public;

create or replace function order_campsite_type_carousel(slug uuid, positions integer[]) returns void as
$$
	update campsite_type_carousel
	set position = cast(temp.position as integer)
	from unnest(positions) with ordinality as temp(media_id, position)
		join campsite_type on campsite_type.slug = order_campsite_type_carousel.slug
	where campsite_type_carousel.campsite_type_id = campsite_type.campsite_type_id
	  and campsite_type_carousel.media_id = temp.media_id
	;
$$
	language sql
;

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

commit;
