-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from NgBlaze/feat/add-superadmin-create-products
feat: add superadmin create products
- Loading branch information
Showing
7 changed files
with
170 additions
and
35 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\SuperAdmin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use App\Models\Product; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class SuperAdminProductController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
$validator = Validator::make($request->all(), [ | ||
'name' => 'required|string|max:255', | ||
'description' => 'required|string', | ||
'price' => 'required|numeric', | ||
'slug' => 'required|string|max:255', | ||
'tags' => 'required|string', | ||
'imageUrl' => 'nullable|string|max:255', | ||
'status' => 'required|string|max:50', | ||
'quantity' => 'required|integer', | ||
'org_id' => 'required|uuid', | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'success' => false, | ||
'status_code' => 422, | ||
'message' => 'Validation errors', | ||
'data' => $validator->errors(), | ||
], 422); | ||
} | ||
|
||
$product = Product::create([ | ||
'user_id' => auth()->id(), | ||
'name' => $request->name, | ||
'description' => $request->description, | ||
'price' => $request->price, | ||
'slug' => $request->slug, | ||
'tags' => $request->tags, | ||
'imageUrl' => $request->imageUrl, | ||
'status' => $request->status, | ||
'quantity' => $request->quantity, | ||
'is_archived' => false, | ||
'org_id' => $request->org_id, | ||
]); | ||
|
||
return response()->json([ | ||
'success' => true, | ||
'status_code' => 201, | ||
'message' => 'Product created successfully', | ||
'data' => $product, | ||
], 201); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
use App\Models\Product; | ||
|
||
class SuperAdminProductControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testSuperAdminCanCreateProduct() | ||
{ | ||
$this->artisan('migrate:fresh --seed'); | ||
|
||
$loginResponse = $this->postJson('/api/v1/auth/login', [ | ||
'email' => '[email protected]', | ||
'password' => 'bulldozer', | ||
]); | ||
|
||
$loginResponse->assertStatus(200); | ||
$loginResponse->assertJsonStructure([ | ||
'status_code', | ||
'message', | ||
'access_token', | ||
'data' => [ | ||
'user' => [ | ||
'id', | ||
'email', | ||
'role', | ||
], | ||
], | ||
]); | ||
|
||
$accessToken = $loginResponse->json('access_token'); | ||
$userId = $loginResponse->json('data.user.id'); | ||
|
||
$validOrgId = Product::first()->org_id; | ||
|
||
$productResponse = $this->withHeaders([ | ||
'Authorization' => 'Bearer ' . $accessToken, | ||
])->postJson('/api/v1/products', [ | ||
'name' => 'okoz', | ||
'description' => 'boy', | ||
'price' => 10, | ||
'status' => 'active', | ||
'slug' => 'jkdffjk', | ||
'tags' => 'gk;fk', | ||
'quantity' => '5', | ||
'org_id' => $validOrgId, | ||
]); | ||
|
||
$productResponse->assertStatus(201); | ||
$productResponse->assertJson([ | ||
'success' => true, | ||
'status_code' => 201, | ||
'message' => 'Product created successfully', | ||
'data' => [ | ||
'name' => 'okoz', | ||
'description' => 'boy', | ||
'price' => 10, | ||
'status' => 'active', | ||
'slug' => 'jkdffjk', | ||
'tags' => 'gk;fk', | ||
'quantity' => '5', | ||
'org_id' => $validOrgId, | ||
'is_archived' => false, | ||
'imageUrl' => null, | ||
'user_id' => $userId, | ||
], | ||
]); | ||
} | ||
} |