From e169f9e361350e59bd63a0df7e7bbd9ea45bbe8f Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Mon, 6 Jan 2020 10:32:44 +0800 Subject: [PATCH] ADD repository show api --- app/controllers/repositories_controller.rb | 7 +++++++ app/models/project.rb | 1 + app/models/watcher.rb | 4 ++-- app/views/repositories/show.json.jbuilder | 5 +++++ config/routes.rb | 11 +++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 app/views/repositories/show.json.jbuilder diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index d2624eebe..8e9412123 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -2,6 +2,9 @@ class RepositoriesController < ApplicationController include ApplicationHelper before_action :find_user, :find_repository, :authorizate! + def show + end + def entries @entries = Gitea::Repository::Entries::ListService.new(@user, @repo.identifier, ref: params[:ref]).call end @@ -23,6 +26,10 @@ class RepositoriesController < ApplicationController @commit = Gitea::Repository::Commits::GetService.new(@user, @repo.identifier, params[:sha]).call end + def tags + @tags = Gitea::Repository::Tags::ListService.new(@user, @repo.identifier).call + end + private def authorizate! if @repo.hidden? && @repo.user != current_user diff --git a/app/models/project.rb b/app/models/project.rb index 0b02cd4e2..7e4c7a224 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,6 +1,7 @@ class Project < ApplicationRecord include Matchable include Publicable + include Watchable enum project_type: { mirror: 1, common: 0 } # common:开源托管项目, mirror:开源镜像项目 diff --git a/app/models/watcher.rb b/app/models/watcher.rb index d9426e03c..3b613b9f0 100644 --- a/app/models/watcher.rb +++ b/app/models/watcher.rb @@ -1,4 +1,4 @@ class Watcher < ApplicationRecord belongs_to :user - belongs_to :watchable, polymorphic: true -end \ No newline at end of file + belongs_to :watchable, polymorphic: true, counter_cache: :watchers_count +end diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder new file mode 100644 index 000000000..a515a9509 --- /dev/null +++ b/app/views/repositories/show.json.jbuilder @@ -0,0 +1,5 @@ +json.identifier @repo.identifier +json.praises_count @repo.project.praises_count +json.forked_count @repo.project.forked_count +json.watchers_count @repo.project.watchers_count +json.partial! 'author', locals: { user: @repo.user } diff --git a/config/routes.rb b/config/routes.rb index 19d41c18b..fbad0c3e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + require 'sidekiq/web' require 'admin_constraint' mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new @@ -59,12 +60,22 @@ Rails.application.routes.draw do end end + get '/:login/:repo_identifier', to: 'repositories#show' resources :repositories, path: '/:login/:repo_identifier', only: [:index] do collection do get :entries get :sub_entries get :commits get :single_commit + post :files + get :tags + end + end + + resources :contents, path: '/:login/:repo_identifier/contents', only: [:create] do + collection do + put 'files/update', :action => 'update_file' + delete 'files/delete', :action => 'delete_file' end end