1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

(api) OpenAPI prices index endpoint

This commit is contained in:
Sylvain 2022-08-30 14:16:49 +02:00
parent 4319ea9bce
commit f21beeaee7
8 changed files with 136 additions and 2 deletions

View File

@ -3,6 +3,7 @@
- OpenAPI spaces endpoints (index/show) - OpenAPI spaces endpoints (index/show)
- OpenAPI plans endpoints (index/show) - OpenAPI plans endpoints (index/show)
- OpenAPI plans categories index endpoint - OpenAPI plans categories index endpoint
- OpenAPI prices index endpoint
- Improved automated test on statistics generation - Improved automated test on statistics generation
- Refactored statistics generation - Refactored statistics generation
- Refactored test helpers - Refactored test helpers

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
# public API controller for resources of type Price
class OpenAPI::V1::PricesController < OpenAPI::V1::BaseController
extend OpenAPI::ApiDoc
include Rails::Pagination
expose_doc
def index
@prices = PriceService.list(params).order(created_at: :desc)
return if params[:page].blank?
@prices = @prices.page(params[:page]).per(per_page)
paginate @prices, per_page: per_page
end
private
def per_page
params[:per_page] || 20
end
end

View File

@ -0,0 +1,63 @@
# frozen_string_literal: true
# openAPI documentation for prices endpoint
class OpenAPI::V1::PricesDoc < OpenAPI::V1::BaseDoc
resource_description do
short 'Prices'
desc 'Prices for all resources'
formats FORMATS
api_version API_VERSION
end
include OpenAPI::V1::Concerns::ParamGroups
doc_for :index do
api :GET, "/#{API_VERSION}/prices", 'Prices index'
description 'Index of prices, with optional pagination. Order by *created_at* descendant.'
param_group :pagination
param :plan_id, [Integer, Array, 'null'], optional: true, desc: 'Scope the request to one or various plans. Provide "null" to ' \
'this parameter to get prices not associated with any plans (prices ' \
'that applies to users without subscriptions).'
param :group_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various groups.'
param :priceable_type, %w[Machine Space], optional: true, desc: 'Scope the request to a specific type of resource.'
param :priceable_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various resources.'
example <<-PRICES
# /open_api/v1/prices?priceable_type=Space&page=1&per_page=3
{
"prices": [
{
"id": 502,
"priceable_id": 1,
"priceable_type": "Space",
"group_id": 4,
"plan_id": 5,
"amount": 1800,
"updated_at": "2021-06-21T09:40:40.467277+01:00",
"created_at": "2021-06-21T09:40:40.467277+01:00",
},
{
"id": 503,
"priceable_id": 1,
"priceable_type": "Space",
"group_id": 2,
"plan_id": 1,
"amount": 1600,
"updated_at": "2021-06-21T09:40:40.470904+01:00",
"created_at": "2021-06-21T09:40:40.470904+01:00",
},
{
"id": 504,
"priceable_id": 1,
"priceable_type": "Space",
"group_id": 3,
"plan_id": 3,
"amount": 2000,
"updated_at": "2021-06-21T09:40:40.470876+01:00",
"created_at": "2021-06-21T09:40:40.470876+01:00",
}
]
}
PRICES
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
json.prices @prices do |price|
json.extract! price, :id, :group_id, :plan_id, :priceable_id, :priceable_type, :amount, :created_at, :updated_at
end

View File

@ -5,7 +5,7 @@ Apipie.configure do |config|
config.api_base_url = '/open_api' config.api_base_url = '/open_api'
config.doc_base_url = '/open_api/doc' config.doc_base_url = '/open_api/doc'
# where is your API defined? # where is your API defined?
config.api_controllers_matcher = "#{Rails.root}/app/controllers/open_api/v1/*.rb" config.api_controllers_matcher = Rails.root.join('app/controllers/open_api/v1/*.rb')
config.validate = false config.validate = false
config.translate = false config.translate = false
config.default_locale = nil config.default_locale = nil
@ -22,7 +22,12 @@ Apipie.configure do |config|
= Json = Json
--- ---
Depending on your client, you may have to set header <tt>Accept: application/json</tt> for every request, Depending on your client, you may have to set header <tt>Accept: application/json</tt> for every request,
otherwise some clients may request *html* by default which will result in error. otherwise some clients may request *html* by default which will result in error.
= Amounts
---
Everywhere in the OpenAPI amounts are reported in cents. For exemple, if you get <tt>{ "amount" : 1000 }</tt>,
from the OpenAPI, this means that the price is 10 (or whatever your currency is).
RDOC RDOC
end end

View File

@ -242,6 +242,7 @@ Rails.application.routes.draw do
scope only: :index do scope only: :index do
resources :plans, only: %i[index show] resources :plans, only: %i[index show]
resources :plan_categories resources :plan_categories
resources :prices
resources :users resources :users
resources :trainings resources :trainings
resources :user_trainings resources :user_trainings

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'test_helper'
module OpenApi; end
class OpenApi::PricesTest < ActionDispatch::IntegrationTest
def setup
@token = OpenAPI::Client.find_by(name: 'minitest').token
end
test 'list all prices' do
get '/open_api/v1/prices', headers: open_api_headers(@token)
assert_response :success
assert_equal Price.count, json_response(response.body)[:prices].length
end
test 'list all prices with pagination' do
get '/open_api/v1/prices?page=1&per_page=5', headers: open_api_headers(@token)
assert_response :success
assert_equal 5, json_response(response.body)[:prices].length
end
test 'list all prices for a specific machine' do
get '/open_api/v1/prices?priceable_type=Machine&priceable_id=1', headers: open_api_headers(@token)
assert_response :success
assert_equal [1], json_response(response.body)[:prices].pluck(:priceable_id).uniq
end
end

View File

@ -12,11 +12,15 @@ class OpenApi::ReservationsTest < ActionDispatch::IntegrationTest
test 'list all reservations' do test 'list all reservations' do
get '/open_api/v1/reservations', headers: open_api_headers(@token) get '/open_api/v1/reservations', headers: open_api_headers(@token)
assert_response :success assert_response :success
assert_equal Reservation.count, json_response(response.body)[:reservations].length
end end
test 'list all reservations with pagination' do test 'list all reservations with pagination' do
get '/open_api/v1/reservations?page=1&per_page=5', headers: open_api_headers(@token) get '/open_api/v1/reservations?page=1&per_page=5', headers: open_api_headers(@token)
assert_response :success assert_response :success
assert json_response(response.body)[:reservations].length <= 5
end end
test 'list all reservations for a user' do test 'list all reservations for a user' do