-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: record page view for names (#53)
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class IncrementPageViewForName implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public function __construct( | ||
public int $nameId | ||
) { | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
// we use the DB facade and not the model for performance reasons | ||
// if we call the model inside the job, the model will be serialized | ||
// and will consume resources. we don't need to serialize the model for | ||
// this operation. | ||
DB::table('names') | ||
->where('id', $this->nameId) | ||
->increment('page_views'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Jobs; | ||
|
||
use App\Jobs\IncrementPageViewForName; | ||
use App\Models\Name; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Tests\TestCase; | ||
|
||
class IncrementPageViewForNameTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_increments_the_page_view_for_a_name(): void | ||
{ | ||
$name = Name::factory()->create(); | ||
|
||
IncrementPageViewForName::dispatch($name->id); | ||
|
||
$this->assertDatabaseHas('names', [ | ||
'id' => $name->id, | ||
'page_views' => 2, | ||
]); | ||
} | ||
} |