Merge pull request 'Define and document CRUD endpoints for expenses' (#180) from expenses-crud into main
Reviewed-on: #180
This commit is contained in:
		
						commit
						71eecf94a6
					
				| @ -9,11 +9,21 @@ class ExpensesController < ApplicationController | |||||||
|     render json: Expense.all.order(pricing_type: :asc, amount: :desc).as_json(only: %i[id name amount pricing_type]) |     render json: Expense.all.order(pricing_type: :asc, amount: :desc).as_json(only: %i[id name amount pricing_type]) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   def create | ||||||
|  |     Expense.create!(expense_params) | ||||||
|  |     render json: {}, status: :created | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   def update |   def update | ||||||
|     Expense.find(params[:id]).update!(expense_params) |     Expense.find(params[:id]).update!(expense_params) | ||||||
|     render json: {}, status: :ok |     render json: {}, status: :ok | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   def destroy | ||||||
|  |     Expense.find(params[:id]).destroy! | ||||||
|  |     render json: {}, status: :ok | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   private |   private | ||||||
| 
 | 
 | ||||||
|   def expense_params |   def expense_params | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ Rails.application.routes.draw do | |||||||
|     resources :guests, only: %i[index create update destroy] do |     resources :guests, only: %i[index create update destroy] do | ||||||
|       post :bulk_update, on: :collection |       post :bulk_update, on: :collection | ||||||
|     end |     end | ||||||
|     resources :expenses, only: %i[index update] do |     resources :expenses, only: %i[index create update destroy] do | ||||||
|       get :summary, on: :collection |       get :summary, on: :collection | ||||||
|     end |     end | ||||||
|     resources :tables_arrangements, only: %i[index show] |     resources :tables_arrangements, only: %i[index show] | ||||||
|  | |||||||
| @ -16,9 +16,7 @@ RSpec.describe 'expenses', type: :request do | |||||||
|                  required: %i[id name amount pricing_type], |                  required: %i[id name amount pricing_type], | ||||||
|                  properties: { |                  properties: { | ||||||
|                    id: { type: :string, format: :uuid }, |                    id: { type: :string, format: :uuid }, | ||||||
|                    name: { type: :string }, |                    **Swagger::Schema::EXPENSE | ||||||
|                    amount: { type: :number }, |  | ||||||
|                    pricing_type: { type: :string, enum: Expense.pricing_types.keys } |  | ||||||
|                  } |                  } | ||||||
|                } |                } | ||||||
| 
 | 
 | ||||||
| @ -26,10 +24,31 @@ RSpec.describe 'expenses', type: :request do | |||||||
|       end |       end | ||||||
|       regular_api_responses |       regular_api_responses | ||||||
|     end |     end | ||||||
|  | 
 | ||||||
|  |     post 'create expense' do | ||||||
|  |       tags 'Expenses' | ||||||
|  |       consumes 'application/json' | ||||||
|  |       produces 'application/json' | ||||||
|  |       parameter Swagger::Schema::SLUG | ||||||
|  |       parameter name: :body, in: :body, schema: { | ||||||
|  |         type: :object, | ||||||
|  |         required: %i[expense], | ||||||
|  |         properties: { | ||||||
|  |           expense: { | ||||||
|  |             type: :object, | ||||||
|  |             required: %i[name amount pricing_type], | ||||||
|  |             properties: Swagger::Schema::EXPENSE | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       response_empty_201 | ||||||
|  |       response_422 | ||||||
|  |       regular_api_responses | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   path '/{slug}/expenses/{id}' do |   path '/{slug}/expenses/{id}' do | ||||||
| 
 |  | ||||||
|     patch('update expense') do |     patch('update expense') do | ||||||
|       tags 'Expenses' |       tags 'Expenses' | ||||||
|       consumes 'application/json' |       consumes 'application/json' | ||||||
| @ -38,11 +57,7 @@ RSpec.describe 'expenses', type: :request do | |||||||
|       parameter name: 'id', in: :path, type: :string, format: :uuid, description: 'id' |       parameter name: 'id', in: :path, type: :string, format: :uuid, description: 'id' | ||||||
|       parameter name: :body, in: :body, schema: { |       parameter name: :body, in: :body, schema: { | ||||||
|         type: :object, |         type: :object, | ||||||
|         properties: { |         properties: Swagger::Schema::EXPENSE | ||||||
|           name: { type: :string }, |  | ||||||
|           amount: { type: :number, minimum: 0 }, |  | ||||||
|           pricing_type: { type: :string, enum: Expense.pricing_types.keys } |  | ||||||
|         } |  | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       response_empty_200 |       response_empty_200 | ||||||
| @ -50,5 +65,15 @@ RSpec.describe 'expenses', type: :request do | |||||||
|       response_404 |       response_404 | ||||||
|       regular_api_responses |       regular_api_responses | ||||||
|     end |     end | ||||||
|  | 
 | ||||||
|  |     delete('delete expense') do | ||||||
|  |       tags 'Expenses' | ||||||
|  |       produces 'application/json' | ||||||
|  |       parameter Swagger::Schema::SLUG | ||||||
|  |       parameter Swagger::Schema::ID | ||||||
|  |       response_empty_200 | ||||||
|  |       response_404 | ||||||
|  |       regular_api_responses | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  | |||||||
| @ -23,6 +23,12 @@ module Swagger | |||||||
|       color: { type: :string, pattern: '^#(?:[0-9a-fA-F]{3}){1,2}$' } |       color: { type: :string, pattern: '^#(?:[0-9a-fA-F]{3}){1,2}$' } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     EXPENSE = { | ||||||
|  |       name: { type: :string }, | ||||||
|  |       amount: { type: :number, minimum: 0 }, | ||||||
|  |       pricing_type: { type: :string, enum: Expense.pricing_types.keys } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     SLUG = { |     SLUG = { | ||||||
|       name: 'slug', |       name: 'slug', | ||||||
|       in: :path, |       in: :path, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user