From c0bfcd07c755669e542c3b10fed863b1db1d1250 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 18 Jun 2023 16:34:25 +0300 Subject: [PATCH] Add CartController, the selectCity method and its test. --- .../Controllers/API/v2/CartController.php | 52 ++++++++ .../Controllers/API/v2/CityController.php | 14 -- tests/Feature/CartTest.php | 122 ++++++++++++++++++ 3 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 tests/Feature/CartTest.php diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index 5112158..90074f5 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -6,6 +6,13 @@ use App\Http\Requests\StoreCartRequest; use App\Http\Requests\UpdateCartRequest; use App\Http\Resources\API\v2\CartResource; use App\Models\Cart; +use App\Models\User; +use App\Models\City; +use Illuminate\Http\Request; + +use App\Http\Controllers\Controller; +use App\Http\Controllers\API\v2\UserController; +use Illuminate\Auth\Events\Validated; class CartController extends Controller { @@ -17,6 +24,51 @@ class CartController extends Controller // } + public function selectCity(Request $request) { + $matrix_username = $request->input('matrix_username') ?? ''; + $city_name = $request->input('city_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 city + $validation = City::validate_with_name($city_name); + if(array_key_exists('error', $validation)) + return response()->json($validation); + + // Get objects + $user = User::where('matrix_username', $matrix_username)->first(); + $city = City::where('name', $city_name)->first(); + + $cart = Cart::firstOrCreate( + [ + 'user_id' => $user->id, + 'status' => 'CART' + ], + [ + 'city_id' => $city->id, + ] + ); + + if($cart->city_id != $city->id) { + $cart->setCity($city); + + return response()->json([ + 'ok' => 'City changed successfully', + 'name' => $city->name, + 'uuid' => $city->uuid, + ]); + } + + return response()->json([ + 'ok' => 'City selected successfully', + 'name' => $city->name, + 'uuid' => $city->uuid, + ]); + } + /** * Show the form for creating a new resource. */ diff --git a/app/Http/Controllers/API/v2/CityController.php b/app/Http/Controllers/API/v2/CityController.php index 02687db..25960fd 100644 --- a/app/Http/Controllers/API/v2/CityController.php +++ b/app/Http/Controllers/API/v2/CityController.php @@ -32,20 +32,6 @@ class CityController extends Controller return new CityCollection(City::all()); } - public function selectCity(Request $request) - { - $name = $request->input('name'); - $uuid = $request->input('uuid'); - - // Process the received name and uuid as required - - // Return a response - return response()->json([ - 'message' => 'City selected successfully', - 'name' => $name, - 'uuid' => $uuid, - ]); - } /** * Show the form for creating a new resource. diff --git a/tests/Feature/CartTest.php b/tests/Feature/CartTest.php new file mode 100644 index 0000000..31da381 --- /dev/null +++ b/tests/Feature/CartTest.php @@ -0,0 +1,122 @@ +test_user = User::where('username', $this->test_user_username)->first(); + $this->test_city = City::where('name', $this->test_city_name)->first(); + + $this->assertNotNull($this->test_user); + $this->assertNotNull($this->test_city); + } + + public function test_select_city_first_time(): void + { + $this->test_user = User::where('username', $this->test_user_username)->first(); + $this->test_city = City::where('name', $this->test_city_name)->first(); + + $this->assertNotNull($this->test_user); + $this->assertNotNull($this->test_city); + + $response = $this->post('/api/v2/select-city', [ + 'matrix_username' => $this->test_user->matrix_username, + 'city_name' => $this->test_city->name, + ]); + + $response->assertStatus(200); + + $response->assertJson([ + 'ok' => 'City selected successfully', + 'name' => $this->test_city->name, + 'uuid' => $this->test_city->uuid, + ]); + + $this->assertDatabaseHas('carts', [ + 'city_id' => $this->test_city->id, + 'user_id' => $this->test_user->id, + 'status' => 'CART', + ]); + } + + public function test_select_another_city_time(): void + { + $this->test_user = User::where('username', $this->test_user_username)->first(); + $this->test_city = City::where('name', $this->test_city_name2)->first(); + + $this->assertNotNull($this->test_user); + $this->assertNotNull($this->test_city); + + $response = $this->post('/api/v2/select-city', [ + 'matrix_username' => $this->test_user->matrix_username, + 'city_name' => $this->test_city->name, + ]); + + $response->assertStatus(200); + + $response->assertJson([ + 'ok' => 'City changed successfully', + 'name' => $this->test_city->name, + 'uuid' => $this->test_city->uuid, + ]); + + $this->assertDatabaseHas('carts', [ + 'city_id' => $this->test_city->id, + 'user_id' => $this->test_user->id, + 'status' => 'CART', + ]); + } + + public function test_select_city_with_no_city(): void + { + $this->test_city = City::where('name', $this->test_city_name2)->first(); + + $this->assertNotNull($this->test_city); + + $response = $this->post('/api/v2/select-city', [ + 'matrix_username' => '', + 'city_name' => $this->test_city->name, + ]); + + $response->assertStatus(200); + + $response->assertJson([ + 'error' => 'The username is empty, please, write username!!!' + ]); + } + + public function test_select_city_with_no_user(): void + { + $this->test_city = City::where('name', $this->test_city_name2)->first(); + + $this->assertNotNull($this->test_city); + + $response = $this->post('/api/v2/select-city', [ + 'matrix_username' => '', + 'city_name' => $this->test_city->name, + ]); + + $response->assertStatus(200); + + $response->assertJson([ + 'error' => 'The username is empty, please, write username!!!' + ]); + } +}