-
-
Save frankyston/6899412732e99b07487075d5dfc3338d to your computer and use it in GitHub Desktop.
Api upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MedicalRecord < ApplicationRecord | |
enum kind: %i[ imaging results notes ] | |
belongs_to :patient, class_name: "User", inverse_of: :medical_records | |
has_one_attached :document, dependent: :detach | |
scope :with_kind, -> (kind) { where kind: kind } | |
after_create_commit :deliver_updation | |
private | |
def deliver_updation | |
Deliveries::MedicalRecordUpdationDelivery.deliver(self, patient) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::MedicalRecordsController < ApplicationProfessionalController | |
include PatientScoped | |
rescue_from ActiveSupport::MessageVerifier::InvalidSignature, with: :not_found | |
def create | |
if has_kind? && has_document? | |
@medical_record = @patient.medical_records.create!(medical_record_params); render("medical_records/show", status: :created) | |
else | |
head :bad_request | |
end | |
end | |
private | |
def medical_record_params | |
params.permit :kind, :document | |
end | |
def has_kind? | |
MedicalRecord.kinds.keys.include?(params[:kind]) | |
end | |
def has_document? | |
params[:document].present? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UploadsController < ApplicationController | |
def create | |
if params[:name] | |
@blob = upload(upload_params); render(json: response_attributes, status: :created) | |
else | |
head :bad_request | |
end | |
end | |
private | |
def upload(attributes) | |
ActiveStorage::Blob.create_and_upload!(**attributes) | |
end | |
def upload_params | |
{ io: request.body, filename: params[:name] } | |
end | |
def response_attributes | |
{ attachable_sgid: @blob.signed_id } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/basecamp/bc3-api/blob/master/sections/uploads.md#create-an-upload