Add the selectCompany method and its route.
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Http\Resources\API\v2\CartResource;
|
|||||||
use App\Models\Cart;
|
use App\Models\Cart;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\City;
|
use App\Models\City;
|
||||||
|
use App\Models\Company;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
@@ -69,6 +70,48 @@ class CartController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function selectCompany(Request $request) {
|
||||||
|
$matrix_username = $request->input('matrix_username') ?? '';
|
||||||
|
$company_name = $request->input('company_name') ?? '';
|
||||||
|
|
||||||
|
// check for not valid user
|
||||||
|
$validation = User::validate_with_matrix_username($matrix_username);
|
||||||
|
if(array_key_exists('error', $validation))
|
||||||
|
return response()->json($validation);
|
||||||
|
|
||||||
|
// check for not valid company
|
||||||
|
$validation = Company::validate_with_name($company_name);
|
||||||
|
if(array_key_exists('error', $validation))
|
||||||
|
return response()->json($validation);
|
||||||
|
|
||||||
|
// Get objects
|
||||||
|
$user = User::where('matrix_username', $matrix_username)->first();
|
||||||
|
$company = Company::where('name', $company_name)->first();
|
||||||
|
|
||||||
|
$cart = Cart::firstOrCreate([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'status' => 'CART'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if($cart->company_id != $company->id && $cart->company_id != null) {
|
||||||
|
$cart->setCompany($company);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => 'Company changed successfully',
|
||||||
|
'name' => $company->name,
|
||||||
|
'uuid' => $company->uuid,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$cart->setCompany($company);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'ok' => 'Company selected successfully',
|
||||||
|
'name' => $company->name,
|
||||||
|
'uuid' => $company->uuid,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show the form for creating a new resource.
|
* Show the form for creating a new resource.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ Route::group([
|
|||||||
|
|
||||||
// Cart
|
// Cart
|
||||||
Route::post('select-city', 'CartController@selectCity');
|
Route::post('select-city', 'CartController@selectCity');
|
||||||
|
Route::post('select-company', 'CartController@selectCompany');
|
||||||
|
|
||||||
// User
|
// User
|
||||||
Route::post('register', 'UserController@register');
|
Route::post('register', 'UserController@register');
|
||||||
|
|||||||
Reference in New Issue
Block a user