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}
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