From be269101f85aa034085a593e56bd99ac481ecb7c Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Mon, 8 Feb 2021 01:08:52 +0100 Subject: [PATCH] API v1 financial_transaction_types endpoint --- .../financial_transaction_types_controller.rb | 26 ++++++ .../financial_transaction_type_serializer.rb | 17 ++++ config/routes.rb | 1 + doc/swagger.v1.yml | 81 +++++++++++++++++++ spec/api/v1/swagger_spec.rb | 12 +++ 5 files changed, 137 insertions(+) create mode 100644 app/controllers/api/v1/financial_transaction_types_controller.rb create mode 100644 app/serializers/financial_transaction_type_serializer.rb diff --git a/app/controllers/api/v1/financial_transaction_types_controller.rb b/app/controllers/api/v1/financial_transaction_types_controller.rb new file mode 100644 index 00000000..76552375 --- /dev/null +++ b/app/controllers/api/v1/financial_transaction_types_controller.rb @@ -0,0 +1,26 @@ +class Api::V1::FinancialTransactionTypesController < Api::V1::BaseController + include Concerns::CollectionScope + + def index + render json: search_scope + end + + def show + render json: scope.find(params.require(:id)) + end + + private + + def max_per_page + nil + end + + def default_per_page + nil + end + + def scope + FinancialTransactionType.includes(:bank_account, :financial_transaction_class) + end + +end diff --git a/app/serializers/financial_transaction_type_serializer.rb b/app/serializers/financial_transaction_type_serializer.rb new file mode 100644 index 00000000..9990a9b0 --- /dev/null +++ b/app/serializers/financial_transaction_type_serializer.rb @@ -0,0 +1,17 @@ +class FinancialTransactionTypeSerializer < ActiveModel::Serializer + attributes :id, :name, :name_short + attributes :bank_account_id, :bank_account_name, :bank_account_iban + attributes :financial_transaction_class_id, :financial_transaction_class_name + + def financial_transaction_class_name + object.financial_transaction_class.name + end + + def bank_account_name + object.bank_account.try(:name) + end + + def bank_account_iban + object.bank_account.try(:iban) + end +end diff --git a/config/routes.rb b/config/routes.rb index 76310b96..ab475e42 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -266,6 +266,7 @@ Rails.application.routes.draw do end resources :financial_transaction_classes, only: [:index, :show] + resources :financial_transaction_types, only: [:index, :show] resources :financial_transactions, only: [:index, :show] end end diff --git a/doc/swagger.v1.yml b/doc/swagger.v1.yml index ae1b0ec4..4b933d33 100644 --- a/doc/swagger.v1.yml +++ b/doc/swagger.v1.yml @@ -233,6 +233,59 @@ paths: security: - foodsoft_auth: ['all'] + /financial_transaction_types: + get: + summary: financial transaction types + tags: + - 2. Category + parameters: + - $ref: '#/parameters/page' + - $ref: '#/parameters/per_page' + responses: + 200: + description: success + schema: + type: object + properties: + financial_transaction_types: + type: array + items: + $ref: '#/definitions/FinancialTransactionType' + meta: + $ref: '#/definitions/Meta' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + + security: + - foodsoft_auth: ['all'] + /financial_transaction_types/{id}: + parameters: + - $ref: '#/parameters/idInUrl' + get: + summary: find financial transaction type by id + tags: + - 2. Category + responses: + 200: + description: success + schema: + type: object + properties: + financial_transaction_type: + $ref: '#/definitions/FinancialTransactionType' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + 404: + description: not found + schema: + $ref: '#/definitions/Error404' + security: + - foodsoft_auth: ['all'] + /config: get: summary: configuration variables @@ -349,6 +402,34 @@ definitions: description: full name required: ['id', 'name'] + FinancialTransactionType: + type: object + properties: + id: + type: integer + name: + type: string + description: full name + name_short: + type: ['string', 'null'] + description: short name (used for bank transfers) + bank_account_id: + type: ['integer', 'null'] + description: id of the bank account used for this transaction type + bank_account_name: + type: ['string', 'null'] + description: name of the bank account used for this transaction type + bank_account_iban: + type: ['string', 'null'] + description: IBAN of the bank account used for this transaction type + financial_transaction_class_id: + type: integer + description: id of the class of the transaction + financial_transaction_class_name: + type: string + description: name of the class of the transaction + required: ['id', 'name', 'financial_transaction_class_id', 'financial_transaction_class_name'] + Navigation: type: array items: diff --git a/spec/api/v1/swagger_spec.rb b/spec/api/v1/swagger_spec.rb index fc6f2c81..6b9cd918 100644 --- a/spec/api/v1/swagger_spec.rb +++ b/spec/api/v1/swagger_spec.rb @@ -101,6 +101,18 @@ describe 'API v1', type: :apivore, order: :defined do it_handles_invalid_token(:get, '/financial_transaction_classes') it_handles_invalid_token(:get, '/financial_transaction_classes/{id}', ->{ api_auth({'id' => cla_1.id }) }) end + + context 'financial_transaction_types' do + let!(:tpy_1) { create :financial_transaction_type } + let!(:tpy_2) { create :financial_transaction_type } + + it { is_expected.to validate(:get, '/financial_transaction_types', 200, api_auth) } + it { is_expected.to validate(:get, '/financial_transaction_types/{id}', 200, api_auth({'id' => tpy_2.id})) } + it { is_expected.to validate(:get, '/financial_transaction_types/{id}', 404, api_auth({'id' => tpy_2.id + 1})) } + + it_handles_invalid_token(:get, '/financial_transaction_types') + it_handles_invalid_token(:get, '/financial_transaction_types/{id}', ->{ api_auth({'id' => tpy_1.id }) }) + end end # needs to be last context so it is always run at the end