class <%= config[:class_name] %> < Eucalypt::Controller(route: '<%= config[:route] %>')
  helpers <%= config[:helper_class_name] %> if defined? <%= config[:helper_class_name] %>

  # You can use the `current_user` helper provided by Warden in your views.

  # You can also use authorization helpers provided by Pundit in your views.
  # These are useful for conditional displays to users with the correct permissions.
  # - e.g. Showing a form for editing <%= config[:resources] %>

  # Pundit authorization helpers are used in the following way:
  # - `policy(<%= config[:constant] %>).add?`
  # - `policy(<%= config[:constant] %>).edit?`
  # - `policy(<%= config[:constant] %>).delete?`

  # GET - Browse
  get '/' do
    @<%= config[:resources] %> = <%= config[:constant] %>.all
    # Render a view here for displaying all <%= config[:resources] %>
  end

  # GET - Read
  get '/:id' do |id|
    @<%= config[:resource] %> = <%= config[:constant] %>.find id
    # Render a view here for displaying a single <%= config[:resource] %>
  rescue ActiveRecord::RecordNotFound
    status 404 # Resource not found
    redirect to '/'
  end

  # POST - Edit
  post '/:id/edit' do |id|
    authenticate
    <%= config[:resource] %> = <%= config[:constant] %>.find id
    authorize <%= config[:resource]%>, :edit?
    <%= config[:resource] %>.update! params['<%= config[:resource] %>']
    redirect to "/#{id}"
  rescue ActiveRecord::RecordNotFound
    status 404 # Resource not found
    redirect to "/#{id}"
  rescue Pundit::NotAuthorizedError
    status 401 # Unauthorized
    redirect to '/'
  end

  # POST - Add
  post '/' do
    authenticate
    <%= config[:resource] %> = <%= config[:constant] %>.new params['<%= config[:resource] %>']
    authorize <%= config[:resource] %>, :add?
    <%= config[:resource] %>.save!
    redirect to "/#{<%= config[:resource] %>.id}"
  rescue Pundit::NotAuthorizedError
    status 401 # Unauthorized
    redirect to '/'
  end

  # POST - Delete
  post '/:id/delete' do |id|
    authenticate
    <%= config[:resource] %> = <%= config[:constant] %>.find id
    authorize <%= config[:resource]%>, :delete?
    <%= config[:resource] %>.destroy!
    redirect to '/'
  rescue ActiveRecord::RecordNotFound
    status 404 # Resource not found
    redirect to "/#{id}"
  rescue Pundit::NotAuthorizedError
    status 401 # Unauthorized
    redirect to '/'
  end
end