vereto-api/app/controllers/vouchers_controller.rb

69 lines
2 KiB
Ruby
Raw Normal View History

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-08 21:47:01 +02:00
whitelist = `mcrcon -H mine.vereto.net -p #{ENV["MC_RCON_PASS"]} 'whitelist list' | sed 's/There are*.*whitelisted players: //g'`.split(", ")
@vouchers.zip(whitelist)
2019-09-27 19:03:44 +02:00
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 = ""
2019-10-08 21:49:05 +02:00
whitelist = `mcrcon -c -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
2019-10-08 21:49:05 +02:00
output = `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