From 43d5f697c063faf8f7d88062fe47623691d71617 Mon Sep 17 00:00:00 2001 From: Asrani-Aman Date: Mon, 29 Jul 2024 12:52:33 +0530 Subject: [PATCH] feat: votes functionality and validations --- app/controllers/contests_controller.rb | 23 ++++++++++++++++++++++- app/models/contest.rb | 1 + app/models/submission_vote.rb | 1 + app/models/user.rb | 5 +++++ app/views/contests/_submission.html.erb | 2 +- app/views/contests/_view.html.erb | 1 - app/views/contests/show.html.erb | 6 +++++- config/routes.rb | 1 + 8 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 38343b50b5a..0cabf9d4dab 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -78,13 +78,34 @@ def create_submission end end - # PUT /contests/:id/withdraw + # PUT /contests/:contest_id/withdraw/:submission_id def withdraw @submission = Submission.find(params[:submission_id]) @submission.destroy! redirect_to contest_page_path(params[:contest_id]), notice: "Submission was successfully removed." end + # POST /contests/:contest_id/submission/:submission_id/upvote + def upvote + user_contest_votes = current_user.user_contest_votes(params[:contest_id]) + if user_contest_votes > 3 + notice = "You have used all your votes!" + else + vote = SubmissionVote.find_by(user_id: current_user.id, submission_id: params[:submission_id]) + if vote.nil? + @submission_vote = SubmissionVote.new + @submission_vote.user_id = current_user.id + @submission_vote.submission_id = params[:submission_id] + @submission_vote.contest_id = params[:contest_id] + @submission_vote.save! + notice = "You have successfully voted the submission, Thanks! Votes remaining: #{2 - user_contest_votes}" + else + notice = "You have already vote this submission!" + end + end + redirect_to contest_page_path(params[:contest_id]), notice: notice + end + private def authorize_admin diff --git a/app/models/contest.rb b/app/models/contest.rb index 3a5c08562f0..f1be6b752ae 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -3,4 +3,5 @@ class Contest < ApplicationRecord has_noticed_notifications model_name: "NoticedNotification" has_many :submissions, dependent: :destroy + has_many :submission_votes, dependent: :destroy end \ No newline at end of file diff --git a/app/models/submission_vote.rb b/app/models/submission_vote.rb index 1608a3b0dba..fafeeec4c19 100644 --- a/app/models/submission_vote.rb +++ b/app/models/submission_vote.rb @@ -3,4 +3,5 @@ class SubmissionVote < ApplicationRecord belongs_to :submission belongs_to :user + belongs_to :contest end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index a06d3c00e17..d8e84524e53 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -102,6 +102,11 @@ def send_devise_notification(notification, *args) devise_mailer.send(notification, self, *args).deliver_later end + def user_contest_votes(contest) + SubmissionVote.where(user_id: id, contest_id: contest).count + end + + private def send_welcome_mail diff --git a/app/views/contests/_submission.html.erb b/app/views/contests/_submission.html.erb index 3fdf8e556a8..95e41d2be96 100644 --- a/app/views/contests/_submission.html.erb +++ b/app/views/contests/_submission.html.erb @@ -16,7 +16,7 @@ <% if project.author == current_user && contest.status == 'Live' %> <%= link_to "Withdraw", '#', class: "previewButton btn primary-button danger-primary-button", data: {toggle: "modal", target: "#withdraw-submission-confirmation-modal", submission: submission, contest: contest} %> <% elsif project.author != current_user && contest.status == 'Live' %> - <%= link_to "Vote", '#', class: "previewButton btn primary-button" %> + <%= link_to "Vote", vote_submission_path(contest.id, submission.id), class: "previewButton btn primary-button", method: :post %> <% end %> diff --git a/app/views/contests/_view.html.erb b/app/views/contests/_view.html.erb index e0a1e91cca8..dc3195df92a 100644 --- a/app/views/contests/_view.html.erb +++ b/app/views/contests/_view.html.erb @@ -11,7 +11,6 @@ diff --git a/app/views/contests/show.html.erb b/app/views/contests/show.html.erb index 6dff32cfd31..199c6f0cffd 100644 --- a/app/views/contests/show.html.erb +++ b/app/views/contests/show.html.erb @@ -11,7 +11,11 @@

Contest #<%= @contest.id %>

Get a chance to get featured on our website and show off your circuit to <%= @user_count %> users!

+ <% if @contest.status == 'Live' %> +

Your Remaining Votes: <%= 3 - current_user.user_contest_votes(@contest.id) %>

+ <% end %>
+ <% if @contest.status == 'Live' %>
Time Left: ... @@ -40,7 +44,7 @@
Contest Entries(<%= @contest.submissions.count %>)
- Vote for your favourite entries and help us choose our winners for this week! One user gets 1 votes. + Vote for your favourite entries and help us choose our winners for this week! One user gets 3 votes.
<% @submissions.each do |submission| %> diff --git a/config/routes.rb b/config/routes.rb index 7a802aa91f2..df46c1e5878 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,6 +112,7 @@ get "/contests/:id/new_submission", to: "contests#new_submission", as: "new_submission" post "/contests/:id/create_submission", to: "contests#create_submission", as: "create_submission" delete "/contests/:contest_id/withdraw/:submission_id", to: "contests#withdraw", as: "withdraw_submission" + post "/contests/:contest_id/upvote/:submission_id", to: "contests#upvote", as: "vote_submission" # lti scope "lti" do