Skip to content

Commit

Permalink
Merge pull request #396 from NgBlaze/feat/add-superadmin-create-products
Browse files Browse the repository at this point in the history
feat: add superadmin create products
  • Loading branch information
Dev-Tonia authored Aug 7, 2024
2 parents 49bdbb4 + 54d6c86 commit bf1908e
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 35 deletions.
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);
}
}
2 changes: 1 addition & 1 deletion database/seeders/OrderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class OrderSeeder extends Seeder
{
public function run()
{
Order::factory()->count(10)->create();
// Order::factory()->count(10)->create();
}
}
File renamed without changes.
Binary file added public/uploads/1723037656.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1723038561.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 39 additions & 34 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
<?php

use App\Http\Controllers\Api\V1\Admin\BlogController;
use App\Http\Controllers\Api\V1\Admin\CustomerController;
use App\Http\Controllers\Api\V1\Admin\DashboardController;
use App\Http\Controllers\Api\V1\Admin\EmailTemplateController;
use App\Http\Controllers\Api\V1\Admin\Plan\FeatureController;
use App\Http\Controllers\Api\V1\Admin\Plan\SubscriptionController;
use App\Http\Controllers\Api\V1\PaymentController;
use App\Http\Controllers\Api\V1\Admin\FaqController;
use App\Http\Controllers\NotificationSettingController;
use App\Http\Controllers\UserNotificationController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\QuestController;
use App\Http\Controllers\Api\V1\JobController;
use App\Http\Controllers\Api\V1\RoleController;
use App\Http\Controllers\BillingPlanController;
use App\Http\Controllers\Api\V1\ArticleController;
use App\Http\Controllers\Api\V1\Auth\AuthController;
use App\Http\Controllers\Api\V1\Auth\ForgetPasswordRequestController;
use App\Http\Controllers\Api\V1\Auth\LoginController;
use App\Http\Controllers\Api\V1\Auth\ResetUserPasswordController;
use App\Http\Controllers\Api\V1\Auth\SocialAuthController;
use App\Http\Controllers\Api\V1\Auth\ForgotResetPasswordController;
use App\Http\Controllers\Api\V1\BlogSearchController;
use App\Http\Controllers\Api\V1\CategoryController;
use App\Http\Controllers\Api\V1\ContactController;
use App\Http\Controllers\Api\V1\HelpArticleController;
use App\Http\Controllers\Api\V1\NotificationPreferenceController;
use App\Http\Controllers\Api\V1\Organisation\OrganisationController;
use App\Http\Controllers\Api\V1\CommentController;
use App\Http\Controllers\Api\V1\Organisation\OrganizationMemberController;
use App\Http\Controllers\Api\V1\PreferenceController;
use App\Http\Controllers\Api\V1\ContactController;
use App\Http\Controllers\Api\V1\PaymentController;
use App\Http\Controllers\Api\V1\ProductController;
use App\Http\Controllers\Api\V1\SqueezeController;
use App\Http\Controllers\Api\V1\CategoryController;
use App\Http\Controllers\Api\V1\TimezoneController;
use App\Http\Controllers\Api\V1\WaitListController;
use App\Http\Controllers\Api\V1\Admin\FaqController;
use App\Http\Controllers\Api\V1\Auth\AuthController;
use App\Http\Controllers\Api\V1\JobSearchController;
use App\Http\Controllers\Api\V1\User\UserController;
use App\Http\Controllers\UserNotificationController;
use App\Http\Controllers\Api\V1\Admin\BlogController;
use App\Http\Controllers\Api\V1\Auth\LoginController;
use App\Http\Controllers\Api\V1\BlogSearchController;
use App\Http\Controllers\Api\V1\PreferenceController;
use App\Http\Controllers\Api\V1\HelpArticleController;
use App\Http\Controllers\Api\V1\SqueezePageCoontroller;
use App\Http\Controllers\Api\V1\User\AccountController;
use App\Http\Controllers\Api\V1\User\ProfileController;
use App\Http\Controllers\NotificationSettingController;
use App\Http\Controllers\InvitationAcceptanceController;
use App\Http\Controllers\Api\V1\Admin\CustomerController;
use App\Http\Controllers\Api\V1\Admin\DashboardController;
use App\Http\Controllers\Api\V1\Auth\SocialAuthController;
use App\Http\Controllers\Api\V1\User\ExportUserController;
use App\Http\Controllers\Api\V1\CookiePreferencesController;
use App\Http\Controllers\Api\V1\Admin\Plan\FeatureController;


use App\Http\Controllers\Api\V1\Admin\EmailTemplateController;
use App\Http\Controllers\Api\V1\Auth\ResetUserPasswordController;
use App\Http\Controllers\Api\V1\NotificationPreferenceController;
use App\Http\Controllers\Api\V1\Admin\Plan\SubscriptionController;
use App\Http\Controllers\Api\V1\Testimonial\TestimonialController;
use App\Http\Controllers\BillingPlanController;
use App\Http\Controllers\Api\V1\User\ProfileController;
use App\Http\Controllers\Api\V1\JobSearchController;
use App\Http\Controllers\Api\V1\WaitListController;
use App\Http\Controllers\Api\V1\CookiePreferencesController;
use App\Http\Controllers\Api\V1\SqueezePageCoontroller;
use App\Http\Controllers\Api\V1\TimezoneController;
use App\Http\Controllers\Api\V1\Auth\ForgotResetPasswordController;
use App\Http\Controllers\Api\V1\Organisation\OrganisationController;
use App\Http\Controllers\Api\V1\Auth\ForgetPasswordRequestController;

use App\Http\Controllers\QuestController;
use App\Http\Controllers\Api\V1\SuperAdmin\SuperAdminProductController;
use App\Http\Controllers\Api\V1\Organisation\OrganizationMemberController;
/*
|--------------------------------------------------------------------------
| API Routes
Expand Down Expand Up @@ -138,6 +139,11 @@
Route::get('/help-center/topics', [HelpArticleController::class, 'getArticles']);
Route::get('/help-center/topics/search', [HelpArticleController::class, 'search']);

//Super Admin Add Products
Route::middleware(['auth:api', 'admin'])->group(function () {
Route::post('/products', [SuperAdminProductController::class, 'store']);
});

Route::middleware(['auth:api', 'admin'])->group(function () {
Route::get('/email-templates', [EmailTemplateController::class, 'index']);
Route::post('/email-templates', [EmailTemplateController::class, 'store']);
Expand Down Expand Up @@ -257,7 +263,6 @@



// quest
// quest
Route::get('/quests/{id}/messages', [QuestController::class, 'getQuestMessages']);

});
74 changes: 74 additions & 0 deletions tests/Feature/SuperAdminProductControllerTest.php
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,
],
]);
}
}

0 comments on commit bf1908e

Please sign in to comment.