From a7892e49b13e89d0a42fd37f2919bb836b626b64 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 13 Oct 2018 16:29:36 +0200 Subject: [PATCH] API v1 article_categories endpoint --- .../api/v1/article_categories_controller.rb | 26 ++++++++ app/models/article_category.rb | 14 +++++ .../article_category_serializer.rb | 3 + config/routes.rb | 1 + doc/swagger.v1.yml | 61 +++++++++++++++++++ spec/api/v1/swagger_spec.rb | 12 ++++ 6 files changed, 117 insertions(+) create mode 100644 app/controllers/api/v1/article_categories_controller.rb create mode 100644 app/serializers/article_category_serializer.rb diff --git a/app/controllers/api/v1/article_categories_controller.rb b/app/controllers/api/v1/article_categories_controller.rb new file mode 100644 index 0000000000..ce29cb0e85 --- /dev/null +++ b/app/controllers/api/v1/article_categories_controller.rb @@ -0,0 +1,26 @@ +class Api::V1::ArticleCategoriesController < 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 + ArticleCategory.all + end + +end diff --git a/app/models/article_category.rb b/app/models/article_category.rb index 7a89e0766e..7e4ecd11d5 100644 --- a/app/models/article_category.rb +++ b/app/models/article_category.rb @@ -9,6 +9,12 @@ class ArticleCategory < ApplicationRecord # @!attribute articles # @return [Array
] Articles with this category. has_many :articles + # @!attribute order_articles + # @return [Array] Order articles with this category. + has_many :order_articles, through: :articles + # @!attribute orders + # @return [Array] Orders with articles in this category. + has_many :orders, through: :order_articles normalize_attributes :name, :description @@ -16,6 +22,14 @@ class ArticleCategory < ApplicationRecord before_destroy :check_for_associated_articles + def self.ransackable_attributes(auth_object = nil) + %w(id name) + end + + def self.ransackable_associations(auth_object = nil) + %w(articles order_articles orders) + end + # Find a category that matches a category name; may return nil. # TODO more intelligence like remembering earlier associations (global and/or per-supplier) def self.find_match(category) diff --git a/app/serializers/article_category_serializer.rb b/app/serializers/article_category_serializer.rb new file mode 100644 index 0000000000..4e5df51e3c --- /dev/null +++ b/app/serializers/article_category_serializer.rb @@ -0,0 +1,3 @@ +class ArticleCategorySerializer < ActiveModel::Serializer + attributes :id, :name +end diff --git a/config/routes.rb b/config/routes.rb index 7789e4d735..676e8c36ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -275,6 +275,7 @@ resources :orders, only: [:index, :show] resources :order_articles, only: [:index, :show] resources :group_order_articles + resources :article_categories, only: [:index, :show] end end diff --git a/doc/swagger.v1.yml b/doc/swagger.v1.yml index 131e2e206a..1c5c4cc2c3 100644 --- a/doc/swagger.v1.yml +++ b/doc/swagger.v1.yml @@ -469,6 +469,58 @@ paths: $ref: '#/definitions/Error404' security: - foodsoft_auth: ['orders:read', 'orders:write'] + /article_categories: + get: + summary: article categories + tags: + - 2. Category + parameters: + - $ref: '#/parameters/page' + - $ref: '#/parameters/per_page' + responses: + 200: + description: success + schema: + type: object + properties: + article_categories: + type: array + items: + $ref: '#/definitions/ArticleCategory' + meta: + $ref: '#/definitions/Meta' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + + security: + - foodsoft_auth: ['all'] + /article_categories/{id}: + parameters: + - $ref: '#/parameters/idInUrl' + get: + summary: find article category by id + tags: + - 2. Category + responses: + 200: + description: success + schema: + type: object + properties: + article_category: + $ref: '#/definitions/ArticleCategory' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + 404: + description: not found + schema: + $ref: '#/definitions/Error404' + security: + - foodsoft_auth: ['all'] /financial_transaction_classes: get: @@ -734,6 +786,15 @@ definitions: description: name of the class of the transaction required: ['id', 'name', 'financial_transaction_class_id', 'financial_transaction_class_name'] + ArticleCategory: + type: object + properties: + id: + type: integer + name: + type: string + required: ['id', 'name'] + Order: type: object properties: diff --git a/spec/api/v1/swagger_spec.rb b/spec/api/v1/swagger_spec.rb index c403130055..15b8311b6f 100644 --- a/spec/api/v1/swagger_spec.rb +++ b/spec/api/v1/swagger_spec.rb @@ -216,6 +216,18 @@ def fetch_swagger! it_handles_invalid_token_and_scope(:get, '/order_articles') it_handles_invalid_token_and_scope(:get, '/order_articles/{id}', ->{ api_auth({'id' => order_article.id}) }) end + + context 'article_categories' do + let!(:cat_1) { create :article_category } + let!(:cat_2) { create :article_category } + + it { is_expected.to validate(:get, '/article_categories', 200, api_auth) } + it { is_expected.to validate(:get, '/article_categories/{id}', 200, api_auth({'id' => cat_2.id})) } + it { is_expected.to validate(:get, '/article_categories/{id}', 404, api_auth({'id' => cat_2.id + 1})) } + + it_handles_invalid_token(:get, '/article_categories') + it_handles_invalid_token(:get, '/article_categories/{id}', ->{ api_auth({'id' => cat_1.id }) }) + end end # needs to be last context so it is always run at the end