class Api::NotesController < Api::ApiController
  before_action :set_release
  deserializable_resource :note, only: [:create]

  def index
    render jsonapi: @release.notes, class: { Note: SerializableNote }
  end

  def create
    note = @release.notes.new(note_create_params)
    note.user_id = current_user.id
    note.email = current_user.email
    note.save!
    render jsonapi: note, class: { Note: SerializableNote }, status: :created
  end

  private

  def model_name
    request.path.match(/(\w+_releases)/).captures.first.singularize
  end

  def model_constant
    model_name.camelize.constantize
  end

  def set_release
    @release = authorize model_constant.find(params["#{model_name}_id"])
  end

  def note_create_params
    parameters = params.require(:note).permit!
    parameters.slice(:content)
  end
end
