diff --git a/app/controllers/published_databases_controller.rb b/app/controllers/published_databases_controller.rb new file mode 100644 index 0000000..0c8826c --- /dev/null +++ b/app/controllers/published_databases_controller.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Controller for the PublishedDatabase resource. +class PublishedDatabasesController < ApplicationController + def index + add_total_stat(params) + published_databases = PublishedDatabaseResource.all(params) + respond_with(published_databases) + end + + def add_total_stat(params) + # don't return stats + end +end diff --git a/app/resources/published_database_resource.rb b/app/resources/published_database_resource.rb new file mode 100644 index 0000000..3bff371 --- /dev/null +++ b/app/resources/published_database_resource.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# Public resource for the PublishedDatabase object. +class PublishedDatabaseResource < ApplicationResource + primary_endpoint '/published_databases', [:index] + + attribute :url, :string + attribute :updated_at, :datetime + attribute :created_at, :datetime + + def base_scope + latest_id = PublishedDatabase.order(created_at: :desc).select(:id).limit(1) + PublishedDatabase.where(id: latest_id) + end + + # Only return the latest database. + def default_page_size + 1 + end +end diff --git a/config/routes.rb b/config/routes.rb index 55be6a3..02cd692 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ resources :formats, only: %i[index show] resources :illustrators, only: %i[index show] resources :printings, only: %i[index show] + resources :published_databases, only: [:index] resources :restrictions, only: %i[index show] resources :reviews, only: %i[index show] resources :rulings, only: %i[index show] diff --git a/spec/resources/published_database_resource_reads_spec.rb b/spec/resources/published_database_resource_reads_spec.rb new file mode 100644 index 0000000..b92ab31 --- /dev/null +++ b/spec/resources/published_database_resource_reads_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PublishedDatabaseResource, type: :resource do + describe 'serialization' do + fixtures :published_databases + + let(:new_db) { published_databases(:latest_db) } + + it 'works and only returns the latest database' do + render + data = jsonapi_data + + expect(data.length).to eq(1) + + record = data[0] + expect(record.url).to eq(new_db.url) + expect(Time.parse(record.created_at).to_i).to eq(new_db.created_at.to_i) + expect(record.jsonapi_type).to eq('published_databases') + end + end +end