class Admin::AuthorsController < Admin::DashboardController
	
	before_action :set_author, only: [:edit, :update, :show, :destroy]	

	def index
		@authors = Author.all
	end

	def new
		@author = Author.new
	end

	def create
		@author = Author.new(author_params)
		respond_to do |format|
			if @author.save
				format.html{ redirect_to admin_authors_path, :notice => "New Author has been created" }
			else
				format.html{ render :new }
			end
		end
	end

	def show
		
	end

	def edit
		
	end

	def update
		respond_to do |format|
			if @author.update(author_params)
				format.html{ redirect_to admin_authors_path, :notice => "Author has been updated" }
			else
				format.html{ render :new }
			end
		end
	end

	def destroy
		if @author.destroy
			flash[:notice] = "Author has been deleted successfully"
		else
			flash[:notice] = "There was an error in deleting author. Please try again"
		end
		redirect_to admin_authors_path
	end

	private

		def set_author
			@author = Author.find(params[:id]) if params[:id]
		end

		def author_params
			params.require(:author).permit(:name, :info, :linkedin, :twitter, :facebook, :post_url, :google_plus, :photo, :meta_description, :meta_title)
		end
end