Skip to content

Commit

Permalink
Merge pull request #20 from co-cddo/add-api
Browse files Browse the repository at this point in the history
Add ESDA API
  • Loading branch information
RobNicholsGDS authored Mar 12, 2024
2 parents 9836f84 + 7ea552b commit 42f4274
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 1 deletion.
53 changes: 53 additions & 0 deletions app/controllers/esdas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class EsdasController < ApplicationController
before_action :set_esda, only: %i[show update destroy]

# GET /esdas
# GET /esdas.json
def index
@esdas = Esda.all
end

# GET /esdas/1
# GET /esdas/1.json
def show; end

# POST /esdas
# POST /esdas.json
def create
@esda = Esda.new(esda_params)

if @esda.save
render :show, status: :created, location: @esda
else
render json: @esda.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /esdas/1
# PATCH/PUT /esdas/1.json
def update
if @esda.update(esda_params)
render :show, status: :ok, location: @esda
else
render json: @esda.errors, status: :unprocessable_entity
end
end

# DELETE /esdas/1
# DELETE /esdas/1.json
def destroy
@esda.destroy
end

private

# Use callbacks to share common setup or constraints between actions.
def set_esda
@esda = Esda.find(params[:id])
end

# Only allow a list of trusted parameters through.
def esda_params
params.require(:esda).permit(:metadata)
end
end
14 changes: 14 additions & 0 deletions app/models/esda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Esda < ApplicationRecord
before_validation :json_parse_metadata

validates :metadata, presence: true

def json_parse_metadata
return if metadata.blank?
return if metadata.is_a?(Hash)

self.metadata = JSON.parse(metadata)
rescue JSON::ParserError => e
errors.add :metadata, e.message
end
end
2 changes: 2 additions & 0 deletions app/views/esdas/_esda.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! esda, :id, :metadata, :created_at, :updated_at
json.url esda_url(esda, format: :json)
1 change: 1 addition & 0 deletions app/views/esdas/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @esdas, partial: "esdas/esda", as: :esda
1 change: 1 addition & 0 deletions app/views/esdas/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "esdas/esda", esda: @esda
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :esdas, constraints: ->(req) { req.format == :json }
resources :items
root "home#index"
end
9 changes: 9 additions & 0 deletions db/migrate/20240312152119_create_esdas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateEsdas < ActiveRecord::Migration[7.0]
def change
create_table :esdas, id: :uuid, default: "gen_random_uuid()" do |t|
t.json :metadata

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/factories/esdas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :esda do
metadata do
{ foo: :bar }
end
end
end
5 changes: 5 additions & 0 deletions spec/models/esda_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Esda, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
104 changes: 104 additions & 0 deletions spec/requests/esdas_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require "rails_helper"

RSpec.describe "/esdas", type: :request do
let!(:esda) { create :esda }
let(:valid_attributes) do
{ metadata: esda.metadata.to_json }
end
let(:invalid_attributes) do
{ metadata: "" }
end

let(:valid_headers) do
{}
end

describe "GET /index" do
it "renders a successful response" do
get esdas_url, headers: valid_headers, as: :json
expect(response).to be_successful
end
end

describe "GET /show" do
it "renders a successful response" do
get esda_url(esda), as: :json
expect(response).to be_successful
end
end

describe "POST /create" do
context "with valid parameters" do
it "creates a new Esda" do
expect {
post esdas_url,
params: { esda: valid_attributes }, headers: valid_headers, as: :json
}.to change(Esda, :count).by(1)
end

it "renders a JSON response with the new esda" do
post esdas_url,
params: { esda: valid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:created)
expect(response.content_type).to match(a_string_including("application/json"))
end
end

context "with invalid parameters" do
it "does not create a new Esda" do
expect {
post esdas_url,
params: { esda: invalid_attributes }, as: :json
}.to change(Esda, :count).by(0)
end

it "renders a JSON response with errors for the new esda" do
post esdas_url,
params: { esda: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
end

describe "PATCH /update" do
context "with valid parameters" do
let(:metadata) do
{ this: :that }.as_json
end
let(:new_attributes) do
{ metadata: metadata.to_json }
end
it "updates the requested esda" do
patch esda_url(esda),
params: { esda: new_attributes }, headers: valid_headers, as: :json
esda.reload
expect(esda.metadata).to eq(metadata)
end

it "renders a JSON response with the esda" do
patch esda_url(esda),
params: { esda: new_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:ok)
expect(response.content_type).to match(a_string_including("application/json"))
end
end

context "with invalid parameters" do
it "renders a JSON response with errors for the esda" do
patch esda_url(esda),
params: { esda: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
end

describe "DELETE /destroy" do
it "destroys the requested esda" do
expect {
delete esda_url(esda), headers: valid_headers, as: :json
}.to change(Esda, :count).by(-1)
end
end
end

0 comments on commit 42f4274

Please sign in to comment.