Class: BarcasController

Inherits:
ApplicationController show all
Defined in:
app/controllers/barcas_controller.rb

Overview

API de Barcas

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_pirata

Instance Method Summary collapse

Instance Method Details

#abandonar ⇒ Object

DELETE /barcas/:barca_id/abandonar

La pirata actual abandona la barca!



117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/barcas_controller.rb', line 117

def abandonar
  find_barca

  if @barca.tripulaciones.find_by(pirata: current_pirata).destroy
    notify(subject: :abandonar, urgency: :'very-low', ttl: 1.day)
    render status: :no_content
  else
    render json: { errors: @barca.errors.messages },
           status: :unprocessable_entity
  end
end

#abordar ⇒ Object

PUT /barcas/:barca_id/abordar

La pirata actual aborda la barca



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/barcas_controller.rb', line 100

def abordar
  find_barca

  @barca.piratas << current_pirata

  if @barca.save
    notify(subject: :abordar, urgency: :'very-low', ttl: 1.day)
    render status: :no_content
  else
    render json: { errors: @barca.errors.messages },
           status: :unprocessable_entity
  end
end

#create ⇒ Object

POST /barcas

Crear una barca

Parameters:

  • barca (Hash) —

    { barca: { nombre: @string, descripcion: @text } }



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/barcas_controller.rb', line 44

def create
  @barca = Barca.new(barca_params)
  # La pirata se une a la barca que crea
  @barca.piratas << current_pirata

  if @barca.save
    notify(subject: 'barcas.create.subject')

    render status: :created
  else
    render json: { errors: @barca.errors.messages },
           status: :unprocessable_entity
  end
end

#destroy ⇒ Object

DELETE /barcas/:id

Elimina una barca y su tripulación, solo si no tiene consensos



85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/barcas_controller.rb', line 85

def destroy
  @barca = current_pirata.barcas.find(params[:id])

  if @barca.consensos.count.zero? && @barca.destroy
    notify(subject: :destroy, urgency: :high)
    render status: :no_content
  else
    render json: { errors: @barca.errors.messages },
           status: :unprocessable_entity
  end
end

#index ⇒ Object

GET /barcas

Devuelve todas las barcas abordadas



12
13
14
# File 'app/controllers/barcas_controller.rb', line 12

def index
  @barcas = current_pirata.barcas
end

#show ⇒ Object

GET /barcas/:id

Ver los detalles de una barca

Parameters:

  • :id (Integer) —

    El ID de la barca



30
31
32
33
34
35
# File 'app/controllers/barcas_controller.rb', line 30

def show
  @barca = Barca.find(params[:id])
  return if @barca

  render json: {}, status: :not_found
end

#todas ⇒ Object

GET /barcas/todas

Ver todas las barcas



19
20
21
# File 'app/controllers/barcas_controller.rb', line 19

def todas
  @barcas = Barca.all
end

#update ⇒ Object

PUT /barcas/:id

Actualizar una barca

Parameters:

  • barca (Hash) —

    { barca: { nombre: @string, descripcion: @text } }



66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/barcas_controller.rb', line 66

def update
  # Las piratas solo pueden modificar sus propias barcas
  @barca = current_pirata.barcas.find(params[:id])

  if @barca.update_attributes barca_params
    notify(subject: :update, urgency: :high)
    render status: :no_content
  else
    render json: { errors: @barca.errors.messages },
           status: :unprocessable_entity
  end
end