-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
594 additions
and
3 deletions.
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,52 @@ | ||
class GameNightsController < AuthenticationController | ||
def create | ||
result = CreateGameNight.call(user: current_user, | ||
game_night_params: game_night_params) | ||
|
||
if result.success? | ||
render json: result.game_night, status: :created | ||
else | ||
render json: result.game_night.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
result = UpdateGameNight.call(id: params[:id], | ||
game_night_params: game_night_params) | ||
|
||
if result.success? | ||
render json: result.game_night, status: :ok | ||
else | ||
render json: result.game_night.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def show | ||
result = ShowGameNight.call(id: params[:id]) | ||
|
||
if result.success? | ||
render json: result.game_night, status: :ok | ||
else | ||
render json: result.errors, status: :not_found | ||
end | ||
end | ||
|
||
def destroy | ||
result = DeleteGameNight.call(id: params[:id]) | ||
|
||
if result.success? | ||
head :no_content | ||
else | ||
render json: result.errors, status: :internal_server_error | ||
end | ||
end | ||
|
||
private | ||
|
||
def game_night_params | ||
params.require(:game_night).permit(:name, | ||
:time, | ||
:location_name, | ||
:location_address) | ||
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,27 @@ | ||
class CreateGameNight < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless valid_input? | ||
end | ||
|
||
def execute | ||
context.game_night = create_game_night | ||
end | ||
|
||
def validate_output | ||
context.fail!(errors: context.game_night.errors) unless game_night_saved? | ||
end | ||
|
||
private | ||
|
||
def game_night_saved? | ||
context.game_night.persisted? | ||
end | ||
|
||
def valid_input? | ||
context.game_night_params && context.user | ||
end | ||
|
||
def create_game_night | ||
context.user.game_nights.create(context.game_night_params) | ||
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,19 @@ | ||
class DeleteGameNight < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless context.id | ||
end | ||
|
||
def execute | ||
context.game_night = GameNight.destroy(context.id) | ||
end | ||
|
||
def validate_output | ||
context.fail!(errors: "game night not deleted") unless game_night_destroyed? | ||
end | ||
|
||
private | ||
|
||
def game_night_destroyed? | ||
context.game_night.destroyed? | ||
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,9 @@ | ||
class ShowGameNight < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless context.id | ||
end | ||
|
||
def execute | ||
context.game_night = GameNight.find(context.id) | ||
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,19 @@ | ||
class UpdateGameNight < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless valid_input? | ||
end | ||
|
||
def execute | ||
context.fail!(errors: "game_night update failed") unless update_game_night | ||
end | ||
|
||
private | ||
|
||
def update_game_night | ||
GameNight.update(context.id, context.game_night_params) | ||
end | ||
|
||
def valid_input? | ||
context.game_night_params && context.id | ||
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
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 @@ | ||
class AddNameToGameNights < ActiveRecord::Migration | ||
def change | ||
add_column :game_nights, :name, :string | ||
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
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,230 @@ | ||
RSpec.describe GameNightsController do | ||
let(:user) { create(:confirmed_user) } | ||
let(:game_night) { create(:game_night) } | ||
|
||
before do | ||
authenticate | ||
end | ||
|
||
describe "POST #create" do | ||
let(:params) do | ||
{ game_night: create_game_night_input.fetch(:game_night_params) } | ||
end | ||
|
||
let(:create_game_night_input) do | ||
{ | ||
user: user, | ||
game_night_params: { | ||
name: game_night.name, | ||
time: game_night.time, | ||
location_name: game_night.location_name, | ||
location_address: game_night.location_address } | ||
} | ||
end | ||
|
||
let(:create_game_night_context) do | ||
Interactor::Context.new(errors: :val, game_night: game_night) | ||
end | ||
|
||
before do | ||
allow(CreateGameNight).to receive(:call).with(create_game_night_input). | ||
and_return(create_game_night_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the CreateGameNight interactor" do | ||
expect(CreateGameNight).to receive(:call) | ||
post :create, params | ||
end | ||
end | ||
|
||
context "when CreateGameNight is a success" do | ||
it "returns HTTP status 201" do | ||
post :create, params | ||
expect(response).to have_http_status(201) | ||
end | ||
|
||
it "renders the game_night as JSON" do | ||
post :create, params | ||
expect(serialize(game_night)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when CreateGameNight is a failure" do | ||
let(:create_game_night_context) do | ||
double(:context, success?: false, game_night: game_night) | ||
end | ||
|
||
it "returns HTTP status 422" do | ||
post :create, params | ||
expect(response).to have_http_status(422) | ||
end | ||
|
||
it "returns an error" do | ||
game_night.errors.add(:name, "error") | ||
post :create, params | ||
expect(json["name"]).to eq(["error"]) | ||
end | ||
end | ||
end | ||
|
||
describe "PATCH #update" do | ||
let(:params) do | ||
{ id: game_night.id, | ||
game_night: update_game_night_input.fetch(:game_night_params) } | ||
end | ||
|
||
let(:update_game_night_input) do | ||
{ | ||
id: game_night.id.to_s, | ||
game_night_params: { | ||
name: game_night.name, | ||
time: game_night.time, | ||
location_name: game_night.location_name, | ||
location_address: game_night.location_address } | ||
} | ||
end | ||
|
||
let(:update_game_night_context) do | ||
Interactor::Context.new(errors: :val, game_night: game_night) | ||
end | ||
|
||
before do | ||
allow(UpdateGameNight).to receive(:call).with(update_game_night_input). | ||
and_return(update_game_night_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the UpdateGameNight Interactor" do | ||
expect(UpdateGameNight).to receive(:call) | ||
patch :update, params | ||
end | ||
end | ||
|
||
context "when UpdateGameNight is a success" do | ||
it "returns HTTP status 200" do | ||
patch :update, params | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "renders the GameNight as JSON" do | ||
patch :update, params | ||
expect(serialize(game_night)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when UpdateGameNight is a failure" do | ||
let(:update_game_night_context) do | ||
double(:context, success?: false, game_night: game_night) | ||
end | ||
|
||
it "renders an error" do | ||
game_night.errors.add(:name, "error") | ||
patch :update, params | ||
expect(json["name"]).to eq(["error"]) | ||
end | ||
|
||
it "returns HTTP status 422" do | ||
patch :update, params | ||
expect(response).to have_http_status(422) | ||
end | ||
end | ||
end | ||
|
||
describe "GET #show" do | ||
let(:params) { { id: game_night.id } } | ||
let(:show_game_night_input) { { id: params.fetch(:id).to_s } } | ||
|
||
let(:show_game_night_context) do | ||
Interactor::Context.new(errors: :val, game_night: game_night) | ||
end | ||
|
||
before do | ||
allow(ShowGameNight).to receive(:call).with(show_game_night_input). | ||
and_return(show_game_night_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the ShowGameNight Interactor" do | ||
expect(ShowGameNight).to receive(:call) | ||
get :show, params | ||
end | ||
end | ||
|
||
context "when ShowGameNight is a success" do | ||
it "returns HTTP status 200" do | ||
get :show, params | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "renders the GameNight as JSON" do | ||
get :show, params | ||
expect(serialize(game_night)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when ShowGameNight is a failure" do | ||
let(:show_game_night_context) do | ||
double(:context, errors: { name: ["invalid"] }, | ||
success?: false, | ||
game_night: game_night) | ||
end | ||
|
||
it "returns HTTP status 404" do | ||
get :show, params | ||
expect(response).to have_http_status(404) | ||
end | ||
|
||
it "renders an error" do | ||
get :show, params | ||
expect(json["name"]).to eq(["invalid"]) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
let(:params) { { id: game_night.id } } | ||
let(:delete_game_night_input) { { id: params.fetch(:id).to_s } } | ||
|
||
let(:delete_game_night_context) do | ||
Interactor::Context.new(errors: :val, game_night: game_night) | ||
end | ||
|
||
before do | ||
allow(DeleteGameNight).to receive(:call).with(delete_game_night_input). | ||
and_return(delete_game_night_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the DeleteGameNight Interactor" do | ||
expect(DeleteGameNight).to receive(:call) | ||
delete :destroy, params | ||
end | ||
end | ||
|
||
context "when DeleteGameNight is a success" do | ||
it "returns HTTP status 204" do | ||
delete :destroy, params | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
|
||
context "when DeleteGameNight is a failure" do | ||
let(:delete_game_night_context) do | ||
double(:context, success?: false, | ||
game_night: game_night, | ||
errors: { id: ["invalid"] }) | ||
end | ||
|
||
it "returns HTTP status 500" do | ||
delete :destroy, params | ||
expect(response).to have_http_status(500) | ||
end | ||
|
||
it "renders an error" do | ||
delete :destroy, params | ||
expect(json["id"]).to eq(["invalid"]) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.