Skip to content

Commit

Permalink
feat: votes functionality and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Asrani-Aman committed Jul 29, 2024
1 parent 322680c commit 43d5f69
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 4 deletions.
23 changes: 22 additions & 1 deletion app/controllers/contests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/contest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/submission_vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class SubmissionVote < ApplicationRecord
belongs_to :submission
belongs_to :user
belongs_to :contest
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/contests/_submission.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion app/views/contests/_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</div>
</div>
<div class="modal-footer">
<%= link_to "Vote", '#', class: "btn primary-button", id: "project-vote-button" %>
<%= link_to t("more"), '#', class: "btn primary-button", id: "project-more-button", target: "_blank" %>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion app/views/contests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
<div>
<h3 class="contest-header-title">Contest #<%= @contest.id %></h3>
<p class="mb-0 contest-header-description">Get a chance to get featured on our website and show off your circuit to <%= @user_count %> users! </p>
<% if @contest.status == 'Live' %>
<p class="mb-0 contest-header-description">Your Remaining Votes: <%= 3 - current_user.user_contest_votes(@contest.id) %></p>
<% end %>
</div>

<% if @contest.status == 'Live' %>
<div class="d-flex flex-column align-items-center">
<span class="contest-header-timer">Time Left: <span id="timeLeftCounter">...</span></span>
Expand Down Expand Up @@ -40,7 +44,7 @@
<div class="contest-page-line"></div>
<div>
<h5>Contest Entries(<%= @contest.submissions.count %>)</h5>
<span class="contest-header-sub-description">Vote for your favourite entries and help us choose our winners for this week! One user gets 1 votes.</span>
<span class="contest-header-sub-description">Vote for your favourite entries and help us choose our winners for this week! One user gets 3 votes.</span>
</div>
<div id="my-circuits-div" class="row center-row circuit-page">
<% @submissions.each do |submission| %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 43d5f69

Please sign in to comment.