From 96813a5e508a54257ef03be613a704f1f71af53d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 21 Oct 2023 00:25:50 -0400 Subject: Added quotes database --- app/controllers/quotes_controller.rb | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 app/controllers/quotes_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb new file mode 100644 index 0000000..fb5e33c --- /dev/null +++ b/app/controllers/quotes_controller.rb @@ -0,0 +1,97 @@ +class QuotesController < ApplicationController + def index + @quote = Quote.find(310) + @qnumber = Quote.published.count + @mnumber = Quote.pending.count + end + + def latest + @quotes = Quote.published.order(id: :desc).paginate(page: params[:page], per_page: 10) + + respond_to do |format| + format.html { render :list } + format.json { render :json => @quotes } + format.xml { render :xml => @quotes } + format.atom { render :atom => @quotes } + end + end + + def top + @quotes = Quote.published.order(Arel.sql("(upvotes - downvotes) DESC")).paginate(page: params[:page], per_page: 10) + + respond_to do |format| + format.html { render :list } + format.json { render :json => @quotes } + format.xml { render :xml => @quotes } + end + end + + def tags + @tags = Quote.published.tag_counts_on(:tags) + end + + def tag + @quotes = Quote.published.tagged_with(params[:id]).order(id: :desc).paginate(page: params[:page], per_page: 10) + + respond_to do |format| + format.html { render :list } + format.json { render :json => @quotes } + format.xml { render :xml => @quotes } + end + end + + def show + @quote = Quote.published.find(params[:id]) + + respond_to do |format| + format.html + format.json { render :json => @quote } + format.xml { render :xml => @quote } + end + end + + def new + end + + def upvote + @quote = Quote.published.find(params[:id]) + + respond_to do |format| + if @quote.upvote! request.remote_ip + format.html do + flash[:notice] = "You have upvoted Quote \"#{@quote.id}\"." + redirect_to @quote + end + format.js { render "voted" } + format.xml { head :ok } + else + format.html do + flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." + redirect_to @quote + end + format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } + end + end + end + + def downvote + @quote = Quote.published.find(params[:id]) + + respond_to do |format| + if @quote.downvote! request.remote_ip + format.html do + flash[:notice] = "You have downvoted Quote \"#{@quote.id}\"." + redirect_to @quote + end + format.js { render "voted" } + format.xml { head :ok } + else + format.html do + flash[:notice] = "You have already voted on Quote \"#{@quote.id}\"." + redirect_to @quote + end + format.xml { render :xml => { :error => "Someone from your IP address has already voted on this quote."} } + end + end + end +end -- cgit 1.4.1