feat: 上位タグ(#64) (#173)
#64 おそらく完成 Merge remote-tracking branch 'origin/main' into feature/064 #64 バックエンドぼちぼち Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #173
This commit was merged in pull request #173.
This commit is contained in:
@@ -52,9 +52,12 @@ class PostsController < ApplicationController
|
||||
|
||||
viewed = current_user&.viewed?(post) || false
|
||||
|
||||
render json: (post
|
||||
.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
|
||||
.merge(related: post.related(limit: 20), viewed:))
|
||||
json = post.as_json
|
||||
json['tags'] = build_tag_tree_for(post.tags)
|
||||
json['related'] = post.related(limit: 20)
|
||||
json['viewed'] = viewed
|
||||
|
||||
render json:
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
@@ -78,6 +81,7 @@ class PostsController < ApplicationController
|
||||
if post.save
|
||||
post.resized_thumbnail!
|
||||
post.tags = Tag.normalise_tags(tag_names)
|
||||
post.tags = Tag.expand_parent_tags(post.tags)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
|
||||
status: :created
|
||||
else
|
||||
@@ -112,9 +116,11 @@ class PostsController < ApplicationController
|
||||
post = Post.find(params[:id].to_i)
|
||||
tags = post.tags.where(category: 'nico').to_a +
|
||||
Tag.normalise_tags(tag_names, with_tagme: false)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
if post.update(title:, tags:, original_created_from:, original_created_before:)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
|
||||
status: :ok
|
||||
json = post.as_json
|
||||
json['tags'] = build_tag_tree_for(post.tags)
|
||||
render json:, status: :ok
|
||||
else
|
||||
render json: post.errors, status: :unprocessable_entity
|
||||
end
|
||||
@@ -143,4 +149,46 @@ class PostsController < ApplicationController
|
||||
end
|
||||
posts.distinct
|
||||
end
|
||||
|
||||
def build_tag_tree_for tags
|
||||
tags = tags.to_a
|
||||
tag_ids = tags.map(&:id)
|
||||
|
||||
implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
|
||||
|
||||
children_ids_by_parent = Hash.new { |h, k| h[k] = [] }
|
||||
implications.each do |imp|
|
||||
children_ids_by_parent[imp.parent_tag_id] << imp.tag_id
|
||||
end
|
||||
|
||||
child_ids = children_ids_by_parent.values.flatten.uniq
|
||||
|
||||
root_ids = tag_ids - child_ids
|
||||
|
||||
tags_by_id = tags.index_by(&:id)
|
||||
|
||||
memo = { }
|
||||
|
||||
build_node = -> tag_id, path do
|
||||
tag = tags_by_id[tag_id]
|
||||
return nil unless tag
|
||||
|
||||
if path.include?(tag_id)
|
||||
return tag.as_json(only: [:id, :name, :category, :post_count]).merge(children: [])
|
||||
end
|
||||
|
||||
if memo.key?(tag_id)
|
||||
return memo[tag_id]
|
||||
end
|
||||
|
||||
new_path = path + [tag_id]
|
||||
child_ids = children_ids_by_parent[tag_id] || []
|
||||
|
||||
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
||||
|
||||
memo[tag_id] = tag.as_json(only: [:id, :name, :category, :post_count]).merge(children:)
|
||||
end
|
||||
|
||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user