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
|
|
|
|
|
|
|
|
|
|
render json: @vouchers
|
|
|
|
|
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 = ""
|
|
|
|
|
whitelist = `mcrcon -r -H mine.vereto.net -p #{ENV["MC_RCON_PASS"]} "whitelist list" | sed 's/There are*.*whitelisted players: //g'`.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
|
|
|
|
|
output = `mcrcon -r -H mine.vereto.net -p #{ENV["MC_RCON_PASS"]} "whitelist add #{params[:vouchee]}"`
|
|
|
|
|
render json: output, status: :created, location: @voucher
|
|
|
|
|
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
|
|
|
|
|
|
|
|
render json: "{ Voucher is not valid }" + params[:voucher].in?(whitelist), 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
|