<commit_before>class Api::V1::ProjectsController < ApplicationController
  # before_action :find_project
  respond_to :json

  # FIXME -- this method needs auth checking as well.`
  def show
    respond_with Project.find(params[:id])
  end

  # returns all projects if user is looking at own projects
  # returns public projects for users if otherwise
  def index
    find_project
    if @project
      respond_with @project
      return
    end
    if params[:user_id]
      begin
        id = Integer(params[:user_id])
      rescue
      end
      if !getCurrentUser.nil? && getCurrentUser.id == id
        respond_with Project.where(owner: id)
      else
        respond_with Project.where(owner: id, is_public: true)
      end
    end
  end

  def create
    project = Project.new({})
    project.save
    render :nothing => true, status: 200
  end


  def update
    if self.getCurrentUser
        project = Project.find_by_id(params[:id])
      if project
        if project.owner == self.getCurrentUser.id
          project.update_attributes(params[:project_params])
          project.save
          render :nothing => true, status: 204 #:no_content
        else
          render :nothing => true, status: 401 #:unauthorized
        end
      else
        render :nothing => true, status: 404 #:not_found
      end
    else
      render :nothing => true, status: 401 #:unauthorized
    end
  end


  def destroy
    project = Project.find(params[:id])
    if project.owner == self.getCurrentUser.id
      project.destroy
      render :nothing => true, status: 200 #:ok
    else
      render :nothing => true, status: 401 #:unauthorized
    end
  end

  def getCurrentUser
    return current_user
  end

  def project_listing
    # TODO: actually write this method lol
    project = Project.find(params[:id])
  end


  private

    def project_params
      params.require(:project).permit(:title, :notes, :thumbnail, :contents,
        :is_public, :owner, :last_modified, :created_at, :updated_at)
    end
    
    def find_project_by_name(username, projectname)
      # NOTE: This method needs serious consideration about how to handle
      # projects that can't be found, and ones that are explicitly private
      # 401 reveals that a project does at least exist...is this a problem?
      # TODO: Handle multiple ownership
      @owner = User.find_by!(username: username)
      @project = Project.find_by!(owner: @owner.id, title: projectname)
      if !@project.is_public && @project.owner != self.getCurrentUser.id
        access_denied
      end
    end
      
    def find_project
      if params[:username] && params[:projectname]
        find_project_by_name(params[:username], params[:projectname])
      elsif params[:id]
        @project = Project.find!(params[:id])
      end
    end

end<commit_msg>Some initial cleanup of projects API controller<commit_after>class Api::V1::ProjectsController < ProjectsController
  before_action :find_project, only: [:index]
  respond_to :json

  # FIXME -- this method needs auth checking as well.`
  def show
    respond_with Project.find(params[:id])
  end

  # returns all projects if user is looking at own projects
  # returns public projects for users if otherwise
  def index
    # find_project
    if @project
      respond_with @project
      return
    end
    if params[:user_id]
      begin
        id = Integer(params[:user_id])
      rescue
      end
      if !getCurrentUser.nil? && getCurrentUser.id == id
        respond_with Project.where(owner: id)
      else
        respond_with Project.where(owner: id, is_public: true)
      end
    end
  end

  def create
    project = Project.new({})
    project.save
    render :nothing => true, status: 200
  end


  def update
    if self.getCurrentUser
        project = Project.find_by_id(params[:id])
      if project
        if project.owner == self.getCurrentUser.id
          project.update_attributes(params[:project_params])
          project.save
          render :nothing => true, status: 204 #:no_content
        else
          render :nothing => true, status: 401 #:unauthorized
        end
      else
        render :nothing => true, status: 404 #:not_found
      end
    else
      render :nothing => true, status: 401 #:unauthorized
    end
  end


  def destroy
    project = Project.find(params[:id])
    if project.owner == self.getCurrentUser.id
      project.destroy
      render :nothing => true, status: 200 #:ok
    else
      render :nothing => true, status: 401 #:unauthorized
    end
  end

  def getCurrentUser
    return current_user
  end

  def project_listing
    # TODO: actually write this method lol
    project = Project.find(params[:id])
  end


  private

    def project_params
      params.require(:project).permit(:title, :notes, :thumbnail, :contents,
        :is_public, :owner, :last_modified, :created_at, :updated_at)
    end

end
{'fraction_non_alphanumeric': 0.08660130718954248, 'fraction_numerical': 0.00959967320261438, 'mean_word_length': 2.6166912850812407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6637420', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1383, 'n_words': 429}