From a182f9093f4cdb202b3407479a2b7904aa52c6b8 Mon Sep 17 00:00:00 2001 From: Sambo Date: Wed, 8 Jan 2025 14:41:03 +0700 Subject: [PATCH] Close #2226 organizer create ticket --- .../api/v2/organizer/tickets_controller.rb | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/controllers/spree/api/v2/organizer/tickets_controller.rb b/app/controllers/spree/api/v2/organizer/tickets_controller.rb index dcbe6ee3e..22ad20f87 100644 --- a/app/controllers/spree/api/v2/organizer/tickets_controller.rb +++ b/app/controllers/spree/api/v2/organizer/tickets_controller.rb @@ -3,6 +3,9 @@ module Api module V2 module Organizer class TicketsController < ::Spree::Api::V2::Organizer::BaseController + before_action :set_store, only: %i[create] + before_action :set_event, only: %i[create] + def index event = Spree::Taxon.find(params[:event_id]) @@ -24,9 +27,56 @@ def show end end + def create + resource = Spree::Product.new(product_params) + resource.stores << @store + + resource.taxons << @event + + if resource.save + render_serialized_payload(201) { serialize_resource(resource) } + else + render_error_payload(resource.errors.full_messages.to_sentence, 400) + end + end + + private + + def set_store + @store = Spree::Store.first + return if @store + + render json: { error: 'Store not found' }, status: :not_found + end + + def set_event + @event = Spree::Taxon.find_by(id: params[:event_id]) + return if @event + + render json: { error: 'Event not found' }, status: :not_found + end + def collection_serializer ::Spree::V2::Organizer::TicketSerializer end + + def resource_serializer + ::Spree::V2::Organizer::TicketSerializer + end + + def product_params + params.require(:product).permit( + :name, + :price, + :compare_at_price, + :available_on, + :kyc, + :description, + :shipping_category_id, + :product_type, + :status + ) + end end end end