From 9dec04fe884f375ed7f1ac7d0018052ab79542f5 Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 3 Aug 2019 13:40:02 -0300 Subject: [PATCH] barcas --- app/controllers/barcas_controller.rb | 88 ++++++++++++++++++++++ app/views/barcas/_barca.json.jbuilder | 3 + app/views/barcas/create.json.jbuilder | 3 + app/views/barcas/destroy.json.jbuilder | 3 + app/views/barcas/index.json.jbuilder | 3 + app/views/barcas/show.json.jbuilder | 3 + app/views/barcas/update.json.jbuilder | 3 + test/controllers/barcas_controller_test.rb | 55 ++++++++++++++ 8 files changed, 161 insertions(+) create mode 100644 app/controllers/barcas_controller.rb create mode 100644 app/views/barcas/_barca.json.jbuilder create mode 100644 app/views/barcas/create.json.jbuilder create mode 100644 app/views/barcas/destroy.json.jbuilder create mode 100644 app/views/barcas/index.json.jbuilder create mode 100644 app/views/barcas/show.json.jbuilder create mode 100644 app/views/barcas/update.json.jbuilder create mode 100644 test/controllers/barcas_controller_test.rb diff --git a/app/controllers/barcas_controller.rb b/app/controllers/barcas_controller.rb new file mode 100644 index 0000000..16dd395 --- /dev/null +++ b/app/controllers/barcas_controller.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +# API de Barcas +class BarcasController < ApplicationController + before_action :authenticate! + + # GET /barcas + # + # Devuelve todas las barcas + # + # @return Array [ barcas ] + def index + @barcas = Barca.all + end + + # GET /barcas/:id + # + # Ver los detalles de una barca + # + # @param :id [Integer] El ID de la barca + # @return Hash { id: @int, created_at: @date, updated_at: @date, + # nombre: @string, descripcion: @text } + def show + @barca = Barca.find(params[:id]) + return if @barca + + render json: {}, status: :not_found + end + + # POST /barcas + # + # Crear una barca + # + # @param barca [Hash] { barca: { nombre: @string, descripcion: @text } } + # @return Hash { id: @int, created_at: @date, updated_at: @date, + # nombre: @string, descripcion: @text } + def create + @barca = Barca.new(barca_params) + + if @barca.save + render status: :created + else + render json: { errors: @barca.errors.messages }, + status: :unprocessable_entity + end + end + + # PUT /barcas/:id + # + # Actualizar una barca + # + # @param barca [Hash] { barca: { nombre: @string, descripcion: @text } } + # @return hash { id: @int, created_at: @date, updated_at: @date, + # nombre: @string, descripcion: @text } + def update + @barca = Barca.find(params[:id]) + + if @barca.update_attributes barca_params + render status: :no_content + else + render json: { errors: @barca.errors.messages }, + status: :unprocessable_entity + end + end + + # DELETE /barcas/:id + # + # Elimina una barca y su tripulaciĆ³n + # + # @return hash { id: @int, created_at: @date, updated_at: @date, + # nombre: @string, descripcion: @text } + def destroy + @barca = Barca.find(params[:id]) + + if @barca.destroy + render status: :no_content + else + render json: { errors: @barca.errors.messages }, + status: :unprocessable_entity + end + end + + private + + def barca_params + params.require(:barca).permit(:nombre, :descripcion) + end +end diff --git a/app/views/barcas/_barca.json.jbuilder b/app/views/barcas/_barca.json.jbuilder new file mode 100644 index 0000000..c99f4c1 --- /dev/null +++ b/app/views/barcas/_barca.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.call(barca, :id, :created_at, :nombre, :descripcion) diff --git a/app/views/barcas/create.json.jbuilder b/app/views/barcas/create.json.jbuilder new file mode 100644 index 0000000..28f196f --- /dev/null +++ b/app/views/barcas/create.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @barca, as: :barca diff --git a/app/views/barcas/destroy.json.jbuilder b/app/views/barcas/destroy.json.jbuilder new file mode 100644 index 0000000..28f196f --- /dev/null +++ b/app/views/barcas/destroy.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @barca, as: :barca diff --git a/app/views/barcas/index.json.jbuilder b/app/views/barcas/index.json.jbuilder new file mode 100644 index 0000000..26d5433 --- /dev/null +++ b/app/views/barcas/index.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.barcas @barcas, partial: 'barcas/barca', as: :barca diff --git a/app/views/barcas/show.json.jbuilder b/app/views/barcas/show.json.jbuilder new file mode 100644 index 0000000..28f196f --- /dev/null +++ b/app/views/barcas/show.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @barca, as: :barca diff --git a/app/views/barcas/update.json.jbuilder b/app/views/barcas/update.json.jbuilder new file mode 100644 index 0000000..28f196f --- /dev/null +++ b/app/views/barcas/update.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @barca, as: :barca diff --git a/test/controllers/barcas_controller_test.rb b/test/controllers/barcas_controller_test.rb new file mode 100644 index 0000000..22e8bd7 --- /dev/null +++ b/test/controllers/barcas_controller_test.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +class BarcasControllerTest < ActionDispatch::IntegrationTest + setup do + @pirata = create :pirata + @auth = { Authorization: ActionController::HttpAuthentication::Basic + .encode_credentials(@pirata.email, @pirata.password) } + end + + test 'se pueden ver todas' do + 2.times do + create :barca + end + + get barcas_url, as: :json, headers: @auth + + body = JSON.parse(@response.body) + + assert_equal 200, @response.status + assert_equal 2, body['barcas'].size + end + + test 'se pueden crear' do + nombre = SecureRandom.hex + post barcas_url, as: :json, headers: @auth, + params: { barca: { nombre: nombre, descripcion: nombre } } + + body = JSON.parse(@response.body) + + assert_equal 201, @response.status + assert_equal nombre, body['nombre'] + assert_equal nombre, body['descripcion'] + end + + test 'se pueden editar' do + barca = create :barca + nombre = SecureRandom.hex + + put barca_url(barca), as: :json, headers: @auth, + params: { barca: { id: barca.id, nombre: nombre, descripcion: nombre } } + + assert_equal 204, @response.status + end + + test 'se pueden eliminar' do + barca = create :barca + + delete barca_url(barca), as: :json, headers: @auth + + assert_equal 204, @response.status + assert_raise ActiveRecord::RecordNotFound do + barca.reload + end + end +end