module Devops
  module API3
    module Routes
      module FilterRoutes

        def self.registered(app)

          app.define_policy :read_filters, "Read filter list"
          app.define_policy :edit_filters, "Add and delete filter list"

          app.get_with_headers "/filter/:provider/images" do |provider|
            check_policy(:read_filters)
            check_provider(provider)
            json Devops::API3::Handler::Filter.new(request).available_images(provider)
          end

          app.post_with_headers "/filter/:provider/images/add" do |provider|
            check_policy(:edit_filters)
            check_provider(provider)
            create_response("Updated", {:images => Devops::API3::Handler::Filter.new(request).add_images(provider)})
          end

          app.post_with_headers "/filter/:provider/images/delete" do |provider|
            check_policy(:edit_filters)
            check_provider(provider)
            create_response("Deleted", {:images => Devops::API3::Handler::Filter.new(request).delete_images(provider)})
          end

          puts "Filter routes initialized"
        end

      end
    end
  end
end
