-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add photos to posts (monicahq/chandler#382)
- Loading branch information
Showing
13 changed files
with
635 additions
and
4 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
app/Domains/Vault/ManageJournals/Services/AddPhotoToPost.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,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']); | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
app/Domains/Vault/ManageJournals/Web/Controllers/PostPhotoController.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\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); | ||
} | ||
} |
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
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.