diff --git a/app/controllers/spree/api/v2/organizer/base_controller.rb b/app/controllers/spree/api/v2/organizer/base_controller.rb new file mode 100644 index 000000000..bbdf8edb1 --- /dev/null +++ b/app/controllers/spree/api/v2/organizer/base_controller.rb @@ -0,0 +1,13 @@ +module Spree + module Api + module V2 + module Organizer + class BaseController < ::Spree::Api::V2::BaseController + def render_serialized_payload(status = 200) + render json: yield, status: status, content_type: content_type + end + end + end + end + end +end diff --git a/app/controllers/spree/api/v2/organizer/events_controller.rb b/app/controllers/spree/api/v2/organizer/events_controller.rb new file mode 100644 index 000000000..8a881a9b3 --- /dev/null +++ b/app/controllers/spree/api/v2/organizer/events_controller.rb @@ -0,0 +1,33 @@ +module Spree + module Api + module V2 + module Organizer + class EventsController < ::Spree::Api::V2::Organizer::BaseController + def index + collection = Spree::Taxon.where(kind: params[:kind]) + .is_child(ActiveModel::Type::Boolean.new.cast(params[:is_child])) + .is_leaf(ActiveModel::Type::Boolean.new.cast(params[:is_leaf])) + .page(params[:page]) + .per(params[:per_page]) + render_serialized_payload do + serialize_collection(collection) + end + end + + def show + resource = Spree::Taxon.find(params[:id]) + render_serialized_payload { serialize_resource(resource) } + end + + def serialize_resource(resource) + Spree::V2::Organizer::EventSerializer.new(resource).serializable_hash + end + + def serialize_collection(collection) + Spree::V2::Organizer::EventSerializer.new(collection).serializable_hash + end + end + end + end + end +end diff --git a/app/models/spree_cm_commissioner/taxon_decorator.rb b/app/models/spree_cm_commissioner/taxon_decorator.rb index c9eaa3851..04cf87fb4 100644 --- a/app/models/spree_cm_commissioner/taxon_decorator.rb +++ b/app/models/spree_cm_commissioner/taxon_decorator.rb @@ -45,6 +45,9 @@ def self.prepended(base) base.whitelisted_ransackable_attributes |= %w[kind] + base.scope :is_child, -> (value) { value ? where.not(parent_id: nil) : where(parent_id: nil) } + base.scope :is_leaf, -> (value) { value ? where.not(id: Spree::Taxon.joins(:children).select(:id)) : where(id: Spree::Taxon.joins(:children).select(:id)) } + base.enum purchasable_on: { both: 0, web: 1, app: 2 } end diff --git a/app/serializers/spree/v2/organizer/base_serializer.rb b/app/serializers/spree/v2/organizer/base_serializer.rb new file mode 100644 index 000000000..f9b37d82e --- /dev/null +++ b/app/serializers/spree/v2/organizer/base_serializer.rb @@ -0,0 +1,9 @@ +module Spree + module V2 + module Organizer + class BaseSerializer + include JSONAPI::Serializer + end + end + end +end diff --git a/app/serializers/spree/v2/organizer/event_serializer.rb b/app/serializers/spree/v2/organizer/event_serializer.rb new file mode 100644 index 000000000..7786c332c --- /dev/null +++ b/app/serializers/spree/v2/organizer/event_serializer.rb @@ -0,0 +1,33 @@ +module Spree + module V2 + module Organizer + class EventSerializer < BaseSerializer + attributes :name, :subtitle, :from_date, :to_date, :description, :kind + + attribute :is_child, &:child? + + attribute :is_leaf, &:leaf? + + # Parent Association + belongs_to :parent, + record_type: :taxon, + serializer: Spree::V2::Storefront::TaxonSerializer + + # Taxonomy Association + belongs_to :taxonomy, + record_type: :taxonomy, + serializer: Spree::V2::Storefront::TaxonomySerializer + + # Children Association + has_many :children, + record_type: :taxon, + serializer: Spree::V2::Storefront::TaxonSerializer + + has_one :category_icon, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer + has_one :app_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer + has_one :web_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer + has_one :home_banner, serializer: ::SpreeCmCommissioner::V2::Storefront::AssetSerializer + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 6a772414d..8eb88855e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -384,6 +384,11 @@ resources :places end + namespace :organizer do + resources :events + resources :tickets + end + namespace :storefront do resources :waiting_room_sessions, only: :create