Skip to content

Commit

Permalink
[collection] add featured attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Apr 10, 2024
1 parent 7cd14b3 commit 465db48
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function index()
{
$paginator = Collection::query()
->published()
->when(request()->boolean('featured'), fn($query) => $query->where('featured', true))
->orderBy('published_at', 'desc')
->paginate();
$paginator->getCollection()->each->append('filter_items_count');
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function store()
}
}

$collection->featured = Request::boolean('featured');
$collection->published_at = Request::input('published_at');

if (Request::has('title_color')) {
Expand Down
5 changes: 5 additions & 0 deletions database/factories/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function published()
{
return $this->state(['published_at' => $this->faker->dateTime]);
}

public function featured()
{
return $this->state(['featured' => true]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('collections', function (Blueprint $table) {
$table->boolean('featured')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('collections', function (Blueprint $table) {
$table->dropColumn('featured');
});
}
};
8 changes: 8 additions & 0 deletions resources/views/collections/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@
</div>
</div>
@endcan

<div class="col-md-6">
<div class="form-group checkbox">
{{ Form::label('featured', 'Zobraziť na homepage') }}
{{ Form::checkbox('featured', @$input['featured'], options: ['class' => 'form-control']) }}
</div>
</div>

<div class="col-md-12 text-center">
{!! Form::submit('Uložiť', array('class' => 'btn btn-default')) !!} &nbsp;
{!! link_to_route('collection.index', 'Zrušiť', null, array('class' => 'btn btn-default')) !!}
Expand Down
37 changes: 29 additions & 8 deletions tests/Feature/Api/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public function testIndex()
'name' => $collection->name,
'text' => $collection->text,
'header_image_src' => 'http://localhost/images/kolekcie/image.jpg',
'header_image_srcset' => 'http://localhost/images/kolekcie/image.1920.jpg 1920w, '
. 'http://localhost/images/kolekcie/image.1400.jpg 1400w, '
. 'http://localhost/images/kolekcie/image.jpg 1024w, '
. 'http://localhost/images/kolekcie/image.640.jpg 640w',
'header_image_srcset' =>
'http://localhost/images/kolekcie/image.1920.jpg 1920w, ' .
'http://localhost/images/kolekcie/image.1400.jpg 1400w, ' .
'http://localhost/images/kolekcie/image.jpg 1024w, ' .
'http://localhost/images/kolekcie/image.640.jpg 640w',
'filter_items_url' => route('api.v1.items.index', [
'filter' => [
'author' => ['author-1'],
Expand All @@ -37,6 +38,25 @@ public function testIndex()
]);
}

public function testIndexFeatured()
{
$featured = Collection::factory()
->published()
->featured()
->create();
Collection::factory()
->published()
->create();

$url = route('api.collections.index', [
'featured' => true,
'size' => 2,
]);
$response = $this->get($url);
$response->assertJsonCount(1, 'data');
$response->assertJsonPath('data.0.id', $featured->id);
}

public function testShow()
{
$collection = Collection::factory()->create([
Expand All @@ -49,10 +69,11 @@ public function testShow()
'name' => $collection->name,
'text' => $collection->text,
'header_image_src' => 'http://localhost/images/kolekcie/image.jpg',
'header_image_srcset' => 'http://localhost/images/kolekcie/image.1920.jpg 1920w, '
. 'http://localhost/images/kolekcie/image.1400.jpg 1400w, '
. 'http://localhost/images/kolekcie/image.jpg 1024w, '
. 'http://localhost/images/kolekcie/image.640.jpg 640w',
'header_image_srcset' =>
'http://localhost/images/kolekcie/image.1920.jpg 1920w, ' .
'http://localhost/images/kolekcie/image.1400.jpg 1400w, ' .
'http://localhost/images/kolekcie/image.jpg 1024w, ' .
'http://localhost/images/kolekcie/image.640.jpg 640w',
'filter_items_url' => route('api.v1.items.index', [
'filter' => [
'author' => 'author-1',
Expand Down

0 comments on commit 465db48

Please sign in to comment.