workarea-api-admin 4.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/README.md +82 -0
  4. data/Rakefile +10 -0
  5. data/app/assets/images/workarea/api/admin/icons/api.svg +1 -0
  6. data/app/controllers/workarea/api/admin/application_controller.rb +73 -0
  7. data/app/controllers/workarea/api/admin/categories_controller.rb +305 -0
  8. data/app/controllers/workarea/api/admin/category_product_rules_controller.rb +232 -0
  9. data/app/controllers/workarea/api/admin/content_assets_controller.rb +299 -0
  10. data/app/controllers/workarea/api/admin/content_controller.rb +268 -0
  11. data/app/controllers/workarea/api/admin/discounts_controller.rb +271 -0
  12. data/app/controllers/workarea/api/admin/email_signups_controller.rb +108 -0
  13. data/app/controllers/workarea/api/admin/fulfillments_controller.rb +268 -0
  14. data/app/controllers/workarea/api/admin/inventory_skus_controller.rb +305 -0
  15. data/app/controllers/workarea/api/admin/navigation_menus_controller.rb +267 -0
  16. data/app/controllers/workarea/api/admin/navigation_taxons_controller.rb +305 -0
  17. data/app/controllers/workarea/api/admin/orders_controller.rb +160 -0
  18. data/app/controllers/workarea/api/admin/pages_controller.rb +305 -0
  19. data/app/controllers/workarea/api/admin/payment_profiles_controller.rb +305 -0
  20. data/app/controllers/workarea/api/admin/payment_transactions_controller.rb +108 -0
  21. data/app/controllers/workarea/api/admin/payments_controller.rb +115 -0
  22. data/app/controllers/workarea/api/admin/prices_controller.rb +232 -0
  23. data/app/controllers/workarea/api/admin/pricing_skus_controller.rb +305 -0
  24. data/app/controllers/workarea/api/admin/product_images_controller.rb +232 -0
  25. data/app/controllers/workarea/api/admin/products_controller.rb +305 -0
  26. data/app/controllers/workarea/api/admin/promo_code_lists_controller.rb +305 -0
  27. data/app/controllers/workarea/api/admin/recommendation_settings_controller.rb +122 -0
  28. data/app/controllers/workarea/api/admin/redirects_controller.rb +305 -0
  29. data/app/controllers/workarea/api/admin/releases_controller.rb +304 -0
  30. data/app/controllers/workarea/api/admin/saved_addresses_controller.rb +232 -0
  31. data/app/controllers/workarea/api/admin/saved_credit_cards_controller.rb +256 -0
  32. data/app/controllers/workarea/api/admin/shipping_rates_controller.rb +232 -0
  33. data/app/controllers/workarea/api/admin/shipping_services_controller.rb +299 -0
  34. data/app/controllers/workarea/api/admin/shippings_controller.rb +105 -0
  35. data/app/controllers/workarea/api/admin/swagger_controller.rb +41 -0
  36. data/app/controllers/workarea/api/admin/tax_categories_controller.rb +242 -0
  37. data/app/controllers/workarea/api/admin/tax_rates_controller.rb +292 -0
  38. data/app/controllers/workarea/api/admin/users_controller.rb +296 -0
  39. data/app/controllers/workarea/api/admin/variants_controller.rb +232 -0
  40. data/app/models/workarea/api/admin/bulk_upsert.rb +74 -0
  41. data/app/models/workarea/user.decorator +7 -0
  42. data/app/views/workarea/admin/users/_api_permissions.html.haml +6 -0
  43. data/app/workers/workarea/perform_api_bulk_upsert.rb +15 -0
  44. data/bin/rails +18 -0
  45. data/config/initializers/append_points.rb +4 -0
  46. data/config/initializers/config.rb +15 -0
  47. data/config/locales/en.yml +9 -0
  48. data/config/routes.rb +81 -0
  49. data/lib/workarea/api/admin.rb +21 -0
  50. data/lib/workarea/api/admin/engine.rb +14 -0
  51. data/lib/workarea/api/admin/swagger.rb +106 -0
  52. data/test/documentation/workarea/api/admin/categories_documentation_test.rb +106 -0
  53. data/test/documentation/workarea/api/admin/category_product_rules_documentation_test.rb +78 -0
  54. data/test/documentation/workarea/api/admin/content_assets_documentation_test.rb +94 -0
  55. data/test/documentation/workarea/api/admin/content_documentation_test.rb +87 -0
  56. data/test/documentation/workarea/api/admin/discounts_documentation_test.rb +85 -0
  57. data/test/documentation/workarea/api/admin/email_signups_documentation_test.rb +41 -0
  58. data/test/documentation/workarea/api/admin/fulfillment_documentation_test.rb +84 -0
  59. data/test/documentation/workarea/api/admin/inventory_skus_documentation_test.rb +95 -0
  60. data/test/documentation/workarea/api/admin/navigation_menus_documentation_test.rb +81 -0
  61. data/test/documentation/workarea/api/admin/navigation_taxons_documentation_test.rb +94 -0
  62. data/test/documentation/workarea/api/admin/orders_documentation_test.rb +65 -0
  63. data/test/documentation/workarea/api/admin/pages_documentation_test.rb +93 -0
  64. data/test/documentation/workarea/api/admin/payment_profiles_documentation_test.rb +98 -0
  65. data/test/documentation/workarea/api/admin/payment_transactions_documentation_test.rb +43 -0
  66. data/test/documentation/workarea/api/admin/payments_documentation_test.rb +41 -0
  67. data/test/documentation/workarea/api/admin/prices_documentation_test.rb +82 -0
  68. data/test/documentation/workarea/api/admin/pricing_skus_documentation_test.rb +95 -0
  69. data/test/documentation/workarea/api/admin/product_images_documentation_test.rb +101 -0
  70. data/test/documentation/workarea/api/admin/products_documentation_test.rb +96 -0
  71. data/test/documentation/workarea/api/admin/promo_code_lists_documentation_test.rb +94 -0
  72. data/test/documentation/workarea/api/admin/recommendation_settings_documentation_test.rb +37 -0
  73. data/test/documentation/workarea/api/admin/redirects_documentation_test.rb +96 -0
  74. data/test/documentation/workarea/api/admin/release_changes_documentation_test.rb +32 -0
  75. data/test/documentation/workarea/api/admin/releases_documentation_test.rb +93 -0
  76. data/test/documentation/workarea/api/admin/saved_addresses_documentation_test.rb +92 -0
  77. data/test/documentation/workarea/api/admin/saved_credit_cards_documentation_test.rb +86 -0
  78. data/test/documentation/workarea/api/admin/shipping_rates_documentation_test.rb +78 -0
  79. data/test/documentation/workarea/api/admin/shipping_services_documentation_test.rb +93 -0
  80. data/test/documentation/workarea/api/admin/shippings_documentation_test.rb +41 -0
  81. data/test/documentation/workarea/api/admin/tax_categories_documentation_test.rb +95 -0
  82. data/test/documentation/workarea/api/admin/tax_rates_documentation_test.rb +78 -0
  83. data/test/documentation/workarea/api/admin/users_documentation_test.rb +99 -0
  84. data/test/documentation/workarea/api/admin/variants_documentation_test.rb +80 -0
  85. data/test/dummy/Rakefile +7 -0
  86. data/test/dummy/app/assets/config/manifest.js +4 -0
  87. data/test/dummy/app/assets/images/.keep +0 -0
  88. data/test/dummy/app/assets/javascripts/application.js +13 -0
  89. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  90. data/test/dummy/app/controllers/application_controller.rb +3 -0
  91. data/test/dummy/app/controllers/concerns/.keep +0 -0
  92. data/test/dummy/app/helpers/application_helper.rb +2 -0
  93. data/test/dummy/app/jobs/application_job.rb +2 -0
  94. data/test/dummy/app/mailers/.keep +0 -0
  95. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  96. data/test/dummy/app/models/.keep +0 -0
  97. data/test/dummy/app/models/concerns/.keep +0 -0
  98. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  99. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  100. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  101. data/test/dummy/bin/bundle +3 -0
  102. data/test/dummy/bin/rails +4 -0
  103. data/test/dummy/bin/rake +4 -0
  104. data/test/dummy/bin/setup +34 -0
  105. data/test/dummy/bin/update +29 -0
  106. data/test/dummy/config.ru +4 -0
  107. data/test/dummy/config/application.rb +25 -0
  108. data/test/dummy/config/boot.rb +5 -0
  109. data/test/dummy/config/cable.yml +9 -0
  110. data/test/dummy/config/environment.rb +5 -0
  111. data/test/dummy/config/environments/development.rb +55 -0
  112. data/test/dummy/config/environments/production.rb +86 -0
  113. data/test/dummy/config/environments/test.rb +43 -0
  114. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  115. data/test/dummy/config/initializers/assets.rb +11 -0
  116. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  117. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  118. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  119. data/test/dummy/config/initializers/inflections.rb +16 -0
  120. data/test/dummy/config/initializers/mime_types.rb +4 -0
  121. data/test/dummy/config/initializers/new_framework_defaults.rb +21 -0
  122. data/test/dummy/config/initializers/session_store.rb +3 -0
  123. data/test/dummy/config/initializers/workarea.rb +8 -0
  124. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  125. data/test/dummy/config/locales/en.yml +23 -0
  126. data/test/dummy/config/puma.rb +47 -0
  127. data/test/dummy/config/routes.rb +6 -0
  128. data/test/dummy/config/secrets.yml +22 -0
  129. data/test/dummy/config/spring.rb +6 -0
  130. data/test/dummy/db/seeds.rb +2 -0
  131. data/test/dummy/log/.keep +0 -0
  132. data/test/dummy/public/404.html +67 -0
  133. data/test/dummy/public/422.html +67 -0
  134. data/test/dummy/public/500.html +66 -0
  135. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  136. data/test/dummy/public/apple-touch-icon.png +0 -0
  137. data/test/dummy/public/favicon.ico +1 -0
  138. data/test/integration/workarea/api/admin/audit_log_integration_test.rb +20 -0
  139. data/test/integration/workarea/api/admin/authentication_integration_test.rb +61 -0
  140. data/test/integration/workarea/api/admin/categories_integration_test.rb +64 -0
  141. data/test/integration/workarea/api/admin/category_product_rules_integration_test.rb +58 -0
  142. data/test/integration/workarea/api/admin/content_assets_integration_test.rb +65 -0
  143. data/test/integration/workarea/api/admin/content_integration_test.rb +59 -0
  144. data/test/integration/workarea/api/admin/discounts_integration_test.rb +64 -0
  145. data/test/integration/workarea/api/admin/email_signups_integration_test.rb +32 -0
  146. data/test/integration/workarea/api/admin/error_handling_integration_test.rb +16 -0
  147. data/test/integration/workarea/api/admin/fulfillments_integration_test.rb +62 -0
  148. data/test/integration/workarea/api/admin/inventory_skus_integration_test.rb +70 -0
  149. data/test/integration/workarea/api/admin/navigation_menus_integration_test.rb +54 -0
  150. data/test/integration/workarea/api/admin/navigation_taxons_integration_test.rb +63 -0
  151. data/test/integration/workarea/api/admin/orders_integration_test.rb +53 -0
  152. data/test/integration/workarea/api/admin/pages_integration_test.rb +64 -0
  153. data/test/integration/workarea/api/admin/payment_profiles_integration_test.rb +75 -0
  154. data/test/integration/workarea/api/admin/payment_transactions_integration_test.rb +45 -0
  155. data/test/integration/workarea/api/admin/payments_integration_test.rb +29 -0
  156. data/test/integration/workarea/api/admin/prices_integration_test.rb +55 -0
  157. data/test/integration/workarea/api/admin/pricing_skus_integration_test.rb +72 -0
  158. data/test/integration/workarea/api/admin/product_images_integration_test.rb +56 -0
  159. data/test/integration/workarea/api/admin/products_integration_test.rb +66 -0
  160. data/test/integration/workarea/api/admin/promo_code_lists_integration_test.rb +70 -0
  161. data/test/integration/workarea/api/admin/recommendation_settings_integration_test.rb +41 -0
  162. data/test/integration/workarea/api/admin/redirects_integration_test.rb +69 -0
  163. data/test/integration/workarea/api/admin/release_changes_integration_test.rb +32 -0
  164. data/test/integration/workarea/api/admin/releases_integration_test.rb +60 -0
  165. data/test/integration/workarea/api/admin/saved_addresses_integration_test.rb +70 -0
  166. data/test/integration/workarea/api/admin/saved_credit_cards_integration_test.rb +76 -0
  167. data/test/integration/workarea/api/admin/shipping_rates_integration_test.rb +54 -0
  168. data/test/integration/workarea/api/admin/shipping_services_integration_test.rb +64 -0
  169. data/test/integration/workarea/api/admin/shippings_integration_test.rb +34 -0
  170. data/test/integration/workarea/api/admin/tax_categories_integration_test.rb +64 -0
  171. data/test/integration/workarea/api/admin/tax_rates_integration_test.rb +66 -0
  172. data/test/integration/workarea/api/admin/users_integration_test.rb +67 -0
  173. data/test/integration/workarea/api/admin/variants_integration_test.rb +59 -0
  174. data/test/models/workarea/api/admin/bulk_upsert_test.rb +79 -0
  175. data/test/test_helper.rb +10 -0
  176. data/test/vcr_cassettes/product_image_from_url.yml +43 -0
  177. data/workarea-api-admin.gemspec +18 -0
  178. metadata +252 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0fa92eaf4f9148dae8ffba798b4acd8af62d8bfffab4e7da7ea981ad47719334
4
+ data.tar.gz: 4240b7bb56d687a50a7849e865243aff1141b84405c8c1457f50fab3460bf38f
5
+ SHA512:
6
+ metadata.gz: ce2bf523e985e185a72754db8620242dacd236e5f3b18d90bf982bbed7b408a804ca45a567034f1e654a6d21d97d13974848ebca41794cddabac3da58ba82ce9
7
+ data.tar.gz: 1543e6e42c32a73e3f96f2996eaaa4d74e4800b959302057f8a4cf25b04001413da98cc67ac22b01f8180cf00aa3818b088447d9a20b1c32cfa6e5513adafb8d
@@ -0,0 +1,12 @@
1
+ .bundle/
2
+ Gemfile.lock
3
+ .sass-cache/
4
+ log/*.log
5
+ pkg/
6
+ test/dummy/public/system/dragonfly
7
+ test/dummy/db/*.sqlite3
8
+ test/dummy/log/*.log
9
+ test/dummy/tmp/
10
+ test/dummy/.sass-cache
11
+ **/.DS_Store
12
+ docs/build
@@ -0,0 +1,82 @@
1
+ # Workarea Admin API
2
+
3
+ Part of the [Workarea API][], the Admin API is used for programmatic
4
+ access to your Workarea application's data model. It's designed to be
5
+ "auto-extending", so that when you add or change fields in your Workarea
6
+ models, those fields will be available for use in the API. This part of
7
+ the Workarea API is primarily used for external services integration,
8
+ such as with order management systems, enterprise resource planners, or
9
+ email service providers, and provides detailed order and shipping information
10
+ to such systems.
11
+
12
+ ## Overview
13
+
14
+ - [Swagger][] integration with an implementation of the API in [OAS 2.0][OpenAPI]
15
+ - Basic CRUD actions for core Workarea models
16
+ - Bulk "Upsert" functionality for importing multiple records in the same
17
+ HTTP `PATCH` request.
18
+
19
+ ## Getting Started
20
+
21
+ To use this plugin, install the **workarea-api** gem.
22
+
23
+ For more information on the API as a whole, and to learn how to set up
24
+ the API plugin, consult the [main README][Workarea API].
25
+
26
+ ## Authentication
27
+
28
+ The Admin API secures itself using basic HTTP authentication. It authenticates
29
+ with the same email address and password used to login to the admin. You will
30
+ also need the API access permissions enabled for your account. This can be done
31
+ in the permissions tab of the user edit page in the admin. You can also do this
32
+ in the Rails console by enabling the `api_access` field on the user:
33
+
34
+ To make requests to the Admin API, ensure your user has API access
35
+ permissions. This can be changed in the "Permissions" tab of the "Edit
36
+ User" page in the admin, or within the `rails console` using the
37
+ following code:
38
+
39
+ ```ruby
40
+ Workarea::User.find_by_email('user@workarea.com').update!(api_access: true)
41
+ ```
42
+
43
+ (**NOTE:** Super-admins will automatically have this access)
44
+
45
+ The Admin API uses [HTTP Basic Authentication][] to authorize client
46
+ requests, with the format `email:password`. You can provide these
47
+ details in an `Authorization` header, encoded in Base64...
48
+
49
+ ```http
50
+ Authorization: Basic eW91cnVzZXJAZXhhbXBsZS5jb206UGFzc3dvcmQhMQ==\n
51
+ ```
52
+
53
+ Or, in the URL, with requests made like:
54
+
55
+ https://youruser@example.com:Password!1@yourtotallyamazingstore.com/api/orders.json
56
+
57
+ Unlike the Storefront API, all Admin API endpoints are protected behind
58
+ authentication.
59
+
60
+ ## Swagger
61
+
62
+ Workarea's Admin API implements the [OpenAPI][] Specification for
63
+ integration with [Swagger][] tools, IPaaS solutions like [Cenit][] or
64
+ [Boomi][], and anything else that speaks OpenAPI. This can be accessed
65
+ at your API's `/api/admin/swagger.json` endpoint, for example
66
+ https://youruser@example.com:Password!1@yourtotallyamazingstore.com/api/swagger.json.
67
+ Point your Swagger client to this endpoint to be able to use and browse
68
+ the Swagger documentation for our API.
69
+
70
+ ## Copyright & Licensing
71
+
72
+ Copyright Workarea 2017-2019. All rights reserved.
73
+
74
+ For licensing, contact [sales@workarea.com][].
75
+
76
+ [Workarea API]: https://homebase.weblinc.com/plugins/workarea-api-admin
77
+ [sales@workarea.com]: mailto:sales@workarea.com
78
+ [HTTP Basic Authentication]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme
79
+ [OpenAPI]: https://swagger.io/resources/open-api/
80
+ [Swagger]: https://swagger.io
81
+ [Cenit]: https://cenit.io
82
+ [Boomi]: https://boomi.com
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ # Load the dummy app's rake tasks
9
+ APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
10
+ load 'rails/tasks/engine.rake'
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><path d="M47.6 20.1c-7.5 1.2-13.2 7.6-13.2 15.4V41c0 1.4 1.1 2.5 2.5 2.5h26.4c1.4 0 2.5-1.1 2.5-2.5v-5.4c0-7.8-5.7-14.3-13.2-15.4V9.8c0-1.4-1.1-2.5-2.5-2.5s-2.5 1.1-2.5 2.5v10.3zM45.9 50.8c0-1.4-1.1-2.5-2.5-2.5s-2.5 1.1-2.5 2.5v5.8h-4.1c-1.4 0-2.5 1.1-2.5 2.5v5.4c0 7.8 5.7 14.3 13.2 15.4v10.4c0 1.4 1.1 2.5 2.5 2.5s2.5-1.1 2.5-2.5V79.9c7.5-1.2 13.2-7.6 13.2-15.4V59c0-1.4-1.1-2.5-2.5-2.5h-4.1v-5.8c0-1.4-1.1-2.5-2.5-2.5s-2.5 1.1-2.5 2.5v5.8h-8.2v-5.7z"/></svg>
@@ -0,0 +1,73 @@
1
+ module Workarea
2
+ module Api
3
+ module Admin
4
+ class ApplicationController < Workarea::ApplicationController
5
+ include CurrentRelease
6
+ include ::Swagger::Blocks
7
+
8
+ respond_to :json
9
+
10
+ before_action :current_user # to force HTTP basic auth
11
+ before_action :set_json_format
12
+ before_action { params.permit! }
13
+ around_action :audit_log
14
+
15
+ rescue_from Mongoid::Errors::DocumentNotFound, with: :handle_not_found
16
+ rescue_from Mongoid::Errors::Validations, with: :handle_invalid
17
+ rescue_from Mongoid::Errors::UnknownAttribute, with: :handle_unknown_attribute
18
+
19
+ def current_user
20
+ return @current_user if defined?(@current_user)
21
+
22
+ if user = authenticate_with_http_basic { |u, p| Api::Admin.authenticate(u, p) }
23
+ @current_user = user
24
+ else
25
+ request_http_basic_authentication
26
+ end
27
+ end
28
+
29
+ def sort_field
30
+ params[:sort_by].presence || :created_at
31
+ end
32
+
33
+ def sort_direction
34
+ if params[:sort_direction].in?(%w(asc desc))
35
+ params[:sort_direction]
36
+ else
37
+ :desc
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def current_release_id
44
+ params[:release_id]
45
+ end
46
+
47
+ def set_json_format
48
+ request.format = :json
49
+ end
50
+
51
+ def handle_not_found(e)
52
+ payload = { params: e.params, problem: e.problem }
53
+ render json: payload, status: :not_found
54
+ end
55
+
56
+ def handle_invalid(e)
57
+ payload = { problem: e.summary, document: e.document }
58
+ render json: payload, status: :unprocessable_entity
59
+ end
60
+
61
+ def handle_unknown_attribute(e)
62
+ render json: e.as_json.slice('problem'), status: :unprocessable_entity
63
+ end
64
+
65
+ def audit_log
66
+ Mongoid::AuditLog.record(current_user) do
67
+ yield
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,305 @@
1
+ module Workarea
2
+ module Api
3
+ module Admin
4
+ class CategoriesController < Admin::ApplicationController
5
+ before_action :find_category, except: [:index, :create, :bulk]
6
+
7
+ swagger_path '/categories' do
8
+ operation :get do
9
+ key :summary, 'All Categories'
10
+ key :description, 'Returns all categories from the system'
11
+ key :operationId, 'listCategories'
12
+ key :produces, ['application/json']
13
+
14
+ parameter do
15
+ key :name, :page
16
+ key :in, :query
17
+ key :description, 'Current page'
18
+ key :required, false
19
+ key :type, :integer
20
+ key :default, 1
21
+ end
22
+ parameter do
23
+ key :name, :sort_by
24
+ key :in, :query
25
+ key :description, 'Field on which to sort (see responses for possible values)'
26
+ key :required, false
27
+ key :type, :string
28
+ key :default, 'created_at'
29
+ end
30
+ parameter do
31
+ key :name, :sort_direction
32
+ key :in, :query
33
+ key :description, 'Direction for sort by'
34
+ key :type, :string
35
+ key :enum, %w(asc desc)
36
+ key :default, 'desc'
37
+ end
38
+
39
+ response 200 do
40
+ key :description, 'Categories'
41
+ schema do
42
+ key :type, :object
43
+ property :categories do
44
+ key :type, :array
45
+ items do
46
+ key :'$ref', 'Workarea::Catalog::Category'
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ operation :post do
54
+ key :summary, 'Create Category'
55
+ key :description, 'Creates a new category.'
56
+ key :operationId, 'addCategory'
57
+ key :produces, ['application/json']
58
+
59
+ parameter do
60
+ key :name, :body
61
+ key :in, :body
62
+ key :description, 'Category to add'
63
+ key :required, true
64
+ schema do
65
+ key :type, :object
66
+ property :category do
67
+ key :'$ref', 'Workarea::Catalog::Category'
68
+ end
69
+ end
70
+ end
71
+
72
+ response 201 do
73
+ key :description, 'Category created'
74
+ schema do
75
+ key :type, :object
76
+ property :category do
77
+ key :'$ref', 'Workarea::Catalog::Category'
78
+ end
79
+ end
80
+ end
81
+
82
+ response 422 do
83
+ key :description, 'Validation failure'
84
+ schema do
85
+ key :type, :object
86
+ property :problem do
87
+ key :type, :string
88
+ end
89
+ property :document do
90
+ key :'$ref', 'Workarea::Catalog::Category'
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ def index
98
+ @categories = Catalog::Category
99
+ .all
100
+ .order_by(sort_field => sort_direction)
101
+ .page(params[:page])
102
+
103
+ respond_with categories: @categories
104
+ end
105
+
106
+ def create
107
+ @category = Catalog::Category.create!(params[:category])
108
+ respond_with(
109
+ { category: @category },
110
+ { status: :created,
111
+ location: category_path(@category.id) }
112
+ )
113
+ end
114
+
115
+ swagger_path '/categories/{id}' do
116
+ operation :get do
117
+ key :summary, 'Find Category by ID'
118
+ key :description, 'Returns a single category'
119
+ key :operationId, 'showCategory'
120
+
121
+ parameter do
122
+ key :name, :id
123
+ key :in, :path
124
+ key :description, 'ID of category to fetch'
125
+ key :required, true
126
+ key :type, :string
127
+ end
128
+
129
+ response 200 do
130
+ key :description, 'Category details'
131
+ schema do
132
+ key :type, :object
133
+ property :category do
134
+ key :'$ref', 'Workarea::Catalog::Category'
135
+ end
136
+ end
137
+ end
138
+
139
+ response 404 do
140
+ key :description, 'Category not found'
141
+ schema do
142
+ key :type, :object
143
+ property :problem do
144
+ key :type, :string
145
+ end
146
+ property :params do
147
+ key :type, :object
148
+ key :additionalProperties, true
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ operation :patch do
155
+ key :summary, 'Update a Category'
156
+ key :description, 'Updates attributes on a category'
157
+ key :operationId, 'updateCategory'
158
+
159
+ parameter do
160
+ key :name, :id
161
+ key :in, :path
162
+ key :description, 'ID of category to update'
163
+ key :required, true
164
+ key :type, :string
165
+ end
166
+
167
+ parameter do
168
+ key :name, :body
169
+ key :in, :body
170
+ key :required, true
171
+ schema do
172
+ key :type, :object
173
+ property :category do
174
+ key :description, 'New attributes'
175
+ key :'$ref', 'Workarea::Catalog::Category'
176
+ end
177
+ end
178
+ end
179
+
180
+ response 204 do
181
+ key :description, 'Category updated successfully'
182
+ end
183
+
184
+ response 422 do
185
+ key :description, 'Validation failure'
186
+ schema do
187
+ key :type, :object
188
+ property :problem do
189
+ key :type, :string
190
+ end
191
+ property :document do
192
+ key :'$ref', 'Workarea::Catalog::Category'
193
+ end
194
+ end
195
+ end
196
+
197
+ response 404 do
198
+ key :description, 'Category not found'
199
+ schema do
200
+ key :type, :object
201
+ property :problem do
202
+ key :type, :string
203
+ end
204
+ property :params do
205
+ key :type, :object
206
+ key :additionalProperties, true
207
+ end
208
+ end
209
+ end
210
+ end
211
+
212
+ operation :delete do
213
+ key :summary, 'Remove a Category'
214
+ key :description, 'Remove a category'
215
+ key :operationId, 'removeCategory'
216
+
217
+ parameter do
218
+ key :name, :id
219
+ key :in, :path
220
+ key :description, 'ID of category to remove'
221
+ key :required, true
222
+ key :type, :string
223
+ end
224
+
225
+ response 204 do
226
+ key :description, 'Category removed successfully'
227
+ end
228
+
229
+ response 404 do
230
+ key :description, 'Category not found'
231
+ schema do
232
+ key :type, :object
233
+ property :problem do
234
+ key :type, :string
235
+ end
236
+ property :params do
237
+ key :type, :object
238
+ key :additionalProperties, true
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ def show
246
+ respond_with category: @category
247
+ end
248
+
249
+ def update
250
+ @category.update_attributes!(params[:category])
251
+ respond_with category: @category
252
+ end
253
+
254
+ swagger_path '/categories/bulk' do
255
+ operation :patch do
256
+ key :summary, 'Bulk Upsert Categories'
257
+ key :description, 'Creates new categories or updates existing ones in bulk.'
258
+ key :operationId, 'upsertCategories'
259
+ key :produces, ['application/json']
260
+
261
+ parameter do
262
+ key :name, :body
263
+ key :in, :body
264
+ key :description, 'Array of categories to upsert'
265
+ key :required, true
266
+ schema do
267
+ key :type, :object
268
+ property :categories do
269
+ key :type, :array
270
+ items do
271
+ key :'$ref', 'Workarea::Catalog::Category'
272
+ end
273
+ end
274
+ end
275
+ end
276
+
277
+ response 204 do
278
+ key :description, 'Upsert received'
279
+ end
280
+ end
281
+ end
282
+
283
+ def bulk
284
+ @bulk = Api::Admin::BulkUpsert.create!(
285
+ klass: Catalog::Category,
286
+ data: params[:categories].map(&:to_h)
287
+ )
288
+
289
+ head :no_content
290
+ end
291
+
292
+ def destroy
293
+ @category.destroy
294
+ head :no_content
295
+ end
296
+
297
+ private
298
+
299
+ def find_category
300
+ @category = Catalog::Category.find(params[:id])
301
+ end
302
+ end
303
+ end
304
+ end
305
+ end