关注列表/点赞列表/fork列表的后端

This commit is contained in:
sylor_huang@126.com 2020-05-15 17:05:53 +08:00
parent cf20cc3a0b
commit 46c929a394
12 changed files with 87 additions and 2 deletions

View File

@ -2,8 +2,9 @@ class ProjectsController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
before_action :require_login, except: %i[index branches group_type_list]
before_action :find_project_with_id, only: %i[show branches update destroy]
before_action :find_project_with_id, only: %i[show branches update destroy fork_users praise_users watch_user]
before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_user]
def index
scope = Projects::ListQuery.call(params)
@ -81,6 +82,21 @@ class ProjectsController < ApplicationController
tip_exception(e.message)
end
def watch_users
watchers = @project.watchers.includes(:user).distinct
@watchers = paginate(watchers)
end
def parise_users
praises = @project.praise_treads.includes(:user).distinct
@praises = paginate(praises)
end
def fork_users
fork_users = @project.fork_users.includes(:user, :project).distinct
@fork_users = paginate(fork_users)
end
private
def project_params
params.permit(:user_id, :name, :description, :repository_name,
@ -91,4 +107,10 @@ class ProjectsController < ApplicationController
params.permit(:user_id, :name, :description, :repository_name,
:project_category_id, :project_language_id, :clone_addr, :private)
end
def project_public?
unless @project.is_public || current_user&admin?
tip_exception(403, "..")
end
end
end

View File

@ -13,7 +13,7 @@ module Watchable
end
def watch!(watchable)
watchable.watchers.create!(user: self)
watchable.watchers.create!(user: self, created_at: Time.current)
end
def unwatch!(watchable)

4
app/models/fork_user.rb Normal file
View File

@ -0,0 +1,4 @@
class ForkUser < ApplicationRecord
belongs_to :project
belongs_to :user
end

View File

@ -12,6 +12,8 @@ class Project < ApplicationRecord
belongs_to :project_category, optional: true , :counter_cache => true
belongs_to :project_language, optional: true , :counter_cache => true
has_many :project_trends, dependent: :destroy
has_many :watchers, as: :watchable, dependent: :destroy
has_many :fork_users, dependent: :destroy
# has_many :commits, dependent: :destroy

View File

@ -51,6 +51,7 @@ class User < ApplicationRecord
has_one :wechat_open_user, class_name: 'OpenUsers::Wechat'
has_one :qq_open_user, class_name: 'OpenUsers::QQ'
accepts_nested_attributes_for :user_extension, update_only: true
has_many :fork_users, dependent: :destroy
has_many :versions
has_many :issue_times, :dependent => :destroy

View File

@ -0,0 +1,8 @@
user = target.user
json.format_time target.created_at.strftime("%Y-%m-%d")
json.name user.try(:show_real_name)
json.login user.try(:login)
json.image_url url_to_avatar(user)
json.is_current_user current_user.try(:id) == target.user_id
json.is_watch current_user&.watched?(user)

View File

@ -0,0 +1,12 @@
json.count @fork_users.size
json.fork_users do
json.array! @fork_users.each do |f|
user = f.user
fork_project = Project.select(:id,).find_by(f.fork_project_id)
json.id f.fork_project_id
json.name "#{user.try(:show_real_name)}/#{fork_project.name}"
json.login user.try(:login)
json.image_url url_to_avatar(user)
json.format_time f.created_at.strftime("%Y-%m-%d")
end
end

View File

@ -0,0 +1,4 @@
json.count @praises.size
json.praises do
json.partial! "/projects/list_user", collection: @praises, as: :target
end

View File

@ -0,0 +1,4 @@
json.cout @watch_users.size
json.watchers do
json.partial! "/projects/list_user", collection: @watch_user, as: :target
end

View File

@ -0,0 +1,6 @@
class AddCreatedAtToWatcher < ActiveRecord::Migration[5.2]
def change
add_column :watchers, :created_at, :datetime
Watcher.update_all(created_at: Time.current)
end
end

View File

@ -0,0 +1,17 @@
class CreateForkUsers < ActiveRecord::Migration[5.2]
def change
create_table :fork_users do |t|
t.integer :project_id #原始项目id
t.integer :fork_project_id #fork后的项目id
t.integer :user_id #fork用户的id
t.timestamps
end
add_index :fork_users, :project_id
add_index :fork_users, :user_id
projects = Project.where.not(forked_from_project_id: [nil, ""])
projects.each do |p|
ForkUser.create(project_id: p.forked_from_project_id, fork_project_id: p.id, user_id: p.user_id)
end
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe ForkUser, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end