From 4319ea9bce69bb6c9e4efa674a10371525993821 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 30 Aug 2022 13:11:08 +0200 Subject: [PATCH] (api) OpenAPI plans categories index endpoint --- CHANGELOG.md | 1 + .../open_api/v1/plan_categories_controller.rb | 11 ++++++ app/doc/open_api/v1/plan_categories_doc.rb | 39 +++++++++++++++++++ app/doc/open_api/v1/plans_doc.rb | 2 +- .../v1/plan_categories/index.json.jbuilder | 5 +++ config/routes.rb | 1 + .../open_api/plan_categories_test.rb | 16 ++++++++ 7 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 app/controllers/open_api/v1/plan_categories_controller.rb create mode 100644 app/doc/open_api/v1/plan_categories_doc.rb create mode 100644 app/views/open_api/v1/plan_categories/index.json.jbuilder create mode 100644 test/integration/open_api/plan_categories_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 19b6d27f1..c04472f1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - OpenAPI spaces endpoints (index/show) - OpenAPI plans endpoints (index/show) +- OpenAPI plans categories index endpoint - Improved automated test on statistics generation - Refactored statistics generation - Refactored test helpers diff --git a/app/controllers/open_api/v1/plan_categories_controller.rb b/app/controllers/open_api/v1/plan_categories_controller.rb new file mode 100644 index 000000000..1bd6ef554 --- /dev/null +++ b/app/controllers/open_api/v1/plan_categories_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# authorized 3rd party softwares can fetch data about plan categories through the OpenAPI +class OpenAPI::V1::PlanCategoriesController < OpenAPI::V1::BaseController + extend OpenAPI::ApiDoc + expose_doc + + def index + @plans_categories = PlanCategory.order(:created_at) + end +end diff --git a/app/doc/open_api/v1/plan_categories_doc.rb b/app/doc/open_api/v1/plan_categories_doc.rb new file mode 100644 index 000000000..8233b346b --- /dev/null +++ b/app/doc/open_api/v1/plan_categories_doc.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# openAPI documentation for plan categories endpoint +class OpenAPI::V1::PlanCategoriesDoc < OpenAPI::V1::BaseDoc + resource_description do + short 'Plans categories' + desc 'Categories of subscription plans' + formats FORMATS + api_version API_VERSION + end + + doc_for :index do + api :GET, "/#{API_VERSION}/plan_categories", 'Plans categories index' + description 'Plans categories index. Order by *created_at* ascendant.' + example <<-PLAN_CATEGORIES + # /open_api/v1/plan_categories + { + "plan_categories": [ + { + "id": 1, + "name": "CRAZY LAB", + "weight": 0, + "description": "Lorem ipsum dolor sit amet", + "updated_at": "2021-12-01 15:15:19.860064000 Z", + "created_at": "2021-12-01 15:19:28.367161000 Z" + }, + { + "id": 2, + "name": "PREMIUM", + "weight": 1, + "description": "

Lorem ipsum dolor sit amet

", + "updated_at": "2021-12-01 15:15:19.860064000 Z", + "created_at": "2021-12-01 15:19:28.367161000 Z" + } + ] + } + PLAN_CATEGORIES + end +end diff --git a/app/doc/open_api/v1/plans_doc.rb b/app/doc/open_api/v1/plans_doc.rb index 0d8d34af5..5f5f14315 100644 --- a/app/doc/open_api/v1/plans_doc.rb +++ b/app/doc/open_api/v1/plans_doc.rb @@ -60,7 +60,7 @@ class OpenAPI::V1::PlansDoc < OpenAPI::V1::BaseDoc "monthly_payment": true, "updated_at": "2020-12-14 14:10:11.056241000 Z", "created_at": "2020-12-14 14:10:11.056241000 Z" - }, + } ] } PLANS diff --git a/app/views/open_api/v1/plan_categories/index.json.jbuilder b/app/views/open_api/v1/plan_categories/index.json.jbuilder new file mode 100644 index 000000000..3ac8c6dc0 --- /dev/null +++ b/app/views/open_api/v1/plan_categories/index.json.jbuilder @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +json.plan_categories @plans_categories do |category| + json.extract! category, :id, :name, :weight, :description, :updated_at, :created_at +end diff --git a/config/routes.rb b/config/routes.rb index 474ac19a3..763080a7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -241,6 +241,7 @@ Rails.application.routes.draw do namespace :v1 do scope only: :index do resources :plans, only: %i[index show] + resources :plan_categories resources :users resources :trainings resources :user_trainings diff --git a/test/integration/open_api/plan_categories_test.rb b/test/integration/open_api/plan_categories_test.rb new file mode 100644 index 000000000..7b7fe3f29 --- /dev/null +++ b/test/integration/open_api/plan_categories_test.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::PlanCategoriesTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all plan categories' do + get '/open_api/v1/plan_categories', headers: open_api_headers(@token) + assert_response :success + end +end