-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add view count to journal entry (monicahq/chandler#269)
- Loading branch information
Showing
8 changed files
with
257 additions
and
2 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
32 changes: 32 additions & 0 deletions
32
database/migrations/2022_10_30_175544_add_counter_to_posts_table.php
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,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('posts', function (Blueprint $table) { | ||
$table->integer('view_count')->after('title')->default(0); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('posts', function (Blueprint $table) { | ||
$table->dropColumn('view_count'); | ||
}); | ||
} | ||
}; |
77 changes: 77 additions & 0 deletions
77
domains/Vault/ManageJournals/Services/IncrementPostReadCounter.php
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,77 @@ | ||
<?php | ||
|
||
namespace App\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Journal; | ||
use App\Models\Post; | ||
use App\Services\BaseService; | ||
|
||
class IncrementPostReadCounter extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private Post $post; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|integer|exists:accounts,id', | ||
'vault_id' => 'required|integer|exists:vaults,id', | ||
'author_id' => 'required|integer|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'post_id' => 'required|integer|exists:posts,id', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
* | ||
* @return array | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
/** | ||
* Increment the read counter of a post. | ||
* | ||
* @param array $data | ||
* @return Post | ||
*/ | ||
public function execute(array $data): Post | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->increment(); | ||
|
||
return $this->post; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
Journal::where('vault_id', $this->data['vault_id']) | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$this->post = Post::where('journal_id', $this->data['journal_id']) | ||
->findOrFail($this->data['post_id']); | ||
} | ||
|
||
private function increment(): void | ||
{ | ||
$this->post->increment('view_count'); | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
tests/Unit/Domains/Vault/ManageJournals/Services/IncrementPostReadCounterTest.php
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,135 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Exceptions\NotEnoughPermissionException; | ||
use App\Models\Account; | ||
use App\Models\Journal; | ||
use App\Models\Post; | ||
use App\Models\User; | ||
use App\Models\Vault; | ||
use App\Vault\ManageJournals\Services\IncrementPostReadCounter; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Validation\ValidationException; | ||
use Tests\TestCase; | ||
|
||
class IncrementPostReadCounterTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_increments_a_post_counter(): void | ||
{ | ||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); | ||
$journal = Journal::factory()->create([ | ||
'vault_id' => $vault->id, | ||
]); | ||
$post = Post::factory()->create([ | ||
'journal_id' => $journal->id, | ||
]); | ||
|
||
$this->executeService($regis, $regis->account, $vault, $journal, $post); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_wrong_parameters_are_given(): void | ||
{ | ||
$request = [ | ||
'title' => 'Ross', | ||
]; | ||
|
||
$this->expectException(ValidationException::class); | ||
(new IncrementPostReadCounter())->execute($request); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_user_doesnt_belong_to_account(): void | ||
{ | ||
$this->expectException(ModelNotFoundException::class); | ||
|
||
$regis = $this->createUser(); | ||
$account = Account::factory()->create(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); | ||
$journal = Journal::factory()->create([ | ||
'vault_id' => $vault->id, | ||
]); | ||
$post = Post::factory()->create([ | ||
'journal_id' => $journal->id, | ||
]); | ||
|
||
$this->executeService($regis, $account, $vault, $journal, $post); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_journal_doesnt_belong_to_vault(): void | ||
{ | ||
$this->expectException(ModelNotFoundException::class); | ||
|
||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); | ||
$journal = Journal::factory()->create(); | ||
$post = Post::factory()->create([ | ||
'journal_id' => $journal->id, | ||
]); | ||
|
||
$this->executeService($regis, $regis->account, $vault, $journal, $post); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_post_doesnt_belong_to_journal(): void | ||
{ | ||
$this->expectException(ModelNotFoundException::class); | ||
|
||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_EDIT, $vault); | ||
$journal = Journal::factory()->create([ | ||
'vault_id' => $vault->id, | ||
]); | ||
$post = Post::factory()->create(); | ||
|
||
$this->executeService($regis, $regis->account, $vault, $journal, $post); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_if_user_doesnt_have_right_permission_in_vault(): void | ||
{ | ||
$this->expectException(NotEnoughPermissionException::class); | ||
|
||
$regis = $this->createUser(); | ||
$vault = $this->createVault($regis->account); | ||
$vault = $this->setPermissionInVault($regis, Vault::PERMISSION_VIEW, $vault); | ||
$journal = Journal::factory()->create([ | ||
'vault_id' => $vault->id, | ||
]); | ||
$post = Post::factory()->create([ | ||
'journal_id' => $journal->id, | ||
]); | ||
|
||
$this->executeService($regis, $regis->account, $vault, $journal, $post); | ||
} | ||
|
||
private function executeService(User $author, Account $account, Vault $vault, Journal $journal, Post $post): void | ||
{ | ||
$request = [ | ||
'account_id' => $account->id, | ||
'vault_id' => $vault->id, | ||
'author_id' => $author->id, | ||
'journal_id' => $journal->id, | ||
'post_id' => $post->id, | ||
]; | ||
|
||
$post = (new IncrementPostReadCounter())->execute($request); | ||
|
||
$this->assertDatabaseHas('posts', [ | ||
'id' => $post->id, | ||
'journal_id' => $journal->id, | ||
'view_count' => 1, | ||
]); | ||
} | ||
} |
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