Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: manage photos (monicahq/chandler#160)
- Loading branch information
Showing
31 changed files
with
1,368 additions
and
29 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManagePhotos\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Models\File; | ||
use App\Services\BaseService; | ||
use Carbon\Carbon; | ||
|
||
class DestroyPhoto extends BaseService implements ServiceInterface | ||
{ | ||
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', | ||
'contact_id' => 'required|integer|exists:contacts,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', | ||
'contact_must_belong_to_vault', | ||
'author_must_be_vault_editor', | ||
]; | ||
} | ||
|
||
/** | ||
* Destroy a file of the photo type. | ||
* | ||
* @param array $data | ||
*/ | ||
public function execute(array $data): void | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
|
||
$this->file->delete(); | ||
|
||
$this->updateLastEditedDate(); | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$this->file = File::where('contact_id', $this->contact->id) | ||
->where('type', File::TYPE_PHOTO) | ||
->findOrFail($this->data['file_id']); | ||
} | ||
|
||
private function updateLastEditedDate(): void | ||
{ | ||
$this->contact->last_updated_at = Carbon::now(); | ||
$this->contact->save(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
domains/Contact/ManagePhotos/Web/Controllers/ContactModulePhotoController.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,60 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManagePhotos\Web\Controllers; | ||
|
||
use App\Contact\ManageDocuments\Services\UploadFile; | ||
use App\Contact\ManagePhotos\Services\DestroyPhoto; | ||
use App\Contact\ManagePhotos\Web\ViewHelpers\ModulePhotosViewHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Contact; | ||
use App\Models\File; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ContactModulePhotoController extends Controller | ||
{ | ||
public function store(Request $request, int $vaultId, int $contactId) | ||
{ | ||
$data = [ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'vault_id' => $vaultId, | ||
'contact_id' => $contactId, | ||
'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); | ||
|
||
$contact = Contact::findOrFail($contactId); | ||
|
||
return response()->json([ | ||
'data' => ModulePhotosViewHelper::dto($file, $contact), | ||
], 201); | ||
} | ||
|
||
public function destroy(Request $request, int $vaultId, int $contactId, int $fileId) | ||
{ | ||
$data = [ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'vault_id' => $vaultId, | ||
'contact_id' => $contactId, | ||
'file_id' => $fileId, | ||
]; | ||
|
||
(new DestroyPhoto())->execute($data); | ||
|
||
return response()->json([ | ||
'data' => route('contact.photo.index', [ | ||
'vault' => $vaultId, | ||
'contact' => $contactId, | ||
]), | ||
], 200); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
domains/Contact/ManagePhotos/Web/Controllers/ContactPhotoController.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,49 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManagePhotos\Web\Controllers; | ||
|
||
use App\Contact\ManagePhotos\Web\ViewHelpers\ContactPhotosIndexViewHelper; | ||
use App\Contact\ManagePhotos\Web\ViewHelpers\ContactPhotosShowViewHelper; | ||
use App\Helpers\PaginatorHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Contact; | ||
use App\Models\File; | ||
use App\Models\Vault; | ||
use App\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class ContactPhotoController extends Controller | ||
{ | ||
public function index(Request $request, int $vaultId, int $contactId) | ||
{ | ||
$vault = Vault::findOrFail($vaultId); | ||
$contact = Contact::findOrFail($contactId); | ||
|
||
$files = File::where('contact_id', $contactId) | ||
->where('type', File::TYPE_PHOTO) | ||
->orderBy('created_at', 'desc') | ||
->paginate(30); | ||
|
||
return Inertia::render('Vault/Contact/Photos/Index', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => ContactPhotosIndexViewHelper::data($files, $contact), | ||
'paginator' => PaginatorHelper::getData($files), | ||
]); | ||
} | ||
|
||
public function show(Request $request, int $vaultId, int $contactId, int $photoId) | ||
{ | ||
$vault = Vault::findOrFail($vaultId); | ||
$contact = Contact::findOrFail($contactId); | ||
|
||
$photo = File::where('contact_id', $contactId) | ||
->where('type', File::TYPE_PHOTO) | ||
->findOrFail($photoId); | ||
|
||
return Inertia::render('Vault/Contact/Photos/Show', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => ContactPhotosShowViewHelper::data($photo, $contact), | ||
]); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
domains/Contact/ManagePhotos/Web/ViewHelpers/ContactPhotosIndexViewHelper.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,61 @@ | ||
<?php | ||
|
||
namespace App\Contact\ManagePhotos\Web\ViewHelpers; | ||
|
||
use App\Helpers\FileHelper; | ||
use App\Helpers\StorageHelper; | ||
use App\Models\Contact; | ||
use App\Models\File; | ||
|
||
class ContactPhotosIndexViewHelper | ||
{ | ||
public static function data($files, Contact $contact): array | ||
{ | ||
$photosCollection = $files->map(function (File $file) use ($contact) { | ||
return self::dto($file, $contact); | ||
}); | ||
|
||
return [ | ||
'contact' => [ | ||
'name' => $contact->name, | ||
], | ||
'photos' => $photosCollection, | ||
'uploadcarePublicKey' => config('services.uploadcare.public_key'), | ||
'canUploadFile' => StorageHelper::canUploadFile($contact->vault->account), | ||
'url' => [ | ||
'show' => route('contact.show', [ | ||
'vault' => $contact->vault_id, | ||
'contact' => $contact->id, | ||
]), | ||
'store' => route('contact.photo.store', [ | ||
'vault' => $contact->vault_id, | ||
'contact' => $contact->id, | ||
]), | ||
], | ||
]; | ||
} | ||
|
||
public static function dto(File $file, Contact $contact): array | ||
{ | ||
return [ | ||
'id' => $file->id, | ||
'name' => $file->name, | ||
'mime_type' => $file->mime_type, | ||
'size' => FileHelper::formatFileSize($file->size), | ||
'url' => [ | ||
'display' => 'https://ucarecdn.com/' . $file->uuid . '/-/scale_crop/400x400/smart/-/format/auto/-/quality/smart_retina/', | ||
'download' => $file->cdn_url, | ||
'show' => route('contact.photo.show', [ | ||
'vault' => $contact->vault_id, | ||
'contact' => $contact->id, | ||
'photo' => $file->id, | ||
]), | ||
'destroy' => route('contact.photo.destroy', [ | ||
'vault' => $contact->vault_id, | ||
'contact' => $contact->id, | ||
'photo' => $file->id, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.