registrar llaves públicas

This commit is contained in:
f 2022-03-12 16:10:16 -03:00
parent 833307d9e6
commit 07eb3108ff
4 changed files with 41 additions and 1 deletions

View file

@ -39,8 +39,11 @@ class ReadingsController < ActionController::API
head :bad_request
end
# Registra una Raspberry junto con su llave pública
def raspberry
@raspberry ||= Raspberry.find_or_create_by! name: params[:controller_id]
@raspberry ||= Raspberry.find_or_create_by!(name: params[:controller_id]).tap do |r|
r.public_keys.find_or_create_by(content: request.headers[:'X-Public-Key'])
end
end
# Procesa la transacción

8
app/models/public_key.rb Normal file
View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class PublicKey < ApplicationRecord
belongs_to :raspberry
validates_presence_of :content
validates_uniqueness_of :content
end

View file

@ -3,6 +3,7 @@
class Raspberry < ApplicationRecord
has_many :readings
has_many :arduinos
has_many :public_keys
validates_presence_of :name
validates_uniqueness_of :name

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
# Una Raspberry puede tener muchas llaves públicas
class CreatePublicKeys < ActiveRecord::Migration[6.1]
def up
create_table :public_keys, id: :uuid do |t|
t.timestamps
t.uuid :raspberry_id, index: true
t.string :content, null: false, unique: true
end
Raspberry.find_each do |r|
r.public_keys.create content: r.public_key
end
remove_column :raspberries, :public_key
end
def down
add_column :raspberries, :public_key, :string
Raspberry.find_each do |r|
r.update public_key: r.public_keys.first.public_key
end
drop_table :public_keys
end
end