From 1b2c0f0d0ab99a6fce28890bc10393881323448b Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 17 Nov 2024 11:47:12 +0100 Subject: [PATCH] Define an endpoint to create new guests --- app/controllers/guests_controller.rb | 5 +++++ config/routes.rb | 2 +- spec/requests/guests_spec.rb | 24 ++++++++++++++++++++++++ spec/swagger_response_helper.rb | 8 ++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index efc3e58..9a0c9fb 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -10,6 +10,11 @@ class GuestsController < ApplicationController .as_json(only: %i[id name status], include: { group: { only: %i[id name] } }) end + def create + Guest.create!(params.require(:guest).permit(:name, :group_id, :status)) + render json: {}, status: :created + end + def update Guest.find(params[:id]).update!(params.require(:guest).permit(:name)) render json: {}, status: :ok diff --git a/config/routes.rb b/config/routes.rb index 80aa2e0..b90eb65 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Rails.application.routes.draw do mount Rswag::Ui::Engine => '/api-docs' mount Rswag::Api::Engine => '/api-docs' resources :groups, only: :index - resources :guests, only: %i[index update] do + resources :guests, only: %i[index create update] do post :bulk_update, on: :collection end resources :expenses, only: %i[index update] do diff --git a/spec/requests/guests_spec.rb b/spec/requests/guests_spec.rb index 9a92de9..a7f637c 100644 --- a/spec/requests/guests_spec.rb +++ b/spec/requests/guests_spec.rb @@ -52,6 +52,30 @@ RSpec.describe 'guests', type: :request do xit end end + + post('create guest') do + tags 'Guests' + consumes 'application/json' + produces 'application/json' + parameter name: :body, in: :body, schema: { + type: :object, + required: %i[guest], + properties: { + guest: { + type: :object, + required: %i[name group_id status], + properties: { + name: { type: :string }, + group_id: { type: :string, format: :uuid }, + status: { type: :string, enum: Guest.statuses.keys } + } + } + } + } + + response_empty_201 + response_422 + end end path '/guests/{id}' do diff --git a/spec/swagger_response_helper.rb b/spec/swagger_response_helper.rb index 7db38fa..1f84ebd 100644 --- a/spec/swagger_response_helper.rb +++ b/spec/swagger_response_helper.rb @@ -17,6 +17,14 @@ module SwaggerResponseHelper end end + def response_empty_201 + response(201, 'Created') do + produces 'application/json' + schema type: :object + xit + end + end + def response_404 response(404, 'Record not found') do produces 'application/json' -- 2.47.1