Skip to content

Commit

Permalink
Admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejkrzywda committed Feb 7, 2024
1 parent f78c954 commit c5f96c1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Admin Catalog

<%= form_for new_product, url: {controller: "admin/catalog", action: "create"} do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :price %>
<%= f.text_field :price %>
</p>
<%= f.submit %>
<% end %>

<ul>
<% products.each do |product| %>
<li> <%= product.name %>, $<%= product.price %></li>
<% end %>
</ul>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions pricing_catalog_rails_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CreateAdminProducts < ActiveRecord::Migration[7.1]
def change
AdminCatalog::Migration.new.change
end
end
10 changes: 9 additions & 1 deletion pricing_catalog_rails_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5f96c1

Please sign in to comment.