API v1 article_categories endpoint
This commit is contained in:
parent
e1d50e5b9c
commit
69732cca0d
6 changed files with 117 additions and 0 deletions
26
app/controllers/api/v1/article_categories_controller.rb
Normal file
26
app/controllers/api/v1/article_categories_controller.rb
Normal file
|
@ -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
|
|
@ -9,6 +9,12 @@ class ArticleCategory < ApplicationRecord
|
|||
# @!attribute articles
|
||||
# @return [Array<Article>] Articles with this category.
|
||||
has_many :articles
|
||||
# @!attribute order_articles
|
||||
# @return [Array<OrderArticle>] Order articles with this category.
|
||||
has_many :order_articles, through: :articles
|
||||
# @!attribute orders
|
||||
# @return [Array<Order>] 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)
|
||||
|
|
3
app/serializers/article_category_serializer.rb
Normal file
3
app/serializers/article_category_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class ArticleCategorySerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
end
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue