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

View File

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

View File

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

View File

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