diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/scores_controller.rb | 37 | ||||
-rw-r--r-- | app/models/score.rb | 5 | ||||
-rw-r--r-- | app/services/tartiflette_scoring_service.rb | 21 |
3 files changed, 35 insertions, 28 deletions
diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 8d494e4..8754b91 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -1,24 +1,12 @@ class ScoresController < ApplicationController allow_unauthenticated_access - before_action :set_tartiflette, only: [ :new, :create, :edit_all, :update_all ] - before_action :scores_params, only: [ :create, :update_all ] + before_action :set_tartiflette, only: [:new, :create, :edit_all, :update_all] def new end def create - if TartifletteScoringService.scored?(@tartiflette, session) - redirect_to root_path, alert: "Vous avez déja noté cette tartiflette." - return - end - - TartifletteScoringService.submit_scores(@tartiflette, scores_params, session) - redirect_to root_path, - notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été enregistrés." - rescue StandardError => e - redirect_to root_path, - status: :unprocessable_entity, - alert: "Erreur lors de l'enregistrement de vos scores : #{e.message}" + handle_scoring(:submit_scores, "enregistrement") end def edit_all @@ -26,15 +14,7 @@ class ScoresController < ApplicationController end def update_all - scores_params.each do |score_id, score_params| - score = @tartiflette.scores.find(score_id) - score.update!(value: score_params[:value]) - end - redirect_to root_path, - notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été mis à jour." - rescue StandardError => e - redirect_to root_path, - alert: "Erreur lors de l'enregistrement de vos scores : #{e.message}" + handle_scoring(:update_scores, "mise à jour") end private @@ -46,4 +26,15 @@ class ScoresController < ApplicationController def scores_params params.require(:scores).permit!.to_h end + + def handle_scoring(service_method, action) + TartifletteScoringService.send(service_method, @tartiflette, scores_params, session) + redirect_to root_path, notice: "Vos scores pour la tartiflette #{@tartiflette.scoring_id} ont été #{action}." + rescue StandardError => e + handle_error("Erreur lors de #{action} de vos scores : #{e.message}") + end + + def handle_error(message) + redirect_to root_path, status: :unprocessable_entity, alert: message + end end diff --git a/app/models/score.rb b/app/models/score.rb index 5147ce0..ccc795f 100644 --- a/app/models/score.rb +++ b/app/models/score.rb @@ -3,4 +3,9 @@ class Score < ApplicationRecord belongs_to :scoring_criterium validates :value, presence: true, inclusion: { in: 1..5 } + validates :session_id, presence: true + validates :tartiflette_id, uniqueness: { + scope: [:scoring_criterium_id, :session_id], + message: "has already been scored for this session and criterium." + } end 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) |