-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f78c954
commit c5f96c1
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
pricing_catalog_rails_app/app/admin/read_models/admin_catalog/admin_catalog.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
pricing_catalog_rails_app/app/admin/read_models/admin_catalog/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
pricing_catalog_rails_app/app/controllers/admin/catalog_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
pricing_catalog_rails_app/db/migrate/20240201154705_create_admin_products.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters