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/services | |
| parent | f3e8563c45ee3a11211428acda1b8305e077545b (diff) | |
publish a command to add a favorites to a users collection as a background job.
Diffstat (limited to 'app/services')
| -rw-r--r-- | app/services/application/add_to_favorites.rb | 21 | ||||
| -rw-r--r-- | app/services/application/handlers/add_to_favorites.rb | 14 |
2 files changed, 14 insertions, 21 deletions
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 |
