From ed553674b2514130df4bb5ecfa0be83430d93062 Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Sat, 10 Jul 2021 00:11:13 +0200 Subject: [PATCH 01/15] Added voucher system --- .../views/admin/vouchers/create.blade.php | 205 +++++++++++++++ resources/views/admin/vouchers/edit.blade.php | 161 ++++++++++++ .../views/admin/vouchers/index.blade.php | 108 ++++++++ resources/views/admin/vouchers/show.blade.php | 240 ++++++++++++++++++ .../models/redeem_voucher_modal.blade.php | 0 5 files changed, 714 insertions(+) create mode 100644 resources/views/admin/vouchers/create.blade.php create mode 100644 resources/views/admin/vouchers/edit.blade.php create mode 100644 resources/views/admin/vouchers/index.blade.php create mode 100644 resources/views/admin/vouchers/show.blade.php create mode 100644 resources/views/models/redeem_voucher_modal.blade.php diff --git a/resources/views/admin/vouchers/create.blade.php b/resources/views/admin/vouchers/create.blade.php new file mode 100644 index 000000000..2a29bbe89 --- /dev/null +++ b/resources/views/admin/vouchers/create.blade.php @@ -0,0 +1,205 @@ +@extends('layouts.main') + +@section('content') + +
+
+
+
+

Products

+
+
+ +
+
+
+
+ + + +
+
+ +
+
+
+
+
+ @csrf +
+
+ + +
+
+ +
+
+
+ + + @error('name') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('price') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('memory') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('cpu') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('swap') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('description') +
+ {{$message}} +
+ @enderror +
+ +
+
+
+ + + @error('disk') +
+ {{$message}} +
+ @enderror +
+
+ + + @error('io') +
+ {{$message}} +
+ @enderror +
+
+ + + @error('databases') +
+ {{$message}} +
+ @enderror +
+
+ + + @error('backups') +
+ {{$message}} +
+ @enderror +
+
+ + + @error('allocations') +
+ {{$message}} +
+ @enderror +
+
+
+ +
+ +
+
+
+
+
+
+ +
+
+ + + + +@endsection diff --git a/resources/views/admin/vouchers/edit.blade.php b/resources/views/admin/vouchers/edit.blade.php new file mode 100644 index 000000000..33b875a29 --- /dev/null +++ b/resources/views/admin/vouchers/edit.blade.php @@ -0,0 +1,161 @@ +@extends('layouts.main') + +@section('content') + +
+
+
+
+

Vouchers

+
+
+ +
+
+
+
+ + + +
+
+ +
+
+
+
+
+ Voucher details +
+
+
+
+ @csrf + +
+ + + @error('memo') +
+ {{$message}} +
+ @enderror +
+ +
+ + + @error('credits') +
+ {{$message}} +
+ @enderror +
+ + +
+ +
+ +
+ +
+
+ @error('code') +
+ {{$message}} +
+ @enderror +
+ +
+ +
+ +
+ +
+
+ @error('uses') +
+ {{$message}} +
+ @enderror +
+ + +
+ + + @error('expires_at') +
+ {{$message}} +
+ @enderror +
+ + +
+ +
+
+
+
+
+
+ +
+
+ + + + + + +@endsection diff --git a/resources/views/admin/vouchers/index.blade.php b/resources/views/admin/vouchers/index.blade.php new file mode 100644 index 000000000..183e2793b --- /dev/null +++ b/resources/views/admin/vouchers/index.blade.php @@ -0,0 +1,108 @@ +@extends('layouts.main') + +@section('content') + +
+
+
+
+

Products

+
+
+ +
+
+
+
+ + + +
+
+ +
+ +
+
+
Products
+ Create new +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + +
ActiveNamePriceMemoryCpuSwapDiskIODatabasesBackupsAllocationsServersCreated at
+ +
+
+ + +
+ + +
+ + + + + + +@endsection diff --git a/resources/views/admin/vouchers/show.blade.php b/resources/views/admin/vouchers/show.blade.php new file mode 100644 index 000000000..46d13b5d2 --- /dev/null +++ b/resources/views/admin/vouchers/show.blade.php @@ -0,0 +1,240 @@ +@extends('layouts.main') + +@section('content') + +
+
+
+
+

Products

+
+
+ +
+
+
+
+ + + +
+
+ +
+
+
Product
+
+ +
+ {{ csrf_field() }} + {{ method_field("DELETE") }} + +
+
+
+
+
+ +
+
+
+ +
+
+ + {{$product->id}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->name}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->price}} + +
+
+
+ + +
+
+
+ +
+
+ + {{$product->memory}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->cpu}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->swap}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->disk}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->io}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->databases}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->allocations}} + +
+
+
+ +
+
+
+ +
+
+ + {{$product->created_at ? $product->created_at->diffForHumans() : ''}} + +
+
+
+ + +
+
+
+ +
+
+ + {{$product->description}} + +
+
+
+ + +
+
+
+ +
+
+ + {{$product->updated_at ? $product->updated_at->diffForHumans() : ''}} + +
+
+
+ +
+
+
+ +
+
+
Servers
+
+
+ + @include('admin.servers.table' , ['filter' => '?product=' . $product->id]) + +
+
+ + +
+ + +
+ + + + +@endsection diff --git a/resources/views/models/redeem_voucher_modal.blade.php b/resources/views/models/redeem_voucher_modal.blade.php new file mode 100644 index 000000000..e69de29bb From a7f8d71f92d355a863379d2920bf69cb73f9078f Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Sat, 10 Jul 2021 00:18:22 +0200 Subject: [PATCH 02/15] Added voucher system --- .../Controllers/Admin/VoucherController.php | 142 ++++++++++ app/Models/Voucher.php | 57 ++++ database/factories/VoucherFactory.php | 29 +++ ...021_07_09_190453_create_vouchers_table.php | 36 +++ ...07_09_191913_create_user_voucher_table.php | 32 +++ resources/views/admin/users/edit.blade.php | 2 +- .../views/admin/vouchers/create.blade.php | 244 +++++++----------- resources/views/admin/vouchers/edit.blade.php | 15 +- .../views/admin/vouchers/index.blade.php | 48 ++-- resources/views/layouts/main.blade.php | 21 +- .../models/redeem_voucher_modal.blade.php | 51 ++++ routes/web.php | 4 + 12 files changed, 497 insertions(+), 184 deletions(-) create mode 100644 app/Http/Controllers/Admin/VoucherController.php create mode 100644 app/Models/Voucher.php create mode 100644 database/factories/VoucherFactory.php create mode 100644 database/migrations/2021_07_09_190453_create_vouchers_table.php create mode 100644 database/migrations/2021_07_09_191913_create_user_voucher_table.php diff --git a/app/Http/Controllers/Admin/VoucherController.php b/app/Http/Controllers/Admin/VoucherController.php new file mode 100644 index 000000000..9d439f4a8 --- /dev/null +++ b/app/Http/Controllers/Admin/VoucherController.php @@ -0,0 +1,142 @@ +validate([ + 'memo' => 'sometimes|string|max:191', + 'code' => 'required|string|alpha_dash|max:36', + 'uses' => 'required|numeric|max:2147483647', + 'credits' => 'required|numeric|between:0,99999999', + 'expires_at' => 'required|date|after:today', + ]); + + Voucher::create($request->except('_token')); + + return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been created!'); + } + + /** + * Display the specified resource. + * + * @param Voucher $voucher + * @return Response + */ + public function show(Voucher $voucher) + { + // + } + + /** + * Show the form for editing the specified resource. + * + * @param Voucher $voucher + * @return Response + */ + public function edit(Voucher $voucher) + { + // + } + + /** + * Update the specified resource in storage. + * + * @param Request $request + * @param Voucher $voucher + * @return Response + */ + public function update(Request $request, Voucher $voucher) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param Voucher $voucher + * @return RedirectResponse + */ + public function destroy(Voucher $voucher) + { + $voucher->delete(); + return redirect()->back()->with('success', 'voucher has been removed!'); + } + + public function dataTable() + { + $query = Voucher::with(['users']); + + return datatables($query) + ->addColumn('actions', function (Voucher $voucher) { + return ' + + + +
+ ' . csrf_field() . ' + ' . method_field("DELETE") . ' + +
+ '; + }) + ->addColumn('status', function (Voucher $voucher) { + $color = 'success'; + if ($voucher->getStatus() != 'VALID') $color = 'danger'; + return ''. $voucher->getStatus() .''; + }) + ->editColumn('uses', function (Voucher $voucher) { + $userCount = $voucher->users()->count(); + return "{$userCount} / {$voucher->uses}"; + }) + ->editColumn('credits', function (Voucher $voucher) { + return number_format($voucher->credits, 2, '.', ''); + }) + ->editColumn('expires_at', function (Voucher $voucher) { + return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : ''; + }) + ->editColumn('code' , function (Voucher $voucher) { + return "{$voucher->code}"; + }) + ->rawColumns(['actions' , 'code' , 'status']) + ->make(); + } + +} diff --git a/app/Models/Voucher.php b/app/Models/Voucher.php new file mode 100644 index 000000000..9071a9a24 --- /dev/null +++ b/app/Models/Voucher.php @@ -0,0 +1,57 @@ +users()->detach(); + }); + } + + public function getStatus(){ + if ($this->users()->count() >= $this->uses) return 'USES_LIMIT_REACHED'; + if ($this->expires_at->isPast()) return 'EXPIRED'; + return 'VALID'; + } + + /** + * @return BelongsToMany + */ + public function users() + { + return $this->belongsToMany(User::class); + } +} diff --git a/database/factories/VoucherFactory.php b/database/factories/VoucherFactory.php new file mode 100644 index 000000000..4f4a2f2bd --- /dev/null +++ b/database/factories/VoucherFactory.php @@ -0,0 +1,29 @@ + $this->faker->numberBetween(100, 1000), + 'expires_at' => $this->faker->dateTimeBetween(now(), '+30 days') + ]; + } +} diff --git a/database/migrations/2021_07_09_190453_create_vouchers_table.php b/database/migrations/2021_07_09_190453_create_vouchers_table.php new file mode 100644 index 000000000..30a58d92b --- /dev/null +++ b/database/migrations/2021_07_09_190453_create_vouchers_table.php @@ -0,0 +1,36 @@ +id(); + $table->string('code', 36)->unique(); + $table->string('memo')->nullable(); + $table->unsignedFloat('credits', 10); + $table->unsignedInteger('uses')->default(1); + $table->timestamp('expires_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('vouchers'); + } +} diff --git a/database/migrations/2021_07_09_191913_create_user_voucher_table.php b/database/migrations/2021_07_09_191913_create_user_voucher_table.php new file mode 100644 index 000000000..b75f7d262 --- /dev/null +++ b/database/migrations/2021_07_09_191913_create_user_voucher_table.php @@ -0,0 +1,32 @@ +foreignId('user_id')->constrained(); + $table->foreignId('voucher_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_voucher'); + } +} diff --git a/resources/views/admin/users/edit.blade.php b/resources/views/admin/users/edit.blade.php index 59988edf8..0148cb9a6 100644 --- a/resources/views/admin/users/edit.blade.php +++ b/resources/views/admin/users/edit.blade.php @@ -72,7 +72,7 @@ class="form-control @error('pterodactyl_id') is-invalid @enderror"
@error('credits') diff --git a/resources/views/admin/vouchers/create.blade.php b/resources/views/admin/vouchers/create.blade.php index 2a29bbe89..33b875a29 100644 --- a/resources/views/admin/vouchers/create.blade.php +++ b/resources/views/admin/vouchers/create.blade.php @@ -6,12 +6,12 @@
-

Products

+

Vouchers

@@ -28,163 +28,94 @@
+
+
+ Voucher details +
+
-
+ @csrf -
-
- - + +
+ + + @error('memo') +
+ {{$message}}
+ @enderror
-
-
-
- - - @error('name') -
- {{$message}} -
- @enderror -
+
+ + + @error('credits') +
+ {{$message}} +
+ @enderror +
-
- - - @error('price') -
- {{$message}} -
- @enderror -
-
- - - @error('memory') -
- {{$message}} -
- @enderror -
- -
- - - @error('cpu') -
- {{$message}} -
- @enderror +
+ +
+ +
+
+
+ @error('code') +
+ {{$message}} +
+ @enderror +
-
- - - @error('swap') -
- {{$message}} -
- @enderror +
+ +
+ +
+
+
+ @error('uses') +
+ {{$message}} +
+ @enderror +
-
- - - @error('description') -
- {{$message}} -
- @enderror -
+
+ + + @error('expires_at') +
+ {{$message}}
-
-
- - - @error('disk') -
- {{$message}} -
- @enderror -
-
- - - @error('io') -
- {{$message}} -
- @enderror -
-
- - - @error('databases') -
- {{$message}} -
- @enderror -
-
- - - @error('backups') -
- {{$message}} -
- @enderror -
-
- - - @error('allocations') -
- {{$message}} -
- @enderror -
-
+ @enderror
+
@@ -34,12 +34,13 @@
- + @csrf + @method('PATCH')
- @error('memo') @@ -51,7 +52,7 @@ class="form-control @error('memo') is-invalid @enderror">
- @@ -66,7 +67,7 @@ class="form-control @error('credits') is-invalid @enderror">
- @@ -86,7 +87,7 @@ class="form-control @error('code') is-invalid @enderror"
- @@ -105,7 +106,7 @@ class="form-control @error('uses') is-invalid @enderror"
- @error('expires_at') diff --git a/resources/views/admin/vouchers/index.blade.php b/resources/views/admin/vouchers/index.blade.php index 183e2793b..bfe4cef61 100644 --- a/resources/views/admin/vouchers/index.blade.php +++ b/resources/views/admin/vouchers/index.blade.php @@ -6,13 +6,13 @@
-

Products

+

Vouchers

@@ -28,8 +28,8 @@
-
Products
- Vouchers + Create new
@@ -39,19 +39,12 @@ class="fas fa-plus mr-1">Create new - - - - - - - - - - - - - + + + + + + @@ -79,21 +72,14 @@ function submitResult() { processing: true, serverSide: true, stateSave: true, - ajax: "{{route('admin.products.datatable')}}", + ajax: "{{route('admin.vouchers.datatable')}}", columns: [ - {data: 'disabled'}, - {data: 'name'}, - {data: 'price'}, - {data: 'memory'}, - {data: 'cpu'}, - {data: 'swap'}, - {data: 'disk'}, - {data: 'io'}, - {data: 'databases'}, - {data: 'backups'}, - {data: 'allocations'}, - {data: 'servers', sortable: false}, - {data: 'created_at'}, + {data: 'status'}, + {data: 'code'}, + {data: 'memo'}, + {data: 'credits'}, + {data: 'uses'}, + {data: 'expires_at'}, {data: 'actions', sortable: false}, ], fnDrawCallback: function( oSettings ) { diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index 5cfa4957f..6cbbfea19 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -96,6 +96,10 @@ class="float-right text-muted text-sm">{{$notification->created_at->longAbsolute Log back in @endif + + + Redeem code + @csrf @@ -253,6 +257,14 @@ class="nav-link @if(Request::routeIs('admin.settings.*')) active @endif"> + + - @endif @@ -289,6 +300,8 @@ class="nav-link @if(Request::routeIs('admin.usefullinks.*')) active @endif"> @endif @yield('content') + + @include('models.redeem_voucher_modal')
@@ -319,6 +332,12 @@ class="nav-link @if(Request::routeIs('admin.usefullinks.*')) active @endif"> diff --git a/routes/web.php b/routes/web.php index 598b09d8a..7313c70fe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,6 +12,7 @@ use App\Http\Controllers\Admin\SettingsController; use App\Http\Controllers\Admin\UsefulLinkController; use App\Http\Controllers\Admin\UserController; +use App\Http\Controllers\Admin\VoucherController; use App\Http\Controllers\Auth\SocialiteController; use App\Http\Controllers\HomeController; use App\Http\Controllers\NotificationController; @@ -110,6 +111,9 @@ Route::get('usefullinks/datatable', [UsefulLinkController::class, 'datatable'])->name('usefullinks.datatable'); Route::resource('usefullinks', UsefulLinkController::class); + Route::get('vouchers/datatable', [VoucherController::class, 'datatable'])->name('vouchers.datatable'); + Route::resource('vouchers', VoucherController::class); + Route::get('api/datatable', [ApplicationApiController::class, 'datatable'])->name('api.datatable'); Route::resource('api', ApplicationApiController::class)->parameters([ 'api' => 'applicationApi', From 55b2223435466e8036f8753aef221d9cb6375af2 Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Sat, 10 Jul 2021 08:58:11 +0200 Subject: [PATCH 03/15] Added Voucher system --- app/Http/Controllers/Admin/UserController.php | 2 +- .../Controllers/Admin/VoucherController.php | 63 +++++++++++++++--- app/Models/User.php | 47 +++++++++++++ app/Models/Voucher.php | 25 ++++++- ...0_062140_update_credits_to_users_table.php | 32 +++++++++ resources/views/admin/users/edit.blade.php | 2 +- .../views/admin/vouchers/create.blade.php | 6 +- .../models/redeem_voucher_modal.blade.php | 66 +++++++++++++++++-- routes/web.php | 8 +++ 9 files changed, 229 insertions(+), 22 deletions(-) create mode 100644 database/migrations/2021_07_10_062140_update_credits_to_users_table.php diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index e49923ba1..496c1bf32 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -78,7 +78,7 @@ public function update(Request $request, User $user) "name" => "required|string|min:4|max:30", "pterodactyl_id" => "required|numeric|unique:users,pterodactyl_id,{$user->id}", "email" => "required|string|email", - "credits" => "required|numeric|min:0|max:999999", + "credits" => "required|numeric|min:0|max:99999999", "server_limit" => "required|numeric|min:0|max:1000000", "role" => Rule::in(['admin', 'mod', 'client', 'member']), ]); diff --git a/app/Http/Controllers/Admin/VoucherController.php b/app/Http/Controllers/Admin/VoucherController.php index 9d439f4a8..ec7420920 100644 --- a/app/Http/Controllers/Admin/VoucherController.php +++ b/app/Http/Controllers/Admin/VoucherController.php @@ -7,9 +7,13 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; +use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\ValidationData; +use Illuminate\Validation\ValidationException; class VoucherController extends Controller { @@ -42,11 +46,11 @@ public function create() public function store(Request $request) { $request->validate([ - 'memo' => 'sometimes|string|max:191', - 'code' => 'required|string|alpha_dash|max:36', - 'uses' => 'required|numeric|max:2147483647', - 'credits' => 'required|numeric|between:0,99999999', - 'expires_at' => 'required|date|after:today', + 'memo' => 'sometimes|string|max:191', + 'code' => 'required|string|alpha_dash|max:36', + 'uses' => 'required|numeric|max:2147483647', + 'credits' => 'required|numeric|between:0,99999999', + 'expires_at' => 'nullable|date|after:1 hour', ]); Voucher::create($request->except('_token')); @@ -100,9 +104,49 @@ public function destroy(Voucher $voucher) return redirect()->back()->with('success', 'voucher has been removed!'); } + /** + * @param Request $request + * @return JsonResponse + * @throws ValidationException + */ + public function redeem(Request $request) + { + #general validations + $request->validate([ + 'code' => 'required|exists:vouchers,code' + ]); + + #get voucher by code + $voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail(); + + #extra validations + if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([ + 'code' => 'This voucher has reached the maximum amount of uses' + ]); + + if ($voucher->getStatus() == 'EXPIRED') throw ValidationException::withMessages([ + 'code' => 'This voucher has expired' + ]); + + if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([ + 'code' => 'You already redeemed this voucher code' + ]); + + if ($request->user()->credits + $voucher->credits >= 99999999) throw ValidationException::withMessages([ + 'code' => "You can't redeem a voucher with this many credits" + ]); + + #redeem voucher + $voucher->redeem($request->user()); + + return response()->json([ + 'success' => "{$voucher->credits} credits have been added to your balance!" + ]); + } + public function dataTable() { - $query = Voucher::with(['users']); + $query = Voucher::query(); return datatables($query) ->addColumn('actions', function (Voucher $voucher) { @@ -120,7 +164,7 @@ public function dataTable() ->addColumn('status', function (Voucher $voucher) { $color = 'success'; if ($voucher->getStatus() != 'VALID') $color = 'danger'; - return ''. $voucher->getStatus() .''; + return '' . $voucher->getStatus() . ''; }) ->editColumn('uses', function (Voucher $voucher) { $userCount = $voucher->users()->count(); @@ -130,12 +174,13 @@ public function dataTable() return number_format($voucher->credits, 2, '.', ''); }) ->editColumn('expires_at', function (Voucher $voucher) { + if (!$voucher->expires_at) return ""; return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : ''; }) - ->editColumn('code' , function (Voucher $voucher) { + ->editColumn('code', function (Voucher $voucher) { return "{$voucher->code}"; }) - ->rawColumns(['actions' , 'code' , 'status']) + ->rawColumns(['actions', 'code', 'status']) ->make(); } diff --git a/app/Models/User.php b/app/Models/User.php index 61605aeae..a65470995 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,17 +7,30 @@ use App\Notifications\WelcomeMessage; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Spatie\Activitylog\Traits\CausesActivity; use Spatie\Activitylog\Traits\LogsActivity; +/** + * Class User + * @package App\Models + */ class User extends Authenticatable implements MustVerifyEmail { use HasFactory, Notifiable, LogsActivity, CausesActivity; + /** + * @var string[] + */ protected static $logAttributes = ['name', 'email']; + /** + * @var string[] + */ protected static $ignoreChangedAttributes = [ 'remember_token', 'credits', @@ -68,6 +81,9 @@ class User extends Authenticatable implements MustVerifyEmail 'last_seen' => 'datetime', ]; + /** + * + */ public static function boot() { parent::boot(); @@ -93,20 +109,32 @@ public static function boot() }); } + /** + * + */ public function sendEmailVerificationNotification() { $this->notify(new QueuedVerifyEmail); } + /** + * @return string + */ public function credits() { return number_format($this->credits, 2, '.', ''); } + /** + * @return string + */ public function getAvatar(){ return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email))); } + /** + * @return string + */ public function creditUsage() { $usage = 0; @@ -118,6 +146,9 @@ public function creditUsage() return number_format($usage, 2, '.', ''); } + /** + * @return array|string|string[] + */ public function getVerifiedStatus(){ $status = ''; if ($this->hasVerifiedEmail()) $status .= 'email '; @@ -126,15 +157,31 @@ public function getVerifiedStatus(){ return $status; } + /** + * @return BelongsToMany + */ + public function vouchers(){ + return $this->belongsToMany(Voucher::class); + } + + /** + * @return HasOne + */ public function discordUser(){ return $this->hasOne(DiscordUser::class); } + /** + * @return HasMany + */ public function servers() { return $this->hasMany(Server::class); } + /** + * @return HasMany + */ public function payments() { return $this->hasMany(Payment::class); diff --git a/app/Models/Voucher.php b/app/Models/Voucher.php index 9071a9a24..0315c8d92 100644 --- a/app/Models/Voucher.php +++ b/app/Models/Voucher.php @@ -41,14 +41,35 @@ public static function boot() }); } + /** + * @return string + */ public function getStatus(){ if ($this->users()->count() >= $this->uses) return 'USES_LIMIT_REACHED'; - if ($this->expires_at->isPast()) return 'EXPIRED'; + if (!is_null($this->expires_at)){ + if ($this->expires_at->isPast()) return 'EXPIRED'; + } + return 'VALID'; } /** - * @return BelongsToMany + * @param User $user + * @return float + */ + public function redeem(User $user){ + try { + $user->increment('credits' , $this->credits); + $this->users()->attach($user); + }catch (\Exception $exception) { + throw $exception; + } + + return $this->credits; + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function users() { diff --git a/database/migrations/2021_07_10_062140_update_credits_to_users_table.php b/database/migrations/2021_07_10_062140_update_credits_to_users_table.php new file mode 100644 index 000000000..bce7b3441 --- /dev/null +++ b/database/migrations/2021_07_10_062140_update_credits_to_users_table.php @@ -0,0 +1,32 @@ +unsignedFloat('credits', 10)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->unsignedFloat('credits')->change(); + }); + } +} diff --git a/resources/views/admin/users/edit.blade.php b/resources/views/admin/users/edit.blade.php index 0148cb9a6..0358f1899 100644 --- a/resources/views/admin/users/edit.blade.php +++ b/resources/views/admin/users/edit.blade.php @@ -72,7 +72,7 @@ class="form-control @error('pterodactyl_id') is-invalid @enderror"
@error('credits') diff --git a/resources/views/admin/vouchers/create.blade.php b/resources/views/admin/vouchers/create.blade.php index 33b875a29..94ff80289 100644 --- a/resources/views/admin/vouchers/create.blade.php +++ b/resources/views/admin/vouchers/create.blade.php @@ -38,7 +38,7 @@ @csrf
- + @@ -53,7 +53,7 @@ class="form-control @error('memo') is-invalid @enderror"> @error('credits')
@@ -84,7 +84,7 @@ class="form-control @error('code') is-invalid @enderror"
- +
@@ -34,7 +36,7 @@
@@ -43,9 +45,61 @@ diff --git a/routes/web.php b/routes/web.php index 7313c70fe..d4658b714 100644 --- a/routes/web.php +++ b/routes/web.php @@ -66,6 +66,14 @@ Route::get('/auth/redirect', [SocialiteController::class, 'redirect'])->name('auth.redirect'); Route::get('/auth/callback', [SocialiteController::class, 'callback'])->name('auth.callback'); + #voucher redeem + Route::post('/voucher/redeem' , [VoucherController::class , 'redeem'])->name('voucher.redeem'); + + Route::get('/test' , function (Request $request) { + $voucher = \App\Models\Voucher::first(); + dd($request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()); + }); + #admin Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () { From 7aa6acb5d8b05b269e94167ca1849d7790af20ea Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Sat, 10 Jul 2021 09:10:42 +0200 Subject: [PATCH 04/15] added the ability to edit vouchers --- .../Controllers/Admin/VoucherController.php | 21 +++++++++++++----- resources/views/admin/vouchers/edit.blade.php | 22 +++++++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Admin/VoucherController.php b/app/Http/Controllers/Admin/VoucherController.php index ec7420920..554c0fb71 100644 --- a/app/Http/Controllers/Admin/VoucherController.php +++ b/app/Http/Controllers/Admin/VoucherController.php @@ -73,11 +73,13 @@ public function show(Voucher $voucher) * Show the form for editing the specified resource. * * @param Voucher $voucher - * @return Response + * @return Application|Factory|View */ public function edit(Voucher $voucher) { - // + return view('admin.vouchers.edit' , [ + 'voucher' => $voucher + ]); } /** @@ -85,11 +87,21 @@ public function edit(Voucher $voucher) * * @param Request $request * @param Voucher $voucher - * @return Response + * @return RedirectResponse */ public function update(Request $request, Voucher $voucher) { - // + $request->validate([ + 'memo' => 'sometimes|string|max:191', + 'code' => 'required|string|alpha_dash|max:36', + 'uses' => 'required|numeric|max:2147483647', + 'credits' => 'required|numeric|between:0,99999999', + 'expires_at' => 'nullable|date|after:1 hour', + ]); + + $voucher->update($request->except('_token')); + + return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been updated!'); } /** @@ -151,7 +163,6 @@ public function dataTable() return datatables($query) ->addColumn('actions', function (Voucher $voucher) { return ' -
diff --git a/resources/views/admin/vouchers/edit.blade.php b/resources/views/admin/vouchers/edit.blade.php index a197f8e26..f04e714af 100644 --- a/resources/views/admin/vouchers/edit.blade.php +++ b/resources/views/admin/vouchers/edit.blade.php @@ -12,7 +12,8 @@
@@ -39,7 +40,9 @@ @method('PATCH')
- + @@ -52,9 +55,9 @@ class="form-control @error('memo') is-invalid @enderror">
- @error('credits')
@@ -67,7 +70,7 @@ class="form-control @error('credits') is-invalid @enderror">
- @@ -85,9 +88,11 @@ class="form-control @error('code') is-invalid @enderror"
- +
- @@ -103,10 +108,9 @@ class="form-control @error('uses') is-invalid @enderror" @enderror
-
- @error('expires_at') From 6895dd72cbc631b3b88416bdc8d0228f4d30aa4d Mon Sep 17 00:00:00 2001 From: AVMG20 Date: Sat, 10 Jul 2021 09:13:56 +0200 Subject: [PATCH 05/15] removed button --- resources/views/models/redeem_voucher_modal.blade.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/resources/views/models/redeem_voucher_modal.blade.php b/resources/views/models/redeem_voucher_modal.blade.php index 135b1cb56..4407cf90e 100644 --- a/resources/views/models/redeem_voucher_modal.blade.php +++ b/resources/views/models/redeem_voucher_modal.blade.php @@ -1,8 +1,3 @@ - - - - +
@@ -31,7 +32,9 @@
@@ -40,35 +43,35 @@ + + + + + +
ActiveNamePriceMemoryCpuSwapDiskIODatabasesBackupsAllocationsServersCreated atStatusCodeMemoCreditsUsed / UsesExpires