Skip to content

Commit

Permalink
feat: add photos to posts (monicahq/chandler#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jan 8, 2023
1 parent d234d8f commit 3ba8712
Show file tree
Hide file tree
Showing 13 changed files with 635 additions and 4 deletions.
81 changes: 81 additions & 0 deletions app/Domains/Vault/ManageJournals/Services/AddPhotoToPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Domains\Vault\ManageJournals\Services;

use App\Interfaces\ServiceInterface;
use App\Models\File;
use App\Models\Post;
use App\Services\BaseService;

class AddPhotoToPost extends BaseService implements ServiceInterface
{
private Post $post;

private array $data;

private File $file;

/**
* 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' => 'nullable|integer|exists:posts,id',
'file_id' => 'required|integer|exists:files,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',
];
}

/**
* Add an image to a post.
*
* @param array $data
* @return Post
*/
public function execute(array $data): Post
{
$this->data = $data;
$this->validate();

$this->file->fileable_id = $this->post->id;
$this->file->fileable_type = Post::class;
$this->file->type = File::TYPE_PHOTO;
$this->file->save();

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']);

$this->file = $this->vault->files()
->findOrFail($this->data['file_id']);
}
}
9 changes: 9 additions & 0 deletions app/Domains/Vault/ManageJournals/Services/DestroyPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function execute(array $data): void
$this->data = $data;

$this->validate();
$this->destroyFiles();
$this->post->delete();
}

Expand All @@ -66,4 +67,12 @@ private function validate(): void
$this->post = $journal->posts()
->findOrFail($this->data['post_id']);
}

private function destroyFiles(): void
{
$files = $this->post->files;
foreach ($files as $file) {
$file->delete();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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

use App\Domains\Contact\ManageDocuments\Services\DestroyFile;
use App\Domains\Contact\ManageDocuments\Services\UploadFile;
use App\Domains\Vault\ManageJournals\Services\AddPhotoToPost;
use App\Domains\Vault\ManageJournals\Web\ViewHelpers\PostEditViewHelper;
use App\Http\Controllers\Controller;
use App\Models\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PostPhotoController extends Controller
{
public function store(Request $request, int $vaultId, int $journalId, int $postId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'uuid' => $request->input('uuid'),
'name' => $request->input('name'),
'original_url' => $request->input('original_url'),
'cdn_url' => $request->input('cdn_url'),
'mime_type' => $request->input('mime_type'),
'size' => $request->input('size'),
'type' => File::TYPE_PHOTO,
];

$file = (new UploadFile())->execute($data);

$post = (new AddPhotoToPost())->execute([
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'journal_id' => $journalId,
'post_id' => $postId,
'file_id' => $file->id,
]);

return response()->json([
'data' => PostEditViewHelper::dtoPhoto($post->journal, $post, $file),
], 200);
}

public function destroy(Request $request, int $vaultId, int $journalId, int $postId, int $fileId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'file_id' => $fileId,
];

(new DestroyFile())->execute($data);

return response()->json([
'data' => true,
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Domains\Vault\ManageJournals\Web\ViewHelpers;

use App\Helpers\DateHelper;
use App\Helpers\FileHelper;
use App\Helpers\PostHelper;
use App\Helpers\StorageHelper;
use App\Models\Contact;
use App\Models\File;
use App\Models\Journal;
use App\Models\Post;
use App\Models\PostSection;
Expand Down Expand Up @@ -50,18 +53,26 @@ public static function data(Journal $journal, Post $post, User $user): array
'name' => $slice->name,
]);

$photos = $post->files()
->where('type', File::TYPE_PHOTO)
->get()
->map(fn (File $file) => self::dtoPhoto($journal, $post, $file));

return [
'id' => $post->id,
'title' => $post->title,
'date' => DateHelper::format($post->written_at, $user),
'editable_date' => $post->written_at->format('Y-m-d'),
'sections' => $sectionsCollection,
'contacts' => $contacts,
'photos' => $photos,
'slice' => $post->sliceOfLife ? self::dtoSlice($journal, $post->sliceOfLife) : null,
'slices' => $slices,
'statistics' => PostHelper::statistics($post),
'tags_in_post' => $tagsAssociatedWithPostCollection,
'tags_in_vault' => $tagsInVaultCollection,
'uploadcarePublicKey' => config('services.uploadcare.public_key'),
'canUploadFile' => StorageHelper::canUploadFile($journal->vault->account),
'journal' => [
'name' => $journal->name,
],
Expand Down Expand Up @@ -91,6 +102,11 @@ public static function data(Journal $journal, Post $post, User $user): array
'journal' => $journal->id,
'post' => $post->id,
]),
'upload_photo' => route('post.photos.store', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
'post' => $post->id,
]),
'back' => route('journal.show', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
Expand Down Expand Up @@ -156,4 +172,23 @@ public static function dtoSlice(Journal $journal, SliceOfLife $slice): array
],
];
}

public static function dtoPhoto(Journal $journal, Post $post, File $file): array
{
return [
'id' => $file->id,
'name' => $file->name,
'size' => FileHelper::formatFileSize($file->size),
'mime_type' => $file->mime_type,
'url' => [
'show' => 'https://ucarecdn.com/'.$file->uuid.'/-/scale_crop/75x75/smart/-/format/auto/-/quality/smart_retina/',
'destroy' => route('post.photos.destroy', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
'post' => $post->id,
'photo' => $file->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Helpers\DateHelper;
use App\Helpers\SliceOfLifeHelper;
use App\Models\Contact;
use App\Models\File;
use App\Models\Post;
use App\Models\PostSection;
use App\Models\Tag;
Expand Down Expand Up @@ -37,6 +38,17 @@ public static function data(Post $post, User $user): array
->get()
->map(fn (Contact $contact) => ContactCardHelper::data($contact));

$photos = $post->files()
->where('type', File::TYPE_PHOTO)
->get()
->map(fn (File $file) => [
'id' => $file->id,
'name' => $file->name,
'url' => [
'display' => 'https://ucarecdn.com/'.$file->uuid.'/-/scale_crop/100x100/smart/-/format/auto/-/quality/smart_retina/',
],
]);

return [
'id' => $post->id,
'title' => $post->title,
Expand All @@ -46,6 +58,7 @@ public static function data(Post $post, User $user): array
'sections' => $sections,
'tags' => $tags,
'contacts' => $contacts,
'photos' => $photos,
'journal' => [
'name' => $post->journal->name,
'url' => [
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -111,6 +112,16 @@ public function tags(): BelongsToMany
return $this->belongsToMany(Tag::class);
}

/**
* Get the files associated with the post.
*
* @return MorphMany
*/
public function files(): MorphMany
{
return $this->morphMany(File::class, 'fileable');
}

/**
* Get the post's title.
*
Expand Down
Loading

0 comments on commit 3ba8712

Please sign in to comment.