Filter projects by category id and expose category_id
Deploy Backend / deploy (push) Successful in 2m44s
Deploy Backend / deploy (push) Successful in 2m44s
This commit is contained in:
1 parent
eacd0d73f1
commit
c2960c4ed0
2 files changed
+58
-1
No files matched your search
@@ -21,6 +21,7 @@ class ProjectController extends Controller
|
||||
* @OA\Parameter(name="locale", in="path", required=true, @OA\Schema(type="string", enum={"fa","en"})),
|
||||
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
|
||||
* @OA\Parameter(name="sort", in="query", @OA\Schema(type="string", enum={"asc","desc"})),
|
||||
* @OA\Parameter(name="category_id", in="query", @OA\Schema(type="integer")),
|
||||
* @OA\Response(response=200, description="OK")
|
||||
* )
|
||||
*/
|
||||
@@ -28,7 +29,21 @@ class ProjectController extends Controller
|
||||
{
|
||||
$perPage = request()->query('per_page', 10);
|
||||
|
||||
$projects = Project::with('category')->paginate($perPage);
|
||||
/**
|
||||
* The category tabs on the website filter by the category id instead of
|
||||
* its translated title: titles change with the locale and fall back to
|
||||
* English when a translation is missing, so matching on them silently
|
||||
* showed every project under every tab.
|
||||
*/
|
||||
$query = Project::with('category');
|
||||
|
||||
if ($categoryId = request()->query('category_id')) {
|
||||
$query->where('category_id', (int) $categoryId);
|
||||
}
|
||||
|
||||
$sort = request()->query('sort') === 'asc' ? 'asc' : 'desc';
|
||||
$projects = $query->orderBy('id', $sort)->paginate($perPage);
|
||||
|
||||
$formattedProjects = $projects->items();
|
||||
|
||||
$formattedProjects = array_map(function ($project) use ($locale) {
|
||||
@@ -36,6 +51,7 @@ class ProjectController extends Controller
|
||||
'id' => $project->id,
|
||||
'title' => $project->getTranslation('title', $locale),
|
||||
'category' => $project->category?->getTranslation('title', $locale),
|
||||
'category_id' => $project->category_id,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
@@ -102,6 +118,7 @@ class ProjectController extends Controller
|
||||
'id' => $project->id,
|
||||
'title' => $project->getTranslation('title', $locale),
|
||||
'category' => $project->category?->getTranslation('title', $locale),
|
||||
'category_id' => $project->category_id,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
|
||||
@@ -37,4 +37,44 @@ class ProjectTest extends TestCase
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* The category tabs on the website filter by category id, because titles are
|
||||
* translated and fall back to English when a translation is missing.
|
||||
*/
|
||||
public function test_can_filter_projects_by_category_id()
|
||||
{
|
||||
$first = ProjectCategory::factory()->create();
|
||||
$first->setTranslation('title', 'en', 'App Design');
|
||||
$first->save();
|
||||
|
||||
$second = ProjectCategory::factory()->create();
|
||||
$second->setTranslation('title', 'en', 'Backend');
|
||||
$second->save();
|
||||
|
||||
$wanted = Project::factory()->create(['category_id' => $first->id]);
|
||||
Project::factory()->create(['category_id' => $second->id]);
|
||||
|
||||
$response = $this->getJson("/api/website/projects/translation/en?category_id={$first->id}");
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(1, 'projects.data');
|
||||
$this->assertSame($wanted->id, $response->json('projects.data.0.id'));
|
||||
$this->assertSame($first->id, $response->json('projects.data.0.category_id'));
|
||||
}
|
||||
|
||||
public function test_projects_expose_their_category_id()
|
||||
{
|
||||
$category = ProjectCategory::factory()->create();
|
||||
$project = Project::factory()->create(['category_id' => $category->id]);
|
||||
|
||||
$response = $this->getJson('/api/website/projects/translation/en');
|
||||
|
||||
$response->assertStatus(200);
|
||||
$this->assertSame($category->id, $response->json('projects.data.0.category_id'));
|
||||
|
||||
$single = $this->getJson("/api/website/projects/{$project->id}/translation/en");
|
||||
$single->assertStatus(200);
|
||||
$this->assertSame($category->id, $single->json('project.category_id'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user