Merge pull request #152 from Hiekki4/development
increment & decrement endpoints
This commit is contained in:
commit
a857aa488a
|
@ -10,6 +10,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -64,26 +65,69 @@ class UserController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Give credits to a user.
|
* increments the users credits or/and server_limit
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return User
|
* @return User
|
||||||
*/
|
*/
|
||||||
public function addCredits(Request $request, int $id)
|
public function increment(Request $request, int $id)
|
||||||
{
|
{
|
||||||
$discordUser = DiscordUser::find($id);
|
$discordUser = DiscordUser::find($id);
|
||||||
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
$request->validate([
|
$request->validate([
|
||||||
"credits" => "required|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 ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
|
||||||
'credits' => "You can't add this amount of credits because you would exceed the credit limit"
|
'credits' => "You can't add this amount of credits because you would exceed the credit limit"
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user->increment('credits', $request->credits);
|
$user->increment('credits', $request->credits);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* decrements the users credits or/and server_limit
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param int $id
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function decrement(Request $request, int $id)
|
||||||
|
{
|
||||||
|
$discordUser = DiscordUser::find($id);
|
||||||
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||||
|
|
||||||
|
$request->validate([
|
||||||
|
"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([
|
||||||
|
'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([
|
||||||
|
'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
|
||||||
|
]);
|
||||||
|
$user->decrement('server_limit', $request->server_limit);
|
||||||
|
}
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ use Illuminate\Support\Facades\Route;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('api.token')->group(function () {
|
Route::middleware('api.token')->group(function () {
|
||||||
Route::patch('/users/{user}/addcredits', [UserController::class, 'addCredits']);
|
Route::patch('/users/{user}/increment', [UserController::class, 'increment']);
|
||||||
|
Route::patch('/users/{user}/decrement', [UserController::class, 'decrement']);
|
||||||
Route::resource('users', UserController::class)->except(['store', 'create']);
|
Route::resource('users', UserController::class)->except(['store', 'create']);
|
||||||
|
|
||||||
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
|
Route::patch('/servers/{server}/suspend', [ServerController::class, 'suspend']);
|
||||||
|
|
Loading…
Reference in a new issue