This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: associate contacts with posts (#245)
- Loading branch information
Showing
36 changed files
with
999 additions
and
31 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
96 changes: 96 additions & 0 deletions
96
app/Domains/Vault/ManageJournals/Services/AddContactToPost.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,96 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Models\Post; | ||
use App\Services\BaseService; | ||
use Carbon\Carbon; | ||
|
||
class AddContactToPost extends BaseService implements ServiceInterface | ||
{ | ||
private Post $post; | ||
|
||
private array $data; | ||
|
||
/** | ||
* 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', | ||
'contact_id' => 'required|integer|exists:contacts,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', | ||
'contact_must_belong_to_vault', | ||
]; | ||
} | ||
|
||
/** | ||
* Add a contact to a post. | ||
* | ||
* @param array $data | ||
* @return Post | ||
*/ | ||
public function execute(array $data): Post | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
|
||
$this->post->contacts()->syncWithoutDetaching($this->contact); | ||
|
||
$this->createFeedItem(); | ||
$this->updateLastEditedDate(); | ||
|
||
return $this->post; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$this->post = $journal->posts() | ||
->findOrFail($this->data['post_id']); | ||
} | ||
|
||
private function updateLastEditedDate(): void | ||
{ | ||
$this->contact->last_updated_at = Carbon::now(); | ||
$this->contact->save(); | ||
} | ||
|
||
private function createFeedItem(): void | ||
{ | ||
$feedItem = ContactFeedItem::create([ | ||
'author_id' => $this->author->id, | ||
'contact_id' => $this->contact->id, | ||
'action' => ContactFeedItem::ACTION_ADDED_TO_POST, | ||
'description' => $this->post->title, | ||
]); | ||
$this->post->feedItem()->save($feedItem); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
app/Domains/Vault/ManageJournals/Services/RemoveContactFromPost.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,98 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Models\Post; | ||
use App\Services\BaseService; | ||
use Carbon\Carbon; | ||
|
||
class RemoveContactFromPost extends BaseService implements ServiceInterface | ||
{ | ||
private Post $post; | ||
|
||
private array $data; | ||
|
||
/** | ||
* 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', | ||
'contact_id' => 'required|integer|exists:contacts,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', | ||
'contact_must_belong_to_vault', | ||
]; | ||
} | ||
|
||
/** | ||
* Remove a contact from a post. | ||
* | ||
* @param array $data | ||
* @return Post | ||
*/ | ||
public function execute(array $data): Post | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
|
||
$this->post->contacts()->detach([ | ||
$this->contact->id, | ||
]); | ||
|
||
$this->updateLastEditedDate(); | ||
$this->createFeedItem(); | ||
|
||
return $this->post; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$journal = $this->vault->journals() | ||
->findOrFail($this->data['journal_id']); | ||
|
||
$this->post = $journal->posts() | ||
->findOrFail($this->data['post_id']); | ||
} | ||
|
||
private function updateLastEditedDate(): void | ||
{ | ||
$this->contact->last_updated_at = Carbon::now(); | ||
$this->contact->save(); | ||
} | ||
|
||
private function createFeedItem(): void | ||
{ | ||
$feedItem = ContactFeedItem::create([ | ||
'author_id' => $this->author->id, | ||
'contact_id' => $this->contact->id, | ||
'action' => ContactFeedItem::ACTION_REMOVED_FROM_POST, | ||
'description' => $this->post->title, | ||
]); | ||
$this->post->feedItem()->save($feedItem); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
app/Domains/Vault/ManageJournals/Web/ViewHelpers/ModuleContactPostViewHelper.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,29 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Web\ViewHelpers; | ||
|
||
use App\Models\Contact; | ||
use App\Models\Post; | ||
use Illuminate\Support\Collection; | ||
|
||
class ModuleContactPostViewHelper | ||
{ | ||
public static function data(Contact $contact): Collection | ||
{ | ||
return $contact->posts() | ||
->orderBy('written_at', 'desc') | ||
->get() | ||
->map(fn (Post $post) => [ | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'excerpt' => $post->excerpt, | ||
'url' => [ | ||
'show' => route('post.show', [ | ||
'vault' => $contact->vault_id, | ||
'journal' => $post->journal_id, | ||
'post' => $post->id, | ||
]), | ||
], | ||
]); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/Domains/Vault/ManageJournals/Web/ViewHelpers/ModulePostsViewHelper.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,46 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Web\ViewHelpers; | ||
|
||
use App\Helpers\DateHelper; | ||
use App\Models\Contact; | ||
use App\Models\Post; | ||
use App\Models\User; | ||
use Illuminate\Support\Collection; | ||
|
||
class ModulePostsViewHelper | ||
{ | ||
public static function data(Contact $contact, User $user): Collection | ||
{ | ||
return $contact->posts() | ||
->orderBy('created_at', 'desc') | ||
->get() | ||
->map(fn (Post $post) => self::dto($post, $user)); | ||
} | ||
|
||
public static function dto(Post $post, User $user): array | ||
{ | ||
return [ | ||
'id' => $post->id, | ||
'title' => $post->title, | ||
'journal' => [ | ||
'id' => $post->journal->id, | ||
'name' => $post->journal->name, | ||
'url' => [ | ||
'show' => route('journal.show', [ | ||
'vault' => $post->journal->vault->id, | ||
'journal' => $post->journal->id, | ||
]), | ||
], | ||
], | ||
'written_at' => DateHelper::formatDate($post->written_at, $user->timezone), | ||
'url' => [ | ||
'show' => route('post.show', [ | ||
'vault' => $post->journal->vault_id, | ||
'journal' => $post->journal->id, | ||
'post' => $post->id, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.