I added the features for users to use promotion code and for promoters to create on their events. May be rewrite to discount code?
83 lines
2.7 KiB
Ruby
83 lines
2.7 KiB
Ruby
class Promoter::PromotionCodesController < ApplicationController
|
|
before_action :authenticate_user!
|
|
before_action :set_event
|
|
before_action :set_promotion_code, only: [ :edit, :update, :destroy ]
|
|
|
|
# GET /promoter/events/:event_id/promotion_codes
|
|
# Display all promotion codes for a specific event
|
|
def index
|
|
@promotion_codes = @event.promotion_codes.includes(:user)
|
|
end
|
|
|
|
|
|
# GET /promoter/events/:event_id/promotion_codes/new
|
|
# Show form to create a new promotion code
|
|
def new
|
|
@promotion_code = @event.promotion_codes.new
|
|
end
|
|
|
|
# GET /promoter/events/:event_id/promotion_codes/:id/edit
|
|
# Show form to edit an existing promotion code
|
|
def edit
|
|
end
|
|
|
|
# POST /promoter/events/:event_id/promotion_codes
|
|
# Create a new promotion code for the event
|
|
def create
|
|
@promotion_code = @event.promotion_codes.new(promotion_code_params_with_conversion)
|
|
@promotion_code.user = current_user
|
|
|
|
if @promotion_code.save
|
|
redirect_to promoter_event_promotion_codes_path(@event), notice: "Promotion code was successfully created."
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
# PATCH/PUT /promoter/events/:event_id/promotion_codes/:id
|
|
# Update an existing promotion code
|
|
def update
|
|
if @promotion_code.update(promotion_code_params_with_conversion)
|
|
redirect_to promoter_event_promotion_codes_path(@event), notice: "Promotion code was successfully updated."
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
# DELETE /promoter/events/:event_id/promotion_codes/:id
|
|
# Delete a promotion code
|
|
def destroy
|
|
@promotion_code.destroy
|
|
redirect_to promoter_event_promotion_codes_path(@event), notice: "Promotion code was successfully destroyed."
|
|
end
|
|
|
|
private
|
|
|
|
# Find the event based on the URL parameter
|
|
def set_event
|
|
@event = Event.find(params[:event_id])
|
|
end
|
|
|
|
# Find the promotion code based on the URL parameter
|
|
def set_promotion_code
|
|
@promotion_code = @event.promotion_codes.find(params[:id])
|
|
end
|
|
|
|
# Strong parameters for promotion code form (accepts euros for display)
|
|
def promotion_code_params
|
|
params.require(:promotion_code).permit(:code, :discount_amount_euros, :expires_at, :active, :usage_limit)
|
|
end
|
|
|
|
# Convert euros to cents for database storage
|
|
# The form displays euros for user convenience, but the database stores cents
|
|
def promotion_code_params_with_conversion
|
|
params = promotion_code_params
|
|
if params[:discount_amount_euros].present?
|
|
# Convert euros to cents (e.g., 20.50 -> 2050)
|
|
params[:discount_amount_cents] = (params[:discount_amount_euros].to_f * 100).to_i
|
|
params.delete(:discount_amount_euros) # Remove the temporary euro parameter
|
|
end
|
|
params
|
|
end
|
|
end
|