envolver todo en una transacción

This commit is contained in:
f 2022-03-03 14:14:22 -03:00
parent cf4dfa3aad
commit 8de7a8a74c

View file

@ -6,23 +6,25 @@ class ReadingsController < ActionController::API
# @see {https://docutopia.tupale.co/sutty:nodemecu:api} # @see {https://docutopia.tupale.co/sutty:nodemecu:api}
def create def create
reading = raspberry.readings.build reading_params Reading.transaction do
reading.id = params[:transaction_uuid] reading = raspberry.readings.build reading_params
# TODO: Verificar firma reading.id = params[:transaction_uuid]
reading.signature = headers[:'X-Signature'] # TODO: Verificar firma
reading.signature = headers[:'X-Signature']
params[:arduinos]&.each do |a| params[:arduinos]&.each do |a|
arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry
a[:sensores]&.each do |s| a[:sensores]&.each do |s|
arduino.sensors.build(sensor_params s) arduino.sensors.build(sensor_params s)
end
end end
end
if reading.save if reading.save
render plain: reading.uuid, status: :ok render plain: reading.id, status: :ok
else else
head :bad_request head :bad_request
end
end end
end end