#
# Projects Controller
#
# @author [alameen]
#
class ProjectsController < ApplicationController
  before_action :set_project, only: [:edit, :update, :destroy,
    :getting_started]

  def new
    @project = Project.new
  end

  def create
    @project = Project.new(project_params)

    if @project.save
      @project.add_user(current_user, :admin)
      redirect_to getting_started_project_path(@project),
                  notice: t('project.created')
    else
      render 'new'
    end
  end

  def update
    @project.update(project_params) && flash[:notice] = t('project.updated')
    render 'edit'
  end

  def destroy
    @project.destroy
    redirect_to dashboard_home_index_path, notice: t('project.destroyed')
  end

  private

  def project_params
    params.require(:project).permit(:name, :technology)
  end

  def set_project
    @project = current_user.projects
               .where('projects.id = ?', params[:id]).take
  end
end
