diff --git a/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/admin_catalog.rb b/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/admin_catalog.rb new file mode 100644 index 00000000..d7cc7575 --- /dev/null +++ b/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/admin_catalog.rb @@ -0,0 +1,36 @@ +module AdminCatalog + + class Migration + def change + ActiveRecord::Base.connection.create_table :admin_catalog_products do |t| + t.string :product_id + t.string :name + t.decimal :price + + t.timestamps + end + end + end + + class Product < ActiveRecord::Base + self.table_name = 'admin_catalog_products' + end + + class Configuration + def call(event_store) + event_store.subscribe( + -> (event) {Product.create(product_id: event.data[:product_id])}, + to: [ProductCatalog::ProductRegistered]) + event_store.subscribe( + -> (event) {Product.find_by(product_id: event.data[:product_id]).update(name: event.data[:name])}, + to: [ProductCatalog::ProductNamed]) + event_store.subscribe( + -> (event) {Product.find_by(product_id: event.data[:product_id]).update(price: event.data[:price])}, + to: [Pricing::PriceSet]) + end + + private + + end + +end \ No newline at end of file diff --git a/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/index.html.erb b/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/index.html.erb new file mode 100644 index 00000000..67a6aa09 --- /dev/null +++ b/pricing_catalog_rails_app/app/admin/read_models/admin_catalog/index.html.erb @@ -0,0 +1,19 @@ +Admin Catalog + +<%= form_for new_product, url: {controller: "admin/catalog", action: "create"} do |f| %> +

+ <%= f.label :name %> + <%= f.text_field :name %> +

+

+ <%= f.label :price %> + <%= f.text_field :price %> +

+ <%= f.submit %> +<% end %> + + \ No newline at end of file diff --git a/pricing_catalog_rails_app/app/controllers/admin/catalog_controller.rb b/pricing_catalog_rails_app/app/controllers/admin/catalog_controller.rb new file mode 100644 index 00000000..a7a36837 --- /dev/null +++ b/pricing_catalog_rails_app/app/controllers/admin/catalog_controller.rb @@ -0,0 +1,16 @@ +class Admin::CatalogController < ApplicationController + def index + products = AdminCatalog::Product.all + prepend_view_path Rails.root.join("app", "admin", "read_models") + render template: "admin_catalog/index", + locals: { products: products, new_product: AdminCatalog::Product.new } + end + + def create + RegisterProduct.new.call( + params[:admin_catalog_product][:name], + params[:admin_catalog_product][:price] + ) + redirect_to admin_root_path + end +end \ No newline at end of file diff --git a/pricing_catalog_rails_app/config/initializers/rails_event_store.rb b/pricing_catalog_rails_app/config/initializers/rails_event_store.rb index a5455ee6..78bde65e 100644 --- a/pricing_catalog_rails_app/config/initializers/rails_event_store.rb +++ b/pricing_catalog_rails_app/config/initializers/rails_event_store.rb @@ -4,6 +4,7 @@ require "rails_event_store" require "arkency/command_bus" require_relative "../../app/public/read_models/public_catalog/public_catalog" +require_relative "../../app/admin/read_models/admin_catalog/admin_catalog" Rails.configuration.to_prepare do Rails.configuration.event_store = Infra::EventStore.main @@ -19,6 +20,7 @@ def call(event_store, command_bus) ProductCatalog::Configuration.new, ].each { |c| c.call(event_store, command_bus) } PublicCatalog::Configuration.new.call(event_store) + AdminCatalog::Configuration.new.call(event_store) end diff --git a/pricing_catalog_rails_app/config/routes.rb b/pricing_catalog_rails_app/config/routes.rb index ad4f5ffe..e77d0686 100644 --- a/pricing_catalog_rails_app/config/routes.rb +++ b/pricing_catalog_rails_app/config/routes.rb @@ -8,4 +8,9 @@ # Defines the root path route ("/") root "catalog#index" + + namespace :admin do + root "catalog#index" + post "catalog", to: "catalog#create" + end end diff --git a/pricing_catalog_rails_app/db/migrate/20240201154705_create_admin_products.rb b/pricing_catalog_rails_app/db/migrate/20240201154705_create_admin_products.rb new file mode 100644 index 00000000..dd7f3b56 --- /dev/null +++ b/pricing_catalog_rails_app/db/migrate/20240201154705_create_admin_products.rb @@ -0,0 +1,5 @@ +class CreateAdminProducts < ActiveRecord::Migration[7.1] + def change + AdminCatalog::Migration.new.change + end +end diff --git a/pricing_catalog_rails_app/db/schema.rb b/pricing_catalog_rails_app/db/schema.rb index 94bee508..f73ac726 100644 --- a/pricing_catalog_rails_app/db/schema.rb +++ b/pricing_catalog_rails_app/db/schema.rb @@ -10,7 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_01_133038) do +ActiveRecord::Schema[7.1].define(version: 2024_02_01_154705) do + create_table "admin_catalog_products", force: :cascade do |t| + t.string "product_id" + t.string "name" + t.decimal "price" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "event_store_events", force: :cascade do |t| t.string "event_id", limit: 36, null: false t.string "event_type", null: false