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 12
feat: associate contacts with posts #245
Merged
Merged
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c11047f
feat: associate contacts with posts
djaiss e6e269c
wip
djaiss c8b5b16
Update web.php
djaiss 2031023
wip
djaiss 3d56ed4
Merge branch 'main' into 2022-10-10-contact-in-posts
djaiss 0ca723c
Merge branch 'main' into 2022-10-10-contact-in-posts
djaiss 6f806c1
Update AddContactToPostTest.php
djaiss 3664c12
wi
djaiss 4aecfcb
wip
djaiss 97f806c
wip
djaiss 82ab9c4
Merge branch 'main' into 2022-10-10-contact-in-posts
djaiss 508f442
Merge remote-tracking branch 'origin/main' into 2022-10-10-contact-in…
asbiin 22eb206
add a watch
asbiin 2a8f8fb
wip
djaiss 323a3e8
Merge branch 'main' into 2022-10-10-contact-in-posts
djaiss 3fcd97e
Update AddContactToPost.php
djaiss 3560ae4
Update Post.php
djaiss d30b8a9
w
djaiss c1c10e2
Update app/Domains/Vault/ManageJournals/Services/RemoveContactFromPos…
djaiss d066656
Update app/Domains/Vault/ManageJournals/Services/AddContactToPost.php
djaiss be20d03
chore: php linting with pint
djaiss 14fa1f5
Update app/Domains/Vault/ManageJournals/Services/AddContactToPost.php
djaiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
97 changes: 97 additions & 0 deletions
97
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,97 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Models\Journal; | ||
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 = Journal::where('vault_id', $this->data['vault_id']) | ||
->findOrFail($this->data['journal_id']); | ||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$this->post = Post::where('journal_id', $journal->id) | ||
->findOrFail($this->data['post_id']); | ||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
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,99 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageJournals\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\Contact; | ||
use App\Models\ContactFeedItem; | ||
use App\Models\Journal; | ||
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::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']); | ||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have used a
switch
on the whole foreach, but it's more changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I won't do this in this PR but will do it. It'll be faster.