-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from co-cddo/add-api
Add ESDA API
- Loading branch information
Showing
11 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
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
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 |
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
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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @esdas, partial: "esdas/esda", as: :esda |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "esdas/esda", esda: @esda |
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
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 |
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
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
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
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 |
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
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 |