From 2a7a19c93ee0e0d77e4e388d43f36a721c7ab715 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 20 Oct 2023 21:27:06 -0400 Subject: Added post voting --- app/controllers/blogs_controller.rb | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'app/controllers') diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 0d218ae..2f9df49 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -38,4 +38,52 @@ class BlogsController < ApplicationController }) end + def upvote + @blog = Blog.find_by_slug(params[:slug]) + + raise ActiveRecord::RecordNotFound unless @blog + raise ActiveRecord::RecordNotFound unless @blog.published + + respond_to do |format| + if @blog.upvote! request.remote_ip + format.html do + flash[:notice] = "You have upvoted the blog post \"#{@blog.title}\"." + redirect_to @blog + end + format.js { render "voted" } + format.xml { head :ok } + else + format.html do + flash[:notice] = "You have already voted on the blog post \"#{@blog.title}\"." + redirect_to @blog + end + format.xml { render :xml => { :error => "Someone from your IP address has already voted on this blog post."} } + end + end + end + + def downvote + @blog = Blog.find_by_slug(params[:slug]) + + raise ActiveRecord::RecordNotFound unless @blog + raise ActiveRecord::RecordNotFound unless @blog.published + + respond_to do |format| + if @blog.downvote! request.remote_ip + format.html do + flash[:notice] = "You have downvoted the blog post \"#{@blog.title}\"." + redirect_to @blog + end + format.js { render "voted" } + format.xml { head :ok } + else + format.html do + flash[:notice] = "You have already voted on the blog post \"#{@blog.title}\"." + redirect_to @blog + end + format.xml { render :xml => { :error => "Someone from your IP address has already voted on this blog post."} } + end + end + end + end -- cgit 1.4.1