From 2ff8f0a32fec83040891d15c188bc23b2e2cf2ad Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Fri, 3 Feb 2023 11:45:02 +1100 Subject: [PATCH] Add tags in edit endpoint --- app/commands/posts/update.rb | 2 +- app/repos/post_repo.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/commands/posts/update.rb b/app/commands/posts/update.rb index 9c244fd..a508a2d 100644 --- a/app/commands/posts/update.rb +++ b/app/commands/posts/update.rb @@ -13,7 +13,7 @@ module Adamantium end if params.key? :add - + post_repo.tag_post(post.id, params[:add][:category]) end if params.key? :delete diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index 05b6fcc..68ddc1e 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -27,6 +27,24 @@ module Adamantium end end + def tag_post(post_id:, tags:) + tags.each do |tag_name| + next if tag_name == "" + + tag = post.tags.where(label: tag_name).one || + post + .tags + .changeset(:create, {label: tag_name, slug: tag_name.downcase.strip.tr(" ", "-").gsub(/[^\w-]/, "")}) + .commit + + posts.post_tags.changeset(:create, { + post_id: post_id, + tag_id: tag[:id] + }) + .commit + end + end + def post_listing(limit: nil) posts .where(post_type: "post")