UserController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\DiscordUser;
  5. use App\Models\User;
  6. use Illuminate\Contracts\Foundation\Application;
  7. use Illuminate\Contracts\Routing\ResponseFactory;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Validation\Rule;
  11. use Illuminate\Validation\ValidationException;
  12. class UserController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @param Request $request
  18. * @return Response
  19. */
  20. public function index(Request $request)
  21. {
  22. return User::paginate($request->query('per_page') ?? 50);
  23. }
  24. /**
  25. * Display the specified resource.
  26. *
  27. * @param int $id
  28. * @return User
  29. */
  30. public function show(int $id)
  31. {
  32. $discordUser = DiscordUser::find($id);
  33. return $discordUser ? $discordUser->user : User::findOrFail($id);
  34. }
  35. /**
  36. * Update the specified resource in storage.
  37. *
  38. * @param Request $request
  39. * @param int $id
  40. * @return User
  41. */
  42. public function update(Request $request, int $id)
  43. {
  44. $discordUser = DiscordUser::find($id);
  45. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  46. $request->validate([
  47. "name" => "sometimes|string|min:4|max:30",
  48. "email" => "sometimes|string|email",
  49. "credits" => "sometimes|numeric|min:0|max:1000000",
  50. "server_limit" => "sometimes|numeric|min:0|max:1000000",
  51. "role" => ['sometimes', Rule::in(['admin', 'mod', 'client', 'member'])],
  52. ]);
  53. $user->update($request->all());
  54. return $user;
  55. }
  56. /**
  57. * increments the users credits or/and server_limit
  58. *
  59. * @param Request $request
  60. * @param int $id
  61. * @return User
  62. * @throws ValidationException
  63. */
  64. public function increment(Request $request, int $id)
  65. {
  66. $discordUser = DiscordUser::find($id);
  67. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  68. $request->validate([
  69. "credits" => "sometimes|numeric|min:0|max:1000000",
  70. "server_limit" => "sometimes|numeric|min:0|max:1000000",
  71. ]);
  72. if($request->credits){
  73. if ($user->credits + $request->credits >= 99999999) throw ValidationException::withMessages([
  74. 'credits' => "You can't add this amount of credits because you would exceed the credit limit"
  75. ]);
  76. $user->increment('credits', $request->credits);
  77. }
  78. if($request->server_limit){
  79. if ($user->server_limit + $request->server_limit >= 2147483647) throw ValidationException::withMessages([
  80. 'server_limit' => "You cannot add this amount of servers because it would exceed the server limit."
  81. ]);
  82. $user->increment('server_limit', $request->server_limit);
  83. }
  84. return $user;
  85. }
  86. /**
  87. * decrements the users credits or/and server_limit
  88. *
  89. * @param Request $request
  90. * @param int $id
  91. * @return User
  92. * @throws ValidationException
  93. */
  94. public function decrement(Request $request, int $id)
  95. {
  96. $discordUser = DiscordUser::find($id);
  97. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  98. $request->validate([
  99. "credits" => "sometimes|numeric|min:0|max:1000000",
  100. "server_limit" => "sometimes|numeric|min:0|max:1000000",
  101. ]);
  102. if($request->credits){
  103. if($user->credits - $request->credits < 0) throw ValidationException::withMessages([
  104. 'credits' => "You can't remove this amount of credits because you would exceed the minimum credit limit"
  105. ]);
  106. $user->decrement('credits', $request->credits);
  107. }
  108. if($request->server_limit){
  109. if($user->server_limit - $request->server_limit < 0) throw ValidationException::withMessages([
  110. 'server_limit' => "You cannot remove this amount of servers because it would exceed the minimum server."
  111. ]);
  112. $user->decrement('server_limit', $request->server_limit);
  113. }
  114. return $user;
  115. }
  116. /**
  117. * Remove the specified resource from storage.
  118. *
  119. * @param int $id
  120. * @return Application|Response|ResponseFactory
  121. */
  122. public function destroy(int $id)
  123. {
  124. $discordUser = DiscordUser::find($id);
  125. $user = $discordUser ? $discordUser->user : User::findOrFail($id);
  126. $user->delete();
  127. return response($user, 200);
  128. }
  129. }