2019-09-27 19:03:44 +02:00
class VouchersController < ApplicationController
before_action :set_voucher , only : [ :show , :update , :destroy ]
skip_before_action :authorize_request , only : [ :index , :show , :create ]
# GET /vouchers
def index
@vouchers = Voucher . all
2019-10-21 11:05:59 +02:00
whitelist = %x{ mc-whitelist-users } . gsub ( " \n " , " " )
2019-09-27 19:03:44 +02:00
2019-10-08 21:51:34 +02:00
render json : { :vouchers = > @vouchers , :whitelist = > whitelist } . to_json
2019-09-27 19:03:44 +02:00
end
# GET /vouchers/1
def show
render json : @voucher
end
# POST /vouchers
def create
# Logic for creating a voucher
2019-09-30 17:22:33 +02:00
output = " "
2019-10-21 11:05:59 +02:00
whitelist = %x( mc-whitelist-users ) . gsub ( " \n " , " " ) . split ( " , " )
2019-09-27 19:03:44 +02:00
2019-09-30 17:22:33 +02:00
if Voucher . exists? ( vouchee : params [ :voucher ] ) && params [ :voucher ] . in? ( whitelist )
@voucher = Voucher . new ( voucher_params )
if @voucher . save
2019-10-08 21:57:37 +02:00
output = %x( mcrcon -c -H mine.vereto.net -p #{ ENV [ " MC_RCON_PASS " ] } "whitelist add #{ params [ :vouchee ] } " )
2019-10-08 21:47:01 +02:00
render json : { :message = > output } . to_json , status : :created , location : @voucher
2019-09-30 17:22:33 +02:00
else
render json : @voucher . errors , status : :unprocessable_entity
end
2019-09-27 19:03:44 +02:00
else
2019-09-30 17:22:33 +02:00
2019-10-08 21:47:01 +02:00
render json : { :message = > " Voucher is not valid. " , :whitelisted = > params [ :voucher ] . in? ( whitelist ) , :exists = > Voucher . exists? ( vouchee : params [ :voucher ] ) , :list = > whitelist } . to_json , status : :unprocessable_entity
2019-09-27 19:03:44 +02:00
end
2019-09-30 17:22:33 +02:00
2019-09-27 19:03:44 +02:00
end
# PATCH/PUT /vouchers/1
def update
if @voucher . update ( voucher_params )
render json : @voucher
else
render json : @voucher . errors , status : :unprocessable_entity
end
end
# DELETE /vouchers/1
def destroy
@voucher . destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_voucher
@voucher = Voucher . find ( params [ :id ] )
end
# Only allow a trusted parameter "white list" through.
def voucher_params
params . permit ( :voucher , :vouchee , :accepted )
end
end