From bace782b5471772ecf01c22568a683dbacfa7e3d Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Fri, 22 Dec 2023 21:17:02 +1100 Subject: [PATCH] Create books via admin --- slices/admin/actions/books/create.rb | 26 ++++++++++++++++++++ slices/admin/config/routes.rb | 1 + slices/admin/repos/book_repo.rb | 2 +- slices/admin/templates/books/index.html.slim | 21 ++++++++++++++++ slices/admin/views/books/create.rb | 10 ++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 slices/admin/actions/books/create.rb create mode 100644 slices/admin/views/books/create.rb diff --git a/slices/admin/actions/books/create.rb b/slices/admin/actions/books/create.rb new file mode 100644 index 0000000..489fdf3 --- /dev/null +++ b/slices/admin/actions/books/create.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Admin + module Actions + module Books + class Create < Admin::Action + + include Deps["repos.book_repo"] + + def handle(req, resp) + book_repo.create({ + name: req.params[:name], + book_author: req.params[:book_author], + slug: req.params[:slug], + content: "", + book_status: req.params[:book_status], + post_type: "book", + published_at: Time.now + }) + + resp.redirect_to "/admin/books" + end + end + end + end +end diff --git a/slices/admin/config/routes.rb b/slices/admin/config/routes.rb index 30aae65..117eed4 100644 --- a/slices/admin/config/routes.rb +++ b/slices/admin/config/routes.rb @@ -67,5 +67,6 @@ module Admin get "/books", to: Auth.call(action: "books.index") patch "/books/:id", to: Auth.call(action: "books.update") + post "/books", to: Auth.call(action: "books.create") end end diff --git a/slices/admin/repos/book_repo.rb b/slices/admin/repos/book_repo.rb index 9d1d177..fe02755 100644 --- a/slices/admin/repos/book_repo.rb +++ b/slices/admin/repos/book_repo.rb @@ -1,7 +1,7 @@ module Admin module Repos class BookRepo < Adamantium::Repo[:posts] - commands update: :by_pk + commands :create, update: :by_pk def list_all posts diff --git a/slices/admin/templates/books/index.html.slim b/slices/admin/templates/books/index.html.slim index d59a3f1..2d73d75 100644 --- a/slices/admin/templates/books/index.html.slim +++ b/slices/admin/templates/books/index.html.slim @@ -2,6 +2,27 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark: h1 Admin // Books div class="max-w-prose prose dark:prose-invert mx-auto" + form action="/admin/books" method="POST" + div class="p-2" + label class="mr-4" for="name" Name + input type="text" name="name" id="name" class="text-gray-900 p-2" + div class="p-2" + label class="mr-4" for="book_author" Author + input type="text" name="book_author" id="book_author" class="text-gray-900 p-2" + div class="p-2" + label class="mr-4" for="slug" Slug + input type="text" name="slug" id="slug" class="text-gray-900 p-2" + div class="p-2" + label for="book_status" + select class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg" name="book_status" + option value="read" Read + option value="to-read" To Read + option value="reading" Reading + div class="p-2" + button + = "Create book" + + - books.each do |book| div class="w-full" diff --git a/slices/admin/views/books/create.rb b/slices/admin/views/books/create.rb new file mode 100644 index 0000000..e6d6ecf --- /dev/null +++ b/slices/admin/views/books/create.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Admin + module Views + module Books + class Create < Admin::View + end + end + end +end