Examples of code I've written in PHP, Javascript, SCSS, etc.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

67 lignes
1.5 KiB

  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. }