Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow MarcRecords to be looked up via ISBN #242

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/controllers/lookup_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

##
# Controller to lookup MARC records based on standard numbers (e.g. ISBN)
class LookupController < ApplicationController
skip_authorization_check

def index
return {} if index_params[:isbn].blank?

@response = grouped_marc_records
end

def index_params
params.permit(:isbn)
end
helper_method :index_params

private

def grouped_marc_records
MarcRecord.includes(:organization).where(isbn: index_params[:isbn]).group_by(&:organization).select do |org, _|
can? :read, org
end
end
end
9 changes: 8 additions & 1 deletion app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ def extract_marc_record_metadata(file, service)
marc001: record['001']&.value,
file_id: file.id,
upload_id: id,
marc: record
marc: record,
isbn: record_isbn(record)
)

out.checksum ||= Digest::MD5.hexdigest(record.to_xml.to_s)

yield out
end
end

def record_isbn(record)
marc020 = record['020'] || {}

(marc020['a'] || marc020['z'])&.[](/^x?\d+/)
end
end
31 changes: 31 additions & 0 deletions app/views/lookup/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="container">
<div class="d-flex justify-content-between">
<h1>Marc Records</h1>
</div>
<%= link_to "view as JSON", url_for(index_params.merge(format: :json)) %>

<table class="table table-striped organizations">
<thead>
<tr>
<th>marc001</th>
<th>Stream</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<% @response.each do |organization, marc_records| %>
<tr>
<td colspan="3" class="bg-info font-weight-bold text-white"><%= organization.name %></td>
</tr>

<% marc_records.each do |marc_record| %>
<tr>
<td><%= marc_record.marc001 %></td>
<td><%= link_to(marc_record.stream.display_name, organization_stream_path(organization, marc_record.stream)) %></td>
<td><%= link_to('marc21', marc21_organization_marc_record_url(organization, marc_record)) %>, <%= link_to('marcxml', marcxml_organization_marc_record_url(organization, marc_record)) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
11 changes: 11 additions & 0 deletions app/views/lookup/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

json.total @response&.values&.sum(&:count) || 0
json.isbn index_params[:isbn]
json.organizations @response do |organization, records|
json.extract! organization, :id, :name, :slug
json.records records do |record|
json.extract! record, :id, :marc001, :bytecount, :length, :checksum
json.url organization_marc_record_url(record.organization, record)
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get '/documentation/:id', to: 'pages#show', as: :pages
get '/api', to: 'pages#api'

resources :lookup, only: :index

get 'contact_emails/confirm/:token', to: 'contact_emails#confirm', as: :contact_email_confirmation

Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20201120180608_add_isbn_to_marc_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIsbnToMarcRecord < ActiveRecord::Migration[6.0]
def change
add_column :marc_records, :isbn, :string
add_index :marc_records, :isbn
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_11_20_154520) do
ActiveRecord::Schema.define(version: 2020_11_20_180608) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -150,8 +150,10 @@
t.bigint "length"
t.bigint "index"
t.string "checksum"
t.string "isbn"
t.index ["file_id", "marc001"], name: "index_marc_records_on_file_id_and_marc001"
t.index ["file_id"], name: "index_marc_records_on_file_id"
t.index ["isbn"], name: "index_marc_records_on_isbn"
t.index ["upload_id", "marc001"], name: "index_marc_records_on_upload_id_and_marc001"
t.index ["upload_id"], name: "index_marc_records_on_upload_id"
end
Expand Down