Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
feat: associate contacts with posts (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 4, 2022
1 parent ef5c005 commit bedc777
Show file tree
Hide file tree
Showing 36 changed files with 999 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\Domains\Contact\ManageReligion\Web\ViewHelpers\ModuleReligionViewHelper;
use App\Domains\Contact\ManageReminders\Web\ViewHelpers\ModuleRemindersViewHelper;
use App\Domains\Contact\ManageTasks\Web\ViewHelpers\ModuleContactTasksViewHelper;
use App\Domains\Vault\ManageJournals\Web\ViewHelpers\ModulePostsViewHelper;
use App\Helpers\StorageHelper;
use App\Models\Contact;
use App\Models\Module;
Expand Down Expand Up @@ -285,6 +286,10 @@ public static function modules(TemplatePage $page, Contact $contact, User $user)
$data = ModulePhotosViewHelper::data($contact);
}

if ($module->type == Module::TYPE_POSTS) {
$data = ModulePostsViewHelper::data($contact, $user);
}

$modulesCollection->push([
'id' => $module->id,
'type' => $module->type,
Expand Down
16 changes: 16 additions & 0 deletions app/Domains/Settings/CreateAccount/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,22 @@ private function addTemplatePageInformation(): void
'template_page_id' => $templatePageInformation->id,
'module_id' => $module->id,
]);

// Posts
$module = (new CreateModule())->execute([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'name' => trans('app.module_posts'),
'type' => Module::TYPE_POSTS,
'can_be_deleted' => false,
]);
(new AssociateModuleToTemplatePage())->execute([
'account_id' => $this->author->account_id,
'author_id' => $this->author->id,
'template_id' => $this->template->id,
'template_page_id' => $templatePageInformation->id,
'module_id' => $module->id,
]);
}

private function addFirstInformation(): void
Expand Down
96 changes: 96 additions & 0 deletions app/Domains/Vault/ManageJournals/Services/AddContactToPost.php
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Domains\Vault\ManageJournals\Web\Controllers;

use App\Domains\Vault\ManageJournals\Services\AddContactToPost;
use App\Domains\Vault\ManageJournals\Services\CreatePost;
use App\Domains\Vault\ManageJournals\Services\DestroyPost;
use App\Domains\Vault\ManageJournals\Services\IncrementPostReadCounter;
Expand Down Expand Up @@ -125,6 +126,25 @@ public function update(Request $request, int $vaultId, int $journalId, int $post
'written_at' => Carbon::now()->format('Y-m-d'),
]);

$post->contacts()->detach();

if ($request->input('contacts')) {
if (count($request->input('contacts')) > 0) {
foreach ($request->input('contacts') as $contact) {
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'vault_id' => $vaultId,
'journal_id' => $journalId,
'post_id' => $postId,
'contact_id' => $contact['id'],
];

(new AddContactToPost())->execute($data);
}
}
}

return response()->json([
'data' => PostHelper::statistics($post),
], 200);
Expand Down
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,
]),
],
]);
}
}
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,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domains\Vault\ManageJournals\Web\ViewHelpers;

use App\Helpers\PostHelper;
use App\Models\Contact;
use App\Models\Journal;
use App\Models\Post;
use App\Models\PostSection;
Expand Down Expand Up @@ -37,10 +38,15 @@ public static function data(Journal $journal, Post $post): array
return self::dtoTag($journal, $post, $tag);
});

$contacts = $post->contacts()
->get()
->map(fn (Contact $contact) => self::dtoContact($contact));

return [
'id' => $post->id,
'title' => $post->title,
'sections' => $sectionsCollection,
'contacts' => $contacts,
'statistics' => PostHelper::statistics($post),
'tags_in_post' => $tagsAssociatedWithPostCollection,
'tags_in_vault' => $tagsInVaultCollection,
Expand Down Expand Up @@ -98,4 +104,19 @@ public static function dtoTag(Journal $journal, Post $post, Tag $tag, bool $take
],
];
}

public static function dtoContact(Contact $contact): array
{
return [
'id' => $contact->id,
'name' => $contact->name,
'avatar' => $contact->avatar,
'url' => [
'show' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
}
Loading

0 comments on commit bedc777

Please sign in to comment.