docbrown/app/controllers/user_subscriptions_controller.rb
Alex 9ee024f9d0
UserSubscription backend refactor (#9060)
* Refactor controller validations to model

* Update specs and move current email validation back

* Refactor #subscribed

* Cleanup UserSubscriptions::Create service

* Refactor service

* Move source inside of call for clarity

* Specs for UserSubscription::Create

* Cleanup spec

* Fix Article spec

* Updated for subscriber_email

* Add comment for explicitness of subscriber_email

* Fix reference in comment...oops!

* I know what I'm doing...sometimes...maybe

* Rename CachChecker - Subscription to IsSubscribed

* Use include vs eq in specs for error messages

* attr_accessor --> attr_reader

* Don't explicitly set nil

* Rename Create service to CreateFromService

* Use errors_as_sentence

* Rename service to CreateFromControllerParams

* Assign Struct to a new constant
2020-07-02 12:08:37 -04:00

29 lines
1,001 B
Ruby

class UserSubscriptionsController < ApplicationController
before_action :authenticate_user!
USER_SUBSCRIPTION_PARAMS = %i[source_type source_id subscriber_email].freeze
def subscribed
params.require(%i[source_type source_id])
is_subscribed = UserSubscriptions::IsSubscribedCacheChecker.call(current_user, params)
render json: { is_subscribed: is_subscribed, success: true }, status: :ok
end
def create
rate_limit!(:user_subscription_creation)
user_subscription = UserSubscriptions::CreateFromControllerParams.call(current_user, user_subscription_params)
if user_subscription.success
rate_limiter.track_limit_by_action(:user_subscription_creation)
render json: { message: "success", success: true }, status: :ok
else
render json: { error: user_subscription.error, success: false }, status: :unprocessable_entity
end
end
def user_subscription_params
params.require(:user_subscription).permit(USER_SUBSCRIPTION_PARAMS)
end
end