class RepositoriesController < ApplicationController
  include ApplicationHelper
  before_action :find_user, :find_repository, :authorizate!

  def entries
    @entries = Gitea::Repository::Entries::ListService.new(@user, @repo.identifier, ref: params[:ref]).call
  end

  def sub_entries
    interactor = Repositories::EntriesInteractor.call(@user, @repo.identifier, params[:filepath], ref: params[:ref])
    if interactor.success?
      @sub_entries = interactor.result
    else
      render_error(interactor.error)
    end
  end

  def commits
    @hash_commit = Gitea::Repository::Commits::ListService.new(@user, @repo.identifier, sha: params[:sha], page: params[:page]).call
  end

  def single_commit
    @commit = Gitea::Repository::Commits::GetService.new(@user, @repo.identifier, params[:sha]).call
  end

  private
  def authorizate!
    if @repo.hidden? && @repo.user != current_user
      render_forbidden
    end
  end
end