diff options
| author | mo khan <mo@mokhan.ca> | 2014-10-16 20:35:28 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-10-16 20:35:28 -0600 |
| commit | f2eef36abce2148fe044b6dbda85fadf77754019 (patch) | |
| tree | 33f25a21fd7487cafbad6578e057d71668bb1797 /app | |
| parent | f3e8563c45ee3a11211428acda1b8305e077545b (diff) | |
publish a command to add a favorites to a users collection as a background job.
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/favorites_controller.rb | 17 | ||||
| -rw-r--r-- | app/services/application/add_to_favorites.rb | 21 | ||||
| -rw-r--r-- | app/services/application/handlers/add_to_favorites.rb | 14 |
3 files changed, 24 insertions, 28 deletions
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index d284a8b2..b3986845 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,20 +1,23 @@ class FavoritesController < ApplicationController before_action :authenticate! + def initialize(command_bus = Spank::IOC.resolve(:command_bus)) + @bus = command_bus + super() + end + def index @creation = FindCreationQuery.new.fetch(params[:creation_id]) @favorites = @creation.favorites end def create - AddToFavorites.new(self).run(params[:creation_id]) + cake = Creation.find(params[:creation_id]) + bus.publish(:add_cake_to_favorites, { user_id: current_user.id, cake_id: cake.id }) + redirect_to cake, notice: "This has been added to your favorites" end - def favorite_created(cake, message) - redirect_to cake, notice: message - end + private - def create_favorite_failed(cake) - redirect_to cake - end + attr_reader :bus end diff --git a/app/services/application/add_to_favorites.rb b/app/services/application/add_to_favorites.rb deleted file mode 100644 index 3cd50409..00000000 --- a/app/services/application/add_to_favorites.rb +++ /dev/null @@ -1,21 +0,0 @@ -class AddToFavorites - def initialize(context, current_user = context.current_user) - @context = context - @current_user = current_user - end - - def run(creation_id) - cake = Creation.find(creation_id) - if @current_user.owns(cake) - @context.favorite_created(cake, "You can't favorite your own stuff") - return - end - - favorite = @current_user.add_favorite(cake) - if favorite.save - @context.favorite_created(cake, 'Welcome to the fanclub!') - else - @context.create_favorite_failed - end - end -end diff --git a/app/services/application/handlers/add_to_favorites.rb b/app/services/application/handlers/add_to_favorites.rb new file mode 100644 index 00000000..3e671903 --- /dev/null +++ b/app/services/application/handlers/add_to_favorites.rb @@ -0,0 +1,14 @@ +class AddToFavorites + def handles?(event) + :add_cake_to_favorites + end + + def handle(message) + cake = Creation.find(message[:cake_id]) + user = User.find(message[:user_id]) + return if user.owns(cake) + + favorite = user.add_favorite(cake) + favorite.save + end +end |
