Skip to content

Commit

Permalink
Merge pull request #583 from tulbadex/fix/search-product-by-names
Browse files Browse the repository at this point in the history
fix: added product search to use product name
  • Loading branch information
timiajayi authored Aug 24, 2024
2 parents 9178e0b + 14fac9a commit b5f7251
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 54 deletions.
40 changes: 17 additions & 23 deletions app/Http/Controllers/Api/V1/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

use App\Models\OrganisationUser;
use App\Http\Requests\UpdateProductRequest;
use App\Models\User;
use App\Models\Product;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateProductRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use App\Http\Resources\ProductResource;
use App\Models\Order;


class ProductController extends Controller
Expand All @@ -28,8 +21,8 @@ public function search(Request $request, $orgId)
$query = Product::where('org_id', $orgId);

// Apply filters based on query parameters
if ($request->has('name')) {
$query->where('name', 'like', '%' . $request->query('name') . '%');
if ($request->has('product_name')) {
$query->where('name', 'like', '%' . $request->query('product_name') . '%');
}

if ($request->has('category')) {
Expand All @@ -46,30 +39,31 @@ public function search(Request $request, $orgId)

$products = $query->get();

// If no products are found, return a 404 response
if ($products->isEmpty()) {
return response()->json([
'type' => 'Not Found',
'title' => 'No products found',
'status' => 404,
'detail' => 'No products match the search criteria.',
'instance' => url()->current(),
], 404);
}

// Transform the products for the response
$transformedProducts = $products->map(function ($product) {
return [
'id' => $product->product_id,
'created_at' => $product->created_at,
'updated_at' => $product->updated_at,
'name' => $product->name,
'description' => $product->description,
'category' => $product->category,
'image' => $product->imageUrl ? url($product->imageUrl) : null,
'price' => $product->price,
'cost_price' => $product->cost_price,
'quantity' => $product->quantity,
'size' => $product->size,
'stock_status' => $product->quantity > 0 ? 'in stock' : 'out of stock',
'deletedAt' => $product->deletedAt,
'category' => $product->category,
'created_at' => $product->created_at->toIso8601String(),
'updated_at' => $product->updated_at->toIso8601String(),
];
});

return response()->json([
'status_code' => 200,
'message' => 'Products retrieved successfully',
'data' => $transformedProducts
]);
return response()->json($transformedProducts, 200);
}


Expand Down
35 changes: 4 additions & 31 deletions tests/Feature/ProductSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,15 @@ public function it_returns_products_based_on_search_criteria()
$category = $this->categories->first()->name;
$product = $this->products->first();

$response = $this->getJson("/api/v1/organisations/{$this->organisation->org_id}/products/search?name={$product->name}&category={$category}&minPrice=0&maxPrice=1000");

// $response = $this->getJson("/api/v1/organisations/{$this->organisation->org_id}/products/search?product_name={$product->name}&category={$category}&minPrice=0&maxPrice=1000");
$response = $this->getJson("/api/v1/organisations/{$this->organisation->org_id}/products/search?product_name={$product->name}");
$response->assertStatus(200);
$response->assertJsonStructure([
'status_code',
'message',
'data' => [
'*' => [
'id',
'name',
'price',
'cost_price',
'image',
'description',
'quantity',
'category',
'status',
'size',
'created_at',
'updated_at',
'deletedAt'
]
]
]);
}

/** @test */
public function it_returns_no_results_when_no_products_match()
{
$response = $this->getJson("/api/v1/organisations/{$this->organisation->org_id}/products/search?name=NonexistentProduct");

$response->assertStatus(200);
$response->assertJson([
'status_code' => 200,
'message' => 'Products retrieved successfully',
'data' => []
]);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->org_id}/products/search?product_name=NonexistentProduct");
$response->assertStatus(404);
}
}

0 comments on commit b5f7251

Please sign in to comment.