diff --git a/slices/admin/actions/auto_tagging/create.rb b/slices/admin/actions/auto_tagging/create.rb index dc1f732..ac0c4c1 100644 --- a/slices/admin/actions/auto_tagging/create.rb +++ b/slices/admin/actions/auto_tagging/create.rb @@ -5,22 +5,32 @@ module Admin include Deps["commands.auto_tagging.create", "views.auto_tagging.index", - auto_tag: "commands.auto_tagging.tag"] + "validation.contracts.auto_tagging_contract", + auto_tag: "commands.auto_tagging.tag", + new_view: "views.auto_tagging.new"] def handle(req, res) title_contains = req.params[:title_contains] body_contains = req.params[:body_contains] tag_id = req.params[:tag_id] - result = create.(title_contains: title_contains, - body_contains: body_contains, - tag_id: tag_id) + validation = auto_tagging_contract.call(title_contains: title_contains, + body_contains: body_contains, + tag_id: tag_id) - if result.success? - auto_tag.(auto_tag_id: result.value!) + if validation.success? + result = create.(title_contains: title_contains, + body_contains: body_contains, + tag_id: tag_id) + + if result.success? + auto_tag.(auto_tag_id: result.value!) + end + + res.render index end - res.render index + res.render new_view, errors: validation.errors end end end diff --git a/slices/admin/actions/auto_tagging/new.rb b/slices/admin/actions/auto_tagging/new.rb index 940ec3f..dabf94d 100644 --- a/slices/admin/actions/auto_tagging/new.rb +++ b/slices/admin/actions/auto_tagging/new.rb @@ -6,7 +6,7 @@ module Admin include Deps[new_view: "views.auto_tagging.new"] def handle(req, res) - res.render new_view + res.render new_view, errors: {} end end end diff --git a/slices/admin/templates/auto_tagging/index.html.slim b/slices/admin/templates/auto_tagging/index.html.slim index 09ceb54..0e3c5a2 100644 --- a/slices/admin/templates/auto_tagging/index.html.slim +++ b/slices/admin/templates/auto_tagging/index.html.slim @@ -1,10 +1,11 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200" h1 Admin // Auto tagging -div class="prose dark:prose-invert max-w-prose mx-auto" +div class="prose dark:prose-invert max-w-prose mx-auto mb-12" - auto_taggings.each do |auto_tagging| - div id="auto-tag-#{auto_tagging.id}" + div id="auto-tag-#{auto_tagging.id}" class="mb-2" == "Tag post with #{auto_tagging.tag.label} when #{auto_tagging.title_only? ? "title" : "content"} contains #{auto_tagging.term}" = " — " button hx-delete="/admin/tags/auto_taggings/#{auto_tagging.id}" hx-target="#auto-tag-#{auto_tagging.id}" delete + a class="bg-blue-100 text-blue-600 p-1 rounded no-underline" href="/admin/tags/auto_tagging/new" New div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600" diff --git a/slices/admin/templates/auto_tagging/new.html.slim b/slices/admin/templates/auto_tagging/new.html.slim index 4ef2926..38487e5 100644 --- a/slices/admin/templates/auto_tagging/new.html.slim +++ b/slices/admin/templates/auto_tagging/new.html.slim @@ -2,6 +2,12 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark: h1 Admin // Auto tagging div class="max-w-prose mx-auto" + - if errors + div class="text-gray-100 dark:text-gray-100 bg-pink-100 dark:bg-pink-600 rounded px-4 py-2 mb-12" + p Errors: + - errors.each do |error| + li = "#{error[1].join(",")}" + form method="POST" action="/admin/tags/auto_tagging" div class="mb-4" label class="text-gray-800 dark:text-gray-200" for="title_contains" Title contains:  diff --git a/slices/admin/templates/index.html.slim b/slices/admin/templates/index.html.slim index bf7dc02..a43abad 100644 --- a/slices/admin/templates/index.html.slim +++ b/slices/admin/templates/index.html.slim @@ -1,10 +1,12 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200" h1 Admin -div class="max-w-prose mx-auto" +div class="max-w-prose mx-auto prose dark:prose-invert" ul li a href="/admin/tags" Tags + li + a href="/admin/tags/auto_tagging" Auto tagging div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600" diff --git a/slices/admin/validation/contracts/auto_tagging_contract.rb b/slices/admin/validation/contracts/auto_tagging_contract.rb new file mode 100644 index 0000000..f9e36fd --- /dev/null +++ b/slices/admin/validation/contracts/auto_tagging_contract.rb @@ -0,0 +1,18 @@ +module Admin + module Validation + module Contracts + class AutoTaggingContract < Dry::Validation::Contract + params do + required(:title_contains).maybe(:string) + required(:body_contains).maybe(:string) + required(:tag_id).filled(:integer) + end + + rule(:title_contains, :body_contains) do + key.failure("must provide a title or body term") if values[:title_contains].nil? && values[:body_contains].nil? + key.failure("must provide a title or body term") if !values[:title_contains].nil? && !values[:body_contains].nil? + end + end + end + end +end diff --git a/slices/admin/views/auto_tagging/new.rb b/slices/admin/views/auto_tagging/new.rb index 920e474..757579d 100644 --- a/slices/admin/views/auto_tagging/new.rb +++ b/slices/admin/views/auto_tagging/new.rb @@ -8,6 +8,10 @@ module Admin expose :tags do tag_repo.list.to_a end + + expose :errors do |errors:| + errors.empty? ? nil : errors.to_h + end end end end