|
- <?php
-
- namespace App\Http\Controllers;
-
- use Illuminate\Http\Request;
- use App\Models\Order;
- use App\Models\Service;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
-
- class OrderController extends Controller
- {
- //This should also reduce user's available credits
- public function newOrder(Request $request) {
- $user = Auth::user();
- $order = new Order;
-
- $order->service_id = $request->service;
- $order->user_id = Auth::user()->id;
- $order->quantity = $request->quantity;
- $order->url = $request->url;
- $order->note = $request->note ?: '';
- $order->status = 'processing';
-
- if (!$this->validateOrder($request)){
- abort(422);
- }
-
- $cost = ceil($order->quantity*$order->service->price/1000);
- $user->credits = $user->credits - $cost;
- if ($cost > $user->credits) {
- abort(520, 'Insufficient Credits');
- }
-
- $user->save();
- $order->save();
- }
-
- // Should probably check for other things like service availability
- public function validateOrder($order) {
- $service = Service::find($order->service);
-
- if ($order->quantity < $service->minimum ||
- $order->quantity > $service->maximum) {
- return false;
- }
- return true;
- }
-
- public function changeURL(Request $request) {
- $validated = $request->validate([
- 'order' => 'required',
- 'url' => 'required'
- ]);
-
- $order = Order::find($request->order);
- $user = Auth::user();
-
- if (!in_array($order->status, ['processing', 'error'])) {
- abort(422);
- }
-
- $order->url = $request->url;
- $order->save();
- }
- }
|