Define and document CRUD endpoints for expenses
All checks were successful
Check usage of free licenses / check-licenses (pull_request) Successful in 56s
Add copyright notice / copyright_notice (pull_request) Successful in 1m33s
Run unit tests / unit_tests (pull_request) Successful in 2m55s

This commit is contained in:
Manuel Bustillo 2024-12-09 19:28:32 +01:00
parent ac09d67f4f
commit be40c97f2f
4 changed files with 51 additions and 10 deletions

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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,