module Endpoints
  class <%= plural_class_name %> < Base
    namespace "<%= url_path %>" do
      get do
        encode serialize(<%= singular_class_name %>.all)
      end

      post do
        # warning: not safe
        <%= field_name %> = <%= singular_class_name %>.new(body_params)
        <%= field_name %>.save
        status 201
        encode serialize(<%= field_name %>)
      end

      get "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        encode serialize(<%= field_name %>)
      end

      patch "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        # warning: not safe
        #<%= field_name %>.update(body_params)
        encode serialize(<%= field_name %>)
      end

      delete "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        <%= field_name %>.destroy
        encode serialize(<%= field_name %>)
      end

      private

      def serialize(data, structure = :default)
        Serializers::<%= singular_class_name %>.new(structure).serialize(data)
      end
    end
  end
end

module Endpoints
  class <%= plural_class_name %> < Base
    namespace "<%= url_path %>" do
      serializer(Serializers::<%= singular_class_name %>)

      get do
        encode serialize(<%= singular_class_name %>.all)
      end

      post do
        # warning: not safe
        <%= field_name %> = <%= singular_class_name %>.new(body_params)
        <%= field_name %>.save
        status 201
        encode serialize(<%= field_name %>)
      end

      get "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        encode serialize(<%= field_name %>)
      end

      patch "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        # warning: not safe
        #<%= field_name %>.update(body_params)
        encode serialize(<%= field_name %>)
      end

      delete "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(id: id) || halt(404)
        <%= field_name %>.destroy
        encode serialize(<%= field_name %>)
      end
    end
  end
end

Use serializer helper in template
Use serializer helper in template
