summaryrefslogtreecommitdiff
path: root/app/controllers/scores_controller.rb
diff options
context:
space:
mode:
authorMarius Peter <marius.peter@tutanota.com>2024-12-29 15:14:43 +0100
committerMarius Peter <marius.peter@tutanota.com>2024-12-29 15:14:43 +0100
commitbe2a93525069de2dfa3c23b0c23e7a9f7ad4c03d (patch)
treeb5493e9d35d024ce7be072ec2168b4a98ba0e63f /app/controllers/scores_controller.rb
First commit.
Diffstat (limited to 'app/controllers/scores_controller.rb')
-rw-r--r--app/controllers/scores_controller.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb
new file mode 100644
index 0000000..650c4e6
--- /dev/null
+++ b/app/controllers/scores_controller.rb
@@ -0,0 +1,49 @@
+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 ]
+
+ 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}"
+ end
+
+ def edit_all
+ @scores = @tartiflette.scores
+ 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 edit_tartiflette_scores_path(@tartiflette),
+ alert: "Erreur lors de l'enregistrement de vos scores : #{e.message}"
+ end
+
+ private
+
+ def set_tartiflette
+ @tartiflette = Tartiflette.find(params[:tartiflette_id])
+ end
+
+ def scores_params
+ params.require(:scores).permit!.to_h
+ end
+end
Copyright 2019--2025 Marius PETER