class EventsController < ApplicationController
  before_action :set_event, only: [:show, :update, :destroy]

  # GET /events
  def index
    respond_to do |format|
      @events = Event
        .where('events.end_at >= ?', DateTime.now)
        .active
        .limit(3)
      format.json { render template: 'events/index', status: :ok }
    end
  end

  # GET /events/1
  def show
    respond_to do |format|
      format.json { render template: 'events/show', status: :ok }
    end
  end

  # POST /events
  def create
    @event = Event.new(event_params)

    if @event.save
      render json: @event, status: :created, location: @event
    else
      render json: @event.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /events/1
  def update
    if @event.update(event_params)
      render json: @event
    else
      render json: @event.errors, status: :unprocessable_entity
    end
  end

  # DELETE /events/1
  def destroy
    @event.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_event
      @event = Event.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def event_params
      params.require(:event).permit(:event_type, :begin_at, :end_at, :name, :description, :status, :url)
    end
end
