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 00000000..ce29cb0e --- /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 7a89e076..7e4ecd11 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 00000000..4e5df51e --- /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 7789e4d7..676e8c36 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -275,6 +275,7 @@ Rails.application.routes.draw do 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 131e2e20..1c5c4cc2 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 c4031300..15b8311b 100644 --- a/spec/api/v1/swagger_spec.rb +++ b/spec/api/v1/swagger_spec.rb @@ -216,6 +216,18 @@ describe 'API v1', type: :apivore, order: :defined do 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