diff --git a/app/controllers/expenses_controller.rb b/app/controllers/expenses_controller.rb index 297fa80..2a125d9 100644 --- a/app/controllers/expenses_controller.rb +++ b/app/controllers/expenses_controller.rb @@ -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]) end + def create + Expense.create!(expense_params) + render json: {}, status: :created + end + def update Expense.find(params[:id]).update!(expense_params) render json: {}, status: :ok end + def destroy + Expense.find(params[:id]).destroy! + render json: {}, status: :ok + end + private def expense_params diff --git a/config/routes.rb b/config/routes.rb index 5e88161..6b88b41 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,7 +27,7 @@ Rails.application.routes.draw do resources :guests, only: %i[index create update destroy] do post :bulk_update, on: :collection end - resources :expenses, only: %i[index update] do + resources :expenses, only: %i[index create update destroy] do get :summary, on: :collection end resources :tables_arrangements, only: %i[index show] diff --git a/spec/requests/expenses_spec.rb b/spec/requests/expenses_spec.rb index 74cf861..068fec5 100644 --- a/spec/requests/expenses_spec.rb +++ b/spec/requests/expenses_spec.rb @@ -16,9 +16,7 @@ RSpec.describe 'expenses', type: :request do required: %i[id name amount pricing_type], properties: { id: { type: :string, format: :uuid }, - name: { type: :string }, - amount: { type: :number }, - pricing_type: { type: :string, enum: Expense.pricing_types.keys } + **Swagger::Schema::EXPENSE } } @@ -26,10 +24,31 @@ RSpec.describe 'expenses', type: :request do end regular_api_responses 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 path '/{slug}/expenses/{id}' do - patch('update expense') do tags 'Expenses' 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: :body, in: :body, schema: { type: :object, - properties: { - name: { type: :string }, - amount: { type: :number, minimum: 0 }, - pricing_type: { type: :string, enum: Expense.pricing_types.keys } - } + properties: Swagger::Schema::EXPENSE } response_empty_200 @@ -50,5 +65,15 @@ RSpec.describe 'expenses', type: :request do response_404 regular_api_responses 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 diff --git a/spec/requests/schemas.rb b/spec/requests/schemas.rb index 30a9b58..eb51216 100644 --- a/spec/requests/schemas.rb +++ b/spec/requests/schemas.rb @@ -23,6 +23,12 @@ module Swagger 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 = { name: 'slug', in: :path,