|
@@ -10,6 +10,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Http\Response;
|
|
|
use Illuminate\Validation\Rule;
|
|
|
+use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
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 int $id
|
|
|
* @return User
|
|
|
*/
|
|
|
- public function addCredits(Request $request, int $id)
|
|
|
+ public function increment(Request $request, int $id)
|
|
|
{
|
|
|
$discordUser = DiscordUser::find($id);
|
|
|
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
|
|
|
|
|
$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([
|
|
|
+ 'credits' => "You can't add this amount of credits because you would exceed the credit limit"
|
|
|
+ ]);
|
|
|
+ $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;
|
|
|
+ }
|
|
|
|
|
|
- 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"
|
|
|
+ /**
|
|
|
+ * 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",
|
|
|
]);
|
|
|
|
|
|
- $user->increment('credits', $request->credits);
|
|
|
+ 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;
|
|
|
}
|