Skip to content

Commit 7ea552b

Browse files
committed
Add ESDA API
1 parent 9836f84 commit 7ea552b

File tree

11 files changed

+204
-1
lines changed

11 files changed

+204
-1
lines changed

app/controllers/esdas_controller.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class EsdasController < ApplicationController
2+
before_action :set_esda, only: %i[show update destroy]
3+
4+
# GET /esdas
5+
# GET /esdas.json
6+
def index
7+
@esdas = Esda.all
8+
end
9+
10+
# GET /esdas/1
11+
# GET /esdas/1.json
12+
def show; end
13+
14+
# POST /esdas
15+
# POST /esdas.json
16+
def create
17+
@esda = Esda.new(esda_params)
18+
19+
if @esda.save
20+
render :show, status: :created, location: @esda
21+
else
22+
render json: @esda.errors, status: :unprocessable_entity
23+
end
24+
end
25+
26+
# PATCH/PUT /esdas/1
27+
# PATCH/PUT /esdas/1.json
28+
def update
29+
if @esda.update(esda_params)
30+
render :show, status: :ok, location: @esda
31+
else
32+
render json: @esda.errors, status: :unprocessable_entity
33+
end
34+
end
35+
36+
# DELETE /esdas/1
37+
# DELETE /esdas/1.json
38+
def destroy
39+
@esda.destroy
40+
end
41+
42+
private
43+
44+
# Use callbacks to share common setup or constraints between actions.
45+
def set_esda
46+
@esda = Esda.find(params[:id])
47+
end
48+
49+
# Only allow a list of trusted parameters through.
50+
def esda_params
51+
params.require(:esda).permit(:metadata)
52+
end
53+
end

app/models/esda.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Esda < ApplicationRecord
2+
before_validation :json_parse_metadata
3+
4+
validates :metadata, presence: true
5+
6+
def json_parse_metadata
7+
return if metadata.blank?
8+
return if metadata.is_a?(Hash)
9+
10+
self.metadata = JSON.parse(metadata)
11+
rescue JSON::ParserError => e
12+
errors.add :metadata, e.message
13+
end
14+
end

app/views/esdas/_esda.json.jbuilder

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! esda, :id, :metadata, :created_at, :updated_at
2+
json.url esda_url(esda, format: :json)

app/views/esdas/index.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.array! @esdas, partial: "esdas/esda", as: :esda

app/views/esdas/show.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.partial! "esdas/esda", esda: @esda

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :esdas, constraints: ->(req) { req.format == :json }
23
resources :items
34
root "home#index"
45
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateEsdas < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :esdas, id: :uuid, default: "gen_random_uuid()" do |t|
4+
t.json :metadata
5+
6+
t.timestamps
7+
end
8+
end
9+
end

db/schema.rb

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/esdas.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :esda do
3+
metadata do
4+
{ foo: :bar }
5+
end
6+
end
7+
end

spec/models/esda_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Esda, type: :model do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end

spec/requests/esdas_spec.rb

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "/esdas", type: :request do
4+
let!(:esda) { create :esda }
5+
let(:valid_attributes) do
6+
{ metadata: esda.metadata.to_json }
7+
end
8+
let(:invalid_attributes) do
9+
{ metadata: "" }
10+
end
11+
12+
let(:valid_headers) do
13+
{}
14+
end
15+
16+
describe "GET /index" do
17+
it "renders a successful response" do
18+
get esdas_url, headers: valid_headers, as: :json
19+
expect(response).to be_successful
20+
end
21+
end
22+
23+
describe "GET /show" do
24+
it "renders a successful response" do
25+
get esda_url(esda), as: :json
26+
expect(response).to be_successful
27+
end
28+
end
29+
30+
describe "POST /create" do
31+
context "with valid parameters" do
32+
it "creates a new Esda" do
33+
expect {
34+
post esdas_url,
35+
params: { esda: valid_attributes }, headers: valid_headers, as: :json
36+
}.to change(Esda, :count).by(1)
37+
end
38+
39+
it "renders a JSON response with the new esda" do
40+
post esdas_url,
41+
params: { esda: valid_attributes }, headers: valid_headers, as: :json
42+
expect(response).to have_http_status(:created)
43+
expect(response.content_type).to match(a_string_including("application/json"))
44+
end
45+
end
46+
47+
context "with invalid parameters" do
48+
it "does not create a new Esda" do
49+
expect {
50+
post esdas_url,
51+
params: { esda: invalid_attributes }, as: :json
52+
}.to change(Esda, :count).by(0)
53+
end
54+
55+
it "renders a JSON response with errors for the new esda" do
56+
post esdas_url,
57+
params: { esda: invalid_attributes }, headers: valid_headers, as: :json
58+
expect(response).to have_http_status(:unprocessable_entity)
59+
expect(response.content_type).to match(a_string_including("application/json"))
60+
end
61+
end
62+
end
63+
64+
describe "PATCH /update" do
65+
context "with valid parameters" do
66+
let(:metadata) do
67+
{ this: :that }.as_json
68+
end
69+
let(:new_attributes) do
70+
{ metadata: metadata.to_json }
71+
end
72+
it "updates the requested esda" do
73+
patch esda_url(esda),
74+
params: { esda: new_attributes }, headers: valid_headers, as: :json
75+
esda.reload
76+
expect(esda.metadata).to eq(metadata)
77+
end
78+
79+
it "renders a JSON response with the esda" do
80+
patch esda_url(esda),
81+
params: { esda: new_attributes }, headers: valid_headers, as: :json
82+
expect(response).to have_http_status(:ok)
83+
expect(response.content_type).to match(a_string_including("application/json"))
84+
end
85+
end
86+
87+
context "with invalid parameters" do
88+
it "renders a JSON response with errors for the esda" do
89+
patch esda_url(esda),
90+
params: { esda: invalid_attributes }, headers: valid_headers, as: :json
91+
expect(response).to have_http_status(:unprocessable_entity)
92+
expect(response.content_type).to match(a_string_including("application/json"))
93+
end
94+
end
95+
end
96+
97+
describe "DELETE /destroy" do
98+
it "destroys the requested esda" do
99+
expect {
100+
delete esda_url(esda), headers: valid_headers, as: :json
101+
}.to change(Esda, :count).by(-1)
102+
end
103+
end
104+
end

0 commit comments

Comments
 (0)