Merge pull request #129 from ControlPanel-gg/development

added voucher API, removed a tiny bug regarding discord users not being deleted
This commit is contained in:
AVMG 2021-07-15 22:18:23 +02:00 committed by GitHub
commit c7e6684582
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 2 deletions

View file

@ -176,8 +176,7 @@ class VoucherController extends Controller
return '<span class="badge badge-' . $color . '">' . $voucher->getStatus() . '</span>';
})
->editColumn('uses', function (Voucher $voucher) {
$userCount = $voucher->users()->count();
return "{$userCount} / {$voucher->uses}";
return "{$voucher->used} / {$voucher->uses}";
})
->editColumn('credits', function (Voucher $voucher) {
return number_format($voucher->credits, 2, '.', '');

View file

@ -0,0 +1,134 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Voucher;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\Rule;
class VoucherController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index(Request $request)
{
return Voucher::paginate($request->query('per_page') ?? 50);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$request->validate([
'memo' => 'nullable|string|max:191',
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
]);
return Voucher::create($request->all());
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show(int $id)
{
return Voucher::findOrFail($id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, int $id)
{
$voucher = Voucher::findOrFail($id);
$request->validate([
'memo' => 'nullable|string|max:191',
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
]);
$voucher->update($request->all());
return $voucher;
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy(int $id)
{
$voucher = Voucher::findOrFail($id);
$voucher->delete();
return $voucher;
}
/**
* get linked users
* @param Request $request
* @param Voucher $voucher
* @return LengthAwarePaginator
*/
public function users(Request $request, Voucher $voucher){
$request->validate([
'include' => [
'nullable',
'string',
Rule::in(['discorduser']),
]
]);
if($request->input('include') == 'discorduser'){
return $voucher->users()->with('discordUser')->paginate($request->query('per_page') ?? 50);
}
return $voucher->users()->paginate($request->query('per_page') ?? 50);
}
}

View file

@ -107,6 +107,8 @@ class User extends Authenticatable implements MustVerifyEmail
$user->vouchers()->detach();
$user->discordUser()->delete();
Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
});
}

View file

@ -31,6 +31,23 @@ class Voucher extends Model
'expires_at'
];
protected $appends = ['used' , 'status'];
/**
* @return int
*/
public function getUsedAttribute()
{
return $this->users()->count();
}
/**
* @return string
*/
public function getStatusAttribute(){
return $this->getStatus();
}
/**
*
*/

View file

@ -2,6 +2,7 @@
use App\Http\Controllers\Api\ServerController;
use App\Http\Controllers\Api\UserController;
use App\Http\Controllers\Api\VoucherController;
use Illuminate\Support\Facades\Route;
/*
@ -21,6 +22,9 @@ Route::middleware('api.token')->group(function () {
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
Route::patch('/servers/{server}/unsuspend', [ServerController::class, 'unSuspend']);
Route::resource('servers', ServerController::class)->except(['store', 'create', 'edit', 'update']);
// Route::get('/vouchers/{voucher}/users' , [VoucherController::class , 'users']);
Route::resource('vouchers', VoucherController::class)->except('create' , 'edit');
});