Examples of code I've written in PHP, Javascript, SCSS, etc.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

OrderController.php 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Order;
  5. use App\Models\Service;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Log;
  8. class OrderController extends Controller
  9. {
  10. //This should also reduce user's available credits
  11. public function newOrder(Request $request) {
  12. $user = Auth::user();
  13. $order = new Order;
  14. $order->service_id = $request->service;
  15. $order->user_id = Auth::user()->id;
  16. $order->quantity = $request->quantity;
  17. $order->url = $request->url;
  18. $order->note = $request->note ?: '';
  19. $order->status = 'processing';
  20. if (!$this->validateOrder($request)){
  21. abort(422);
  22. }
  23. $cost = ceil($order->quantity*$order->service->price/1000);
  24. $user->credits = $user->credits - $cost;
  25. if ($cost > $user->credits) {
  26. abort(520, 'Insufficient Credits');
  27. }
  28. $user->save();
  29. $order->save();
  30. }
  31. // Should probably check for other things like service availability
  32. public function validateOrder($order) {
  33. $service = Service::find($order->service);
  34. if ($order->quantity < $service->minimum ||
  35. $order->quantity > $service->maximum) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. public function changeURL(Request $request) {
  41. $validated = $request->validate([
  42. 'order' => 'required',
  43. 'url' => 'required'
  44. ]);
  45. $order = Order::find($request->order);
  46. $user = Auth::user();
  47. if (!in_array($order->status, ['processing', 'error'])) {
  48. abort(422);
  49. }
  50. $order->url = $request->url;
  51. $order->save();
  52. }
  53. }