las barcas se pueden abordar o abandonar
This commit is contained in:
parent
e8e38da190
commit
92cf2ea616
5 changed files with 68 additions and 5 deletions
|
@ -36,6 +36,8 @@ class BarcasController < ApplicationController
|
||||||
# nombre: @string, descripcion: @text }
|
# nombre: @string, descripcion: @text }
|
||||||
def create
|
def create
|
||||||
@barca = Barca.new(barca_params)
|
@barca = Barca.new(barca_params)
|
||||||
|
# La pirata se une a la barca que crea
|
||||||
|
@barca.piratas << current_pirata
|
||||||
|
|
||||||
if @barca.save
|
if @barca.save
|
||||||
render status: :created
|
render status: :created
|
||||||
|
@ -53,7 +55,8 @@ class BarcasController < ApplicationController
|
||||||
# @return hash { id: @int, created_at: @date, updated_at: @date,
|
# @return hash { id: @int, created_at: @date, updated_at: @date,
|
||||||
# nombre: @string, descripcion: @text }
|
# nombre: @string, descripcion: @text }
|
||||||
def update
|
def update
|
||||||
@barca = Barca.find(params[:id])
|
# Las piratas solo pueden modificar sus propias barcas
|
||||||
|
@barca = current_pirata.barcas.find(params[:id])
|
||||||
|
|
||||||
if @barca.update_attributes barca_params
|
if @barca.update_attributes barca_params
|
||||||
render status: :no_content
|
render status: :no_content
|
||||||
|
@ -70,7 +73,7 @@ class BarcasController < ApplicationController
|
||||||
# @return hash { id: @int, created_at: @date, updated_at: @date,
|
# @return hash { id: @int, created_at: @date, updated_at: @date,
|
||||||
# nombre: @string, descripcion: @text }
|
# nombre: @string, descripcion: @text }
|
||||||
def destroy
|
def destroy
|
||||||
@barca = Barca.find(params[:id])
|
@barca = current_pirata.barcas.find(params[:id])
|
||||||
|
|
||||||
if @barca.destroy
|
if @barca.destroy
|
||||||
render status: :no_content
|
render status: :no_content
|
||||||
|
@ -80,9 +83,43 @@ class BarcasController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# PUT /barcas/:barca_id/abordar
|
||||||
|
#
|
||||||
|
# La pirata actual aborda la barca
|
||||||
|
def abordar
|
||||||
|
find_barca
|
||||||
|
|
||||||
|
@barca.piratas << current_pirata
|
||||||
|
|
||||||
|
if @barca.save
|
||||||
|
render status: :no_content
|
||||||
|
else
|
||||||
|
render json: { errors: @barca.errors.messages },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# DELETE /barcas/:barca_id/abandonar
|
||||||
|
#
|
||||||
|
# La pirata actual abandona la barca!
|
||||||
|
def abandonar
|
||||||
|
find_barca
|
||||||
|
|
||||||
|
if @barca.tripulaciones.find_by(pirata: current_pirata).destroy
|
||||||
|
render status: :no_content
|
||||||
|
else
|
||||||
|
render json: { errors: @barca.errors.messages },
|
||||||
|
status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def barca_params
|
def barca_params
|
||||||
params.require(:barca).permit(:nombre, :descripcion)
|
params.require(:barca).permit(:nombre, :descripcion)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_barca
|
||||||
|
@barca = Barca.find(params[:barca_id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
3
app/views/barcas/abandonar.json.jbuilder
Normal file
3
app/views/barcas/abandonar.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# empty
|
3
app/views/barcas/abordar.json.jbuilder
Normal file
3
app/views/barcas/abordar.json.jbuilder
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# empty
|
|
@ -8,6 +8,8 @@ Rails.application.routes.draw do
|
||||||
resources :webpush_subscriptions, only: %i[create]
|
resources :webpush_subscriptions, only: %i[create]
|
||||||
# Podemos crear barcas y dentro de ellas consensos
|
# Podemos crear barcas y dentro de ellas consensos
|
||||||
resources :barcas, only: %i[index show create update destroy] do
|
resources :barcas, only: %i[index show create update destroy] do
|
||||||
|
put 'abordar', to: 'barcas#abordar'
|
||||||
|
delete 'abandonar', to: 'barcas#abandonar'
|
||||||
# Podemos crear consensos pero no modificarlos
|
# Podemos crear consensos pero no modificarlos
|
||||||
resources :consensos, only: %i[index show create destroy] do
|
resources :consensos, only: %i[index show create destroy] do
|
||||||
# Y solo le podemos agregar posiciones
|
# Y solo le podemos agregar posiciones
|
||||||
|
|
|
@ -9,7 +9,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
|
||||||
test 'se pueden ver todas' do
|
test 'se pueden ver todas' do
|
||||||
2.times do
|
2.times do
|
||||||
create :barca
|
create :barca, piratas: [@pirata]
|
||||||
end
|
end
|
||||||
|
|
||||||
get barcas_url, as: :json, headers: @auth
|
get barcas_url, as: :json, headers: @auth
|
||||||
|
@ -33,7 +33,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'se pueden editar' do
|
test 'se pueden editar' do
|
||||||
barca = create :barca
|
barca = create :barca, piratas: [@pirata]
|
||||||
nombre = SecureRandom.hex
|
nombre = SecureRandom.hex
|
||||||
|
|
||||||
put barca_url(barca), as: :json, headers: @auth,
|
put barca_url(barca), as: :json, headers: @auth,
|
||||||
|
@ -43,7 +43,7 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'se pueden eliminar' do
|
test 'se pueden eliminar' do
|
||||||
barca = create :barca
|
barca = create :barca, piratas: [@pirata]
|
||||||
|
|
||||||
delete barca_url(barca), as: :json, headers: @auth
|
delete barca_url(barca), as: :json, headers: @auth
|
||||||
|
|
||||||
|
@ -52,4 +52,22 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest
|
||||||
barca.reload
|
barca.reload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'se pueden abandonar' do
|
||||||
|
barca = create :barca, piratas: [@pirata]
|
||||||
|
|
||||||
|
delete barca_abandonar_url(barca), as: :json, headers: @auth
|
||||||
|
|
||||||
|
assert_equal 204, @response.status
|
||||||
|
assert_not barca.reload.piratas.include?(@pirata)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'se pueden abordar' do
|
||||||
|
barca = create :barca
|
||||||
|
|
||||||
|
put barca_abordar_url(barca), as: :json, headers: @auth
|
||||||
|
|
||||||
|
assert_equal 204, @response.status
|
||||||
|
assert barca.reload.piratas.include?(@pirata)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue