From 8de7a8a74cd03736e879a9963401ba25ad76064d Mon Sep 17 00:00:00 2001 From: f Date: Thu, 3 Mar 2022 14:14:22 -0300 Subject: [PATCH] =?UTF-8?q?envolver=20todo=20en=20una=20transacci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/readings_controller.rb | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 796fe1f..cc796c8 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -6,23 +6,25 @@ class ReadingsController < ActionController::API # @see {https://docutopia.tupale.co/sutty:nodemecu:api} def create - reading = raspberry.readings.build reading_params - reading.id = params[:transaction_uuid] - # TODO: Verificar firma - reading.signature = headers[:'X-Signature'] + Reading.transaction do + reading = raspberry.readings.build reading_params + reading.id = params[:transaction_uuid] + # TODO: Verificar firma + reading.signature = headers[:'X-Signature'] - params[:arduinos]&.each do |a| - arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry + params[:arduinos]&.each do |a| + arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry - a[:sensores]&.each do |s| - arduino.sensors.build(sensor_params s) + a[:sensores]&.each do |s| + arduino.sensors.build(sensor_params s) + end end - end - if reading.save - render plain: reading.uuid, status: :ok - else - head :bad_request + if reading.save + render plain: reading.id, status: :ok + else + head :bad_request + end end end