diff --git a/app/controllers/concerns/followable_controller.rb b/app/controllers/concerns/followable_controller.rb index d703c163162c3a8bca651319c9d23ace9ac06303..d4d1cf569c518cd6fa3caf4e6aaf47478e6e12e9 100644 --- a/app/controllers/concerns/followable_controller.rb +++ b/app/controllers/concerns/followable_controller.rb @@ -28,4 +28,18 @@ module FollowableController render status: :forbidden end end + + # PUT /v1/users/1/follow + # PUT /v1/users/1/follow.json + def follow_toggle + if !current_user.following?(followable) + current_user.follow(followable) + render status: :created + elsif current_user.following?(followable) + current_user.unfollow(followable) + render status: :ok + else + render status: :forbidden + end + end end diff --git a/config/routes.rb b/config/routes.rb index df4dbc69e123122d8a2d473e6402c455aeb2d470..2ef9a747bce42a72c79b623e5ba1d374077703ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ Rails.application.routes.draw do member do post 'follow', as: :follow, action: :follow delete 'follow', as: :unfollow, action: :unfollow + put 'follow', as: :follow_toggle, action: :follow_toggle end end