-- Deploy camper:order_amenity_carousel to pg
-- requires: roles
-- requires: amenity_carousel
-- requires: amenity

begin;

set search_path to camper, public;

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

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

commit;
