Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing problems with error handling #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ def create

def show
@article = Article.find_by(id: params[:id])

if @article
render 'show'
else
flash[:alert] = "Error showing the article."
redirect_to articles_path
end
end

def destroy
Expand All @@ -56,18 +49,12 @@ def update
if @article.update(article_params)
redirect_to @article, notice: 'Article was successfully updated.'
else
flash[:alert] = "Article not found."
render :edit
end
end
def edit
@article = Article.find_by(id: params[:id])

if @article
render 'edit'
else
flash[:alert] = "Article not found."
redirect_to articles_path
end
end

def my_articles
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SubscriptionsController < ApplicationController
before_action :authenticate_user!

def create
result = Subscriptions::Subscribe.new(params,current_user,article).call
Subscriptions::Subscribe.new(params,current_user,article).call
redirect_to article, notice: 'Subscribed to the article.'
end

Expand Down
6 changes: 0 additions & 6 deletions app/mailers/subscription_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
class SubscriptionMailer < ApplicationMailer
def new_subscription_notification(subscriber, article)
raise ArgumentError, "Subscriber cannot be nil" if subscriber.nil?
raise ArgumentError, "Article cannot be nil" if article.nil?

@subscriber = subscriber
@article = article


mail(to: subscriber.email, subject: "You subscribe new article which title is #{article.title}")
end
def delete_subscription_notification(subscriber, article)
raise ArgumentError, "Subscriber cannot be nil" if subscriber.nil?
raise ArgumentError, "Article cannot be nil" if article.nil?

@subscriber = subscriber
@article = article

Expand Down
15 changes: 5 additions & 10 deletions app/services/subscriptions/subscribe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ def call
end

def new_subscription
if @article
@user.subscribed_articles << @article
else
raise ArgumentError, "Cannot create subscription without an article"
@user.subscribed_articles << @article

if [email protected]_articles.include?(@article)
flash[:alert] = "Error creating the subscription."
redirect_to @article
end
rescue StandardError => e
puts "Error in new_subscription: #{e.message}"
end

def adding_points
@user.update!(points: @user.points + 10)
rescue StandardError => e
puts "Error in adding_points: #{e.message}"
end

def sending_mails
SubscriptionMailer.new_subscription_notification(@user, @article).deliver_now
rescue StandardError => e
puts "Error in sending_mails: #{e.message}"
end
end
end
6 changes: 0 additions & 6 deletions app/services/subscriptions/unsubscribe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ def cancel_subscription
end
def cancel_points
@user.update!(points: @user.points - 10)

rescue StandardError => e
puts "Error in cancel_points: #{e.message}"
end
def cancel_sending_mails
SubscriptionMailer.delete_subscription_notification(@user, @article).deliver_now

rescue StandardError => e
puts "Error in sending_mails: #{e.message}"
end
end
end
Expand Down