Skip to content

Commit

Permalink
Creación función buscar categorías para test y legibilidad de código
Browse files Browse the repository at this point in the history
  • Loading branch information
FazeElian committed Dec 17, 2023
1 parent 6c97184 commit aca0244
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 35 deletions.
33 changes: 22 additions & 11 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ class CategoryController extends Controller
*/
public function index(Request $request)
{
// Obtenemos valor de input de búsqueda
$inputSearchValue = trim($request->get("searchCategory"));

$categories = Category::paginate();

return view('Admin.category.index', compact('categories', "inputSearchValue"))
->with('i', (request()->input('page', 1) - 1) * $categories->perPage());
}

// Función para buscar categorías
public function search(Request $request)
{
// Obtenemos valor de input de búsqueda
$inputSearchValue = trim($request->get("categorieSearch"));
$inputSearchValue = trim($request->get("searchCategory"));

// Autoincrementable para Columna No de vista
$a = 0;
$i = $a++;

// Realiza las consultas a las tablas junto con la tabla categorías
$categories = DB::table("categories")
->select("id", "name", "description")
->where("name", "LIKE", "%" . $inputSearchValue . "%")
->orWhere("id", "LIKE", "%" . $inputSearchValue . "%")
->orderBy("name", "asc")
->paginate(10);
->select("id", "name", "description")
->where("name", "LIKE", "%" . $inputSearchValue . "%")
->orWhere("id", "LIKE", "%" . $inputSearchValue . "%")
->orderBy("name", "asc")
->paginate(10);

return view('Admin.category.index', compact("categories", "i", "inputSearchValue"));
}
Expand Down Expand Up @@ -71,12 +82,12 @@ public function store(Request $request)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$category = Category::find($id);
// public function show($id)
// {
// $category = Category::find($id);

return view('Admin.category.show', compact('category'));
}
// return view('Admin.category.index', compact('category'));
// }

/**
* Show the form for editing the specified resource.
Expand Down
4 changes: 2 additions & 2 deletions resources/views/Admin/category/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</div>

<!-- Barra de búsqueda -->
<form action="{{ route("categories.index") }}" method="get" class="cont-barra-busqueda">
<input type="search" name="categorieSearch" id="barraBusqueda" placeholder="Buscar categoría de productos" value="{{ $inputSearchValue }}">
<form action="{{ route("categories.search") }}" method="get" class="cont-barra-busqueda">
<input type="search" name="searchCategory" id="barraBusqueda" placeholder="Buscar categoría de productos" value="{{ $inputSearchValue }}">
</form>

<!-- Tabla categorías -->
Expand Down
Empty file.
5 changes: 4 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

// Ruta Categorías Productos
Route::resource('products/categories', CategoryController::class)->middleware("auth");
Route::resource('products/categories', CategoryController::class)->middleware("auth")->except(['show']);

// Ruta para buscar Categorías de productos
Route::get('products/categories/search', [CategoryController::class, 'search'])->name('categories.search');

// Ruta Productos
Route::resource('products', ProductController::class)->middleware("auth");
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\Category;

class CategoryTest extends TestCase
{
/**
* A basic feature test example.
*/
// public function testCreateCategory(){
// $category = [
// "name" => "Categoría ejemplo 1",
// "description" => "Descripción Categoria ejemplo 1"
// ];

// $response = $this->post("products/categories", $category);

// $response->assertStatus(302);
// $response->assertRedirect(route("categories.index"));

// $this->assertDatabaseCount("categories", 3);

// $lastCategoryCreated = Category::query()->latest()->first();

// $this->assertDatabaseHas("categories", [
// "name" => $lastCategoryCreated->name,
// "description" => $lastCategoryCreated->description,
// ]);

// $this->assertDatabaseHas("categories", $category);

// $this->assertEquals($category["name"], $lastCategoryCreated->name);
// $this->assertEquals($category["description"], $lastCategoryCreated->description);
// }
}
5 changes: 0 additions & 5 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ public function test_the_application_returns_a_successful_response(): void

$response->assertStatus(200);
}

public function test_basic_test(): void
{
$this->assertTrue(true);
}
}
16 changes: 0 additions & 16 deletions tests/Unit/ExampleTest.php

This file was deleted.

0 comments on commit aca0244

Please sign in to comment.