Skip to content

Commit

Permalink
Merge pull request #5148 from sul-dlss/t5147-version_endpoint
Browse files Browse the repository at this point in the history
Adds show endpoint for object versions.
  • Loading branch information
jcoyne authored Jul 31, 2024
2 parents ab812b9 + 00bcad6 commit 118a709
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
15 changes: 12 additions & 3 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ class VersionsController < ApplicationController
before_action :load_version, only: %i[current close_current openable status]

def index
repository_object = RepositoryObject.find_by!(external_identifier: params[:object_id])
render json: { versions: repository_object_version_content(find_repository_object.versions) }
end

# add an entry with version id and description for each RepositoryObjectVersion
def show
repository_object = find_repository_object
repository_object_version = repository_object.versions.find_by!(version: params[:id])

render json: { versions: repository_object_version_content(repository_object.versions) }
render json: repository_object_version.to_cocina_with_metadata
rescue RepositoryObjectVersion::NoCocina => e
render build_error('No content for this version', e, status: :bad_request)
end

def create
Expand Down Expand Up @@ -112,4 +117,8 @@ def repository_object_version_content(repository_object_versions)
}
end
end

def find_repository_object
RepositoryObject.find_by!(external_identifier: params[:object_id])
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

resources :events, only: [:create, :index], defaults: { format: :json }

resources :versions, only: [:create, :index] do
resources :versions, only: %i[create index show] do
collection do
get 'openable'
get 'current'
Expand Down
38 changes: 38 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,44 @@ paths:
schema:
type: string
example: "some_sunetid"
"/v1/objects/{object_id}/versions/{version_id}":
get:
tags:
- versions
summary: Show the cocina for a particular object version
operationId: "versions#show"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/DRO"
"400":
description: The version exists but there is no record of the cocina data.
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
"404":
description: Not found
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
parameters:
- name: object_id
in: path
description: ID of object
required: true
schema:
$ref: "#/components/schemas/Druid"
- name: version_id
in: path
description: ID of the version
required: true
schema:
type: string
"/v1/objects/{object_id}/user_versions":
get:
tags:
Expand Down
32 changes: 32 additions & 0 deletions spec/requests/show_object_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Show single object version' do
let(:druid) { 'druid:mx123qw2323' }

before do
repository_object = create(:repository_object, :with_repository_object_version, :closed, external_identifier: druid)
create(:repository_object_version, repository_object:, version: 2, closed_at: Time.zone.now, cocina_version: nil)
end

context 'when found' do
it 'returns a 200' do
get "/v1/objects/#{druid}/versions/1",
headers: { 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(type: 'https://cocina.sul.stanford.edu/models/book')
expect(response.parsed_body).to include(version: 1)
end
end

context 'when version has no cocina' do
it 'returns a 400' do
get "/v1/objects/#{druid}/versions/2",
headers: { 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:bad_request)
end
end
end

0 comments on commit 118a709

Please sign in to comment.