improved user controller

This commit is contained in:
AVMG20 2021-10-11 23:46:06 +02:00
parent e53a055bc2
commit 3665b9ff55
5 changed files with 95 additions and 30 deletions

View file

@ -7,23 +7,33 @@ use App\Http\Controllers\Controller;
use App\Models\DiscordUser;
use App\Models\User;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Spatie\QueryBuilder\QueryBuilder;
class UserController extends Controller
{
const ALLOWED_INCLUDES = ['servers', 'payments', 'vouchers', 'discordUser'];
const ALLOWED_FILTERS = ['email', 'pterodactyl_id', 'role', 'suspended'];
/**
* Display a listing of the resource.
*
* @param Request $request
* @return Response
* @return LengthAwarePaginator
*/
public function index(Request $request)
{
return User::paginate($request->query('per_page') ?? 50);
$query = QueryBuilder::for(User::class)
->allowedIncludes(self::ALLOWED_INCLUDES)
->allowedFilters(self::ALLOWED_FILTERS);
return $query->paginate($request->input('per_page') ?? 50);
}
@ -31,12 +41,22 @@ class UserController extends Controller
* Display the specified resource.
*
* @param int $id
* @return User
* @return User|Collection
*/
public function show(int $id)
{
$discordUser = DiscordUser::find($id);
return $discordUser ? $discordUser->user : User::findOrFail($id);
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$query = QueryBuilder::for($user)
->with('discordUser')
->allowedIncludes(self::ALLOWED_INCLUDES)
->where('users.id' , '=' , $id)
->orWhereHas('discordUser' , function (Builder $builder) use ($id) {
$builder->where('id' , '=' , $id);
});
return $query->get();
}
@ -53,11 +73,11 @@ class UserController extends Controller
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$request->validate([
"name" => "sometimes|string|min:4|max:30",
"email" => "sometimes|string|email",
"credits" => "sometimes|numeric|min:0|max:1000000",
"name" => "sometimes|string|min:4|max:30",
"email" => "sometimes|string|email",
"credits" => "sometimes|numeric|min:0|max:1000000",
"server_limit" => "sometimes|numeric|min:0|max:1000000",
"role" => ['sometimes', Rule::in(['admin', 'mod', 'client', 'member'])],
"role" => ['sometimes', Rule::in(['admin', 'mod', 'client', 'member'])],
]);
$user->update($request->all());
@ -81,23 +101,23 @@ class UserController extends Controller
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$request->validate([
"credits" => "sometimes|numeric|min:0|max:1000000",
"credits" => "sometimes|numeric|min:0|max:1000000",
"server_limit" => "sometimes|numeric|min:0|max:1000000",
]);
if($request->credits){
if ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
if ($request->credits) {
if ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
'credits' => "You can't add this amount of credits because you would exceed the credit limit"
]);
event(new UserUpdateCreditsEvent($user));
$user->increment('credits', $request->credits);
}
}
if($request->server_limit){
if ($request->server_limit) {
if ($user->server_limit + $request->server_limit >= 2147483647) throw ValidationException::withMessages([
'server_limit' => "You cannot add this amount of servers because it would exceed the server limit."
]);
$user->increment('server_limit', $request->server_limit);
$user->increment('server_limit', $request->server_limit);
}
return $user;
@ -117,22 +137,22 @@ class UserController extends Controller
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
$request->validate([
"credits" => "sometimes|numeric|min:0|max:1000000",
"credits" => "sometimes|numeric|min:0|max:1000000",
"server_limit" => "sometimes|numeric|min:0|max:1000000",
]);
if($request->credits){
if($user->credits - $request->credits < 0) throw ValidationException::withMessages([
if ($request->credits) {
if ($user->credits - $request->credits < 0) throw ValidationException::withMessages([
'credits' => "You can't remove this amount of credits because you would exceed the minimum credit limit"
]);
$user->decrement('credits', $request->credits);
}
}
if($request->server_limit){
if($user->server_limit - $request->server_limit < 0) throw ValidationException::withMessages([
if ($request->server_limit) {
if ($user->server_limit - $request->server_limit < 0) throw ValidationException::withMessages([
'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
]);
$user->decrement('server_limit', $request->server_limit);
$user->decrement('server_limit', $request->server_limit);
}
return $user;

View file

@ -29,7 +29,6 @@ class AppServiceProvider extends ServiceProvider
{
Paginator::useBootstrap();
Schema::defaultStringLength(191);
QueryBuilderRequest::setArrayValueDelimiter('|');
Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {

View file

@ -23,7 +23,7 @@
"paypal/rest-api-sdk-php": "^1.14",
"socialiteproviders/discord": "^4.1",
"spatie/laravel-activitylog": "^3.16",
"spatie/laravel-query-builder": "^3.5",
"spatie/laravel-query-builder": "^3.6",
"spatie/laravel-validation-rules": "^3.0",
"yajra/laravel-datatables-oracle": "~9.0"
},

14
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b3b61a46d5d4d6560d052cfda863d12c",
"content-hash": "f7ba581ff6641d3ab79d558070e99f3c",
"packages": [
{
"name": "asm89/stack-cors",
@ -3462,16 +3462,16 @@
},
{
"name": "spatie/laravel-query-builder",
"version": "3.5.0",
"version": "3.6.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-query-builder.git",
"reference": "4e5257be24139836dc092f618d7c73bcb1c00302"
"reference": "03d8e1307dcb58b16fcc9c4947633fc60ae74802"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/4e5257be24139836dc092f618d7c73bcb1c00302",
"reference": "4e5257be24139836dc092f618d7c73bcb1c00302",
"url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/03d8e1307dcb58b16fcc9c4947633fc60ae74802",
"reference": "03d8e1307dcb58b16fcc9c4947633fc60ae74802",
"shasum": ""
},
"require": {
@ -3528,7 +3528,7 @@
"type": "custom"
}
],
"time": "2021-07-05T14:17:44+00:00"
"time": "2021-09-06T08:03:10+00:00"
},
{
"name": "spatie/laravel-validation-rules",
@ -8713,5 +8713,5 @@
"ext-intl": "*"
},
"platform-dev": [],
"plugin-api-version": "2.0.0"
"plugin-api-version": "2.1.0"
}

46
config/query-builder.php Normal file
View file

@ -0,0 +1,46 @@
<?php
/**
* @see https://github.com/spatie/laravel-query-builder
*/
return [
/*
* By default the package will use the `include`, `filter`, `sort`
* and `fields` query parameters as described in the readme.
*
* You can customize these query string parameters here.
*/
'parameters' => [
'include' => 'include',
'filter' => 'filter',
'sort' => 'sort',
'fields' => 'fields',
'append' => 'append',
],
/*
* Related model counts are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsCount
*/
'count_suffix' => 'Count',
/*
* By default the package will throw an `InvalidFilterQuery` exception when a filter in the
* URL is not allowed in the `allowedFilters()` method.
*/
'disable_invalid_filter_query_exception' => false,
/*
* By default the package inspects query string of request using $request->query().
* You can change this behavior to inspect the request body using $request->input()
* by setting this value to `body`.
*
* Possible values: `query_string`, `body`
*/
'request_data_source' => 'query_string',
];