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