-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9943 from mkllnk/dfc-provider-test
DFC 1.6 prototype
- Loading branch information
Showing
31 changed files
with
458 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
engines/dfc_provider/app/controllers/dfc_provider/api/base_controller.rb
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
engines/dfc_provider/app/controllers/dfc_provider/api/catalog_items_controller.rb
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
engines/dfc_provider/app/controllers/dfc_provider/api/enterprises_controller.rb
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
engines/dfc_provider/app/controllers/dfc_provider/api/persons_controller.rb
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
engines/dfc_provider/app/controllers/dfc_provider/api/supplied_products_controller.rb
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller used to provide the API products for the DFC application | ||
module DfcProvider | ||
class BaseController < ActionController::Base | ||
protect_from_forgery with: :null_session | ||
|
||
rescue_from ActiveRecord::RecordNotFound, with: :not_found | ||
|
||
before_action :check_authorization | ||
|
||
respond_to :json | ||
|
||
private | ||
|
||
def check_authorization | ||
head :unauthorized if current_user.nil? | ||
end | ||
|
||
def check_enterprise | ||
return if current_enterprise.present? | ||
|
||
not_found | ||
end | ||
|
||
def current_enterprise | ||
@current_enterprise ||= | ||
case params[enterprise_id_param_name] | ||
when 'default' | ||
current_user.enterprises.first! | ||
else | ||
current_user.enterprises.find(params[enterprise_id_param_name]) | ||
end | ||
end | ||
|
||
def enterprise_id_param_name | ||
:enterprise_id | ||
end | ||
|
||
def current_user | ||
@current_user ||= authorization_control.user | ||
end | ||
|
||
def authorization_control | ||
DfcProvider::AuthorizationControl.new(request) | ||
end | ||
|
||
def not_found | ||
head :not_found | ||
end | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
engines/dfc_provider/app/controllers/dfc_provider/catalog_items_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller used to provide the API products for the DFC application | ||
# CatalogItems are items that are being sold by the entreprise. | ||
module DfcProvider | ||
class CatalogItemsController < DfcProvider::BaseController | ||
before_action :check_enterprise | ||
|
||
def index | ||
# CatalogItem is nested into an entreprise which is also nested into | ||
# an user on the DFC specifications, as defined here: | ||
# https://datafoodconsortium.gitbook.io/dfc-standard-documentation | ||
# /technical-specification/api-examples | ||
render json: current_user, serializer: DfcProvider::PersonSerializer | ||
end | ||
|
||
def show | ||
render json: variant, serializer: DfcProvider::CatalogItemSerializer | ||
end | ||
|
||
private | ||
|
||
def variant | ||
@variant ||= | ||
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id]) | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
engines/dfc_provider/app/controllers/dfc_provider/enterprises_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller used to provide the CatalogItem API for the DFC application | ||
module DfcProvider | ||
class EnterprisesController < DfcProvider::BaseController | ||
before_action :check_enterprise | ||
|
||
def show | ||
render json: current_enterprise, serializer: DfcProvider::EnterpriseSerializer | ||
end | ||
|
||
private | ||
|
||
def enterprise_id_param_name | ||
:id | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
engines/dfc_provider/app/controllers/dfc_provider/persons_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller used to provide the Persons API for the DFC application | ||
module DfcProvider | ||
class PersonsController < DfcProvider::BaseController | ||
before_action :check_user_accessibility | ||
|
||
def show | ||
render json: user, serializer: DfcProvider::PersonSerializer | ||
end | ||
|
||
private | ||
|
||
def user | ||
@user ||= Spree::User.find(params[:id]) | ||
end | ||
|
||
def check_user_accessibility | ||
return if current_user == user | ||
|
||
not_found | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
engines/dfc_provider/app/controllers/dfc_provider/supplied_products_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller used to provide the SuppliedProducts API for the DFC application | ||
# SuppliedProducts are products that are managed by an enterprise. | ||
module DfcProvider | ||
class SuppliedProductsController < DfcProvider::BaseController | ||
before_action :check_enterprise | ||
|
||
def show | ||
render json: variant, serializer: DfcProvider::SuppliedProductSerializer | ||
end | ||
|
||
def update | ||
dfc_request = JSON.parse(request.body.read) | ||
return unless dfc_request.key?("dfc-b:description") | ||
|
||
variant.product.update!(name: dfc_request["dfc-b:description"]) | ||
end | ||
|
||
private | ||
|
||
def variant | ||
@variant ||= | ||
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.