forked from monicahq/monica
-
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: journal metrics (monicahq/chandler#443)
- Loading branch information
Showing
40 changed files
with
2,463 additions
and
5 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
app/Domains/Vault/ManageJournals/Services/CreateJournalMetric.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,66 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\JournalMetric; | ||
use App\Services\BaseService; | ||
|
||
class CreateJournalMetric extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private JournalMetric $journalMetric; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|uuid|exists:accounts,id', | ||
'vault_id' => 'required|uuid|exists:vaults,id', | ||
'author_id' => 'required|uuid|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'label' => 'required|string|max:255', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
public function execute(array $data): JournalMetric | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->create(); | ||
|
||
return $this->journalMetric; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$this->vault->journals() | ||
->findOrfail($this->data['journal_id']); | ||
} | ||
|
||
private function create(): void | ||
{ | ||
$this->journalMetric = JournalMetric::create([ | ||
'journal_id' => $this->data['journal_id'], | ||
'label' => $this->data['label'], | ||
]); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/Domains/Vault/ManageJournals/Services/CreatePostMetric.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,76 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\PostMetric; | ||
use App\Services\BaseService; | ||
|
||
class CreatePostMetric extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private PostMetric $postMetric; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|uuid|exists:accounts,id', | ||
'vault_id' => 'required|uuid|exists:vaults,id', | ||
'author_id' => 'required|uuid|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'post_id' => 'required|integer|exists:posts,id', | ||
'journal_metric_id' => 'required|integer|exists:journal_metrics,id', | ||
'value' => 'required|integer', | ||
'label' => 'nullable|string|max:255', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
public function execute(array $data): PostMetric | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->create(); | ||
|
||
return $this->postMetric; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrfail($this->data['journal_id']); | ||
|
||
$journal->posts()->findOrfail($this->data['post_id']); | ||
|
||
$journal->journalMetrics() | ||
->findOrfail($this->data['journal_metric_id']); | ||
} | ||
|
||
private function create(): void | ||
{ | ||
$this->postMetric = PostMetric::create([ | ||
'journal_metric_id' => $this->data['journal_metric_id'], | ||
'post_id' => $this->data['post_id'], | ||
'value' => $this->data['value'], | ||
'label' => $this->valueOrNull($this->data, 'label'), | ||
]); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
app/Domains/Vault/ManageJournals/Services/DestroyJournalMetric.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,59 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\JournalMetric; | ||
use App\Services\BaseService; | ||
|
||
class DestroyJournalMetric extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private JournalMetric $journalMetric; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|uuid|exists:accounts,id', | ||
'vault_id' => 'required|uuid|exists:vaults,id', | ||
'author_id' => 'required|uuid|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'journal_metric_id' => 'required|integer|exists:journal_metrics,id', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
public function execute(array $data): void | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->journalMetric->delete(); | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$this->journalMetric = $journal->journalMetrics() | ||
->findOrFail($this->data['journal_metric_id']); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/Domains/Vault/ManageJournals/Services/DestroyPostMetric.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,62 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\PostMetric; | ||
use App\Services\BaseService; | ||
|
||
class DestroyPostMetric extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private PostMetric $postMetric; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|uuid|exists:accounts,id', | ||
'vault_id' => 'required|uuid|exists:vaults,id', | ||
'author_id' => 'required|uuid|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'post_id' => 'required|integer|exists:posts,id', | ||
'post_metric_id' => 'required|integer|exists:post_metrics,id', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
public function execute(array $data): void | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->postMetric->delete(); | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$post = $journal->posts()->findOrfail($this->data['post_id']); | ||
|
||
$this->postMetric = $post->postMetrics() | ||
->findOrFail($this->data['post_metric_id']); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/Domains/Vault/ManageJournals/Services/UpdateJournalMetric.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,68 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\JournalMetric; | ||
use App\Services\BaseService; | ||
|
||
class UpdateJournalMetric extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
|
||
private JournalMetric $journalMetric; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|uuid|exists:accounts,id', | ||
'vault_id' => 'required|uuid|exists:vaults,id', | ||
'author_id' => 'required|uuid|exists:users,id', | ||
'journal_id' => 'required|integer|exists:journals,id', | ||
'journal_metric_id' => 'required|integer|exists:journal_metrics,id', | ||
'label' => 'nullable|string|max:255', | ||
]; | ||
} | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
public function execute(array $data): JournalMetric | ||
{ | ||
$this->data = $data; | ||
|
||
$this->validate(); | ||
$this->update(); | ||
|
||
return $this->journalMetric; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$this->journalMetric = $journal->journalMetrics() | ||
->findOrFail($this->data['journal_metric_id']); | ||
} | ||
|
||
private function update(): void | ||
{ | ||
$this->journalMetric->label = $this->data['label']; | ||
$this->journalMetric->save(); | ||
} | ||
} |
Oops, something went wrong.