From e5bf7d1527491af4663c203ec17697e8855b650f Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Wed, 1 Jan 2025 17:49:39 +0100 Subject: Cleaned up scores controller and tartiflette scoring service. --- app/services/tartiflette_scoring_service.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'app/services') diff --git a/app/services/tartiflette_scoring_service.rb b/app/services/tartiflette_scoring_service.rb index 3514eb1..2c12025 100644 --- a/app/services/tartiflette_scoring_service.rb +++ b/app/services/tartiflette_scoring_service.rb @@ -1,24 +1,35 @@ class TartifletteScoringService def self.scored?(tartiflette, session) + session[:scored_tartiflettes] ||= [] session[:scored_tartiflettes]&.include?(tartiflette.id) end def self.mark_as_scored(tartiflette, session) session[:scored_tartiflettes] ||= [] - unless scored?(tartiflette, session) - session[:scored_tartiflettes] << tartiflette.id - end + session[:scored_tartiflettes] << tartiflette.id unless scored?(tartiflette, session) end def self.submit_scores(tartiflette, scores, session) - scores.each do |criterium_id, value| + scores.each do |criterium_id, score_params| Score.create!( + session_id: session.id, tartiflette: tartiflette, scoring_criterium_id: criterium_id, - value: value[:value] + value: score_params[:value] ) end mark_as_scored(tartiflette, session) + rescue ActiveRecord::RecordInvalid => e + raise StandardError, "Failed to create score: #{e.message}" + end + + def self.update_scores(tartiflette, scores, session) + scores.each do |score_id, score_params| + score = tartiflette.scores.find(score_id) + score.update!(value: score_params[:value]) + end + rescue ActiveRecord::RecordInvalid => e + raise StandardError, "Failed to update score: #{e.message}" end def self.average_score(tartiflette) -- cgit v1.2.3