Skip to content

Commit

Permalink
API v1 article_categories endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Jul 29, 2020
1 parent b4fa622 commit 6047366
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 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
14 changes: 14 additions & 0 deletions app/models/article_category.rb
Expand Up @@ -9,13 +9,27 @@ 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

validates :name, :presence => true, :uniqueness => true, :length => { :minimum => 2 }

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)
Expand Down
3 changes: 3 additions & 0 deletions app/serializers/article_category_serializer.rb
@@ -0,0 +1,3 @@
class ArticleCategorySerializer < ActiveModel::Serializer
attributes :id, :name
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -265,6 +265,7 @@
resources :orders, only: [:index, :show]
resources :order_articles, only: [:index, :show]
resources :group_order_articles
resources :article_categories, only: [:index, :show]
end
end

Expand Down
61 changes: 61 additions & 0 deletions doc/swagger.v1.yml
Expand Up @@ -461,6 +461,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']

/config:
get:
Expand Down Expand Up @@ -576,6 +628,15 @@ definitions:
description: when the transaction was entered
required: ['id', 'user_id', 'user_name', 'amount', 'note', 'created_at']

ArticleCategory:
type: object
properties:
id:
type: integer
name:
type: string
required: ['id', 'name']

Order:
type: object
properties:
Expand Down
12 changes: 12 additions & 0 deletions spec/api/v1/swagger_spec.rb
Expand Up @@ -176,6 +176,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
Expand Down

0 comments on commit 6047366

Please sign in to comment.