Skip to content

Commit

Permalink
feat: manage photos (monicahq/chandler#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Jul 29, 2022
1 parent ff581a8 commit 57def1f
Show file tree
Hide file tree
Showing 31 changed files with 1,368 additions and 29 deletions.
16 changes: 16 additions & 0 deletions app/Jobs/SetupAccount.php
Expand Up @@ -442,6 +442,22 @@ private function addTemplatePageInformation(): void
'module_id' => $module->id,
]);

// Documents
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'name' => trans('app.module_photos'),
'type' => Module::TYPE_PHOTOS,
'can_be_deleted' => false,
]);
(new AssociateModuleToTemplatePage())->execute([
'account_id' => $this->user->account_id,
'author_id' => $this->user->id,
'template_id' => $this->template->id,
'template_page_id' => $templatePageInformation->id,
'module_id' => $module->id,
]);

// Notes
$module = (new CreateModule())->execute([
'account_id' => $this->user->account_id,
Expand Down
1 change: 1 addition & 0 deletions app/Models/Module.php
Expand Up @@ -35,6 +35,7 @@ class Module extends Model
public const TYPE_GROUPS = 'groups';
public const TYPE_CONTACT_INFORMATION = 'contact_information';
public const TYPE_DOCUMENTS = 'documents';
public const TYPE_PHOTOS = 'photos';

/**
* The attributes that are mass assignable.
Expand Down
27 changes: 14 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -18,6 +18,7 @@
use App\Contact\ManageLoans\Web\ViewHelpers\ModuleLoanViewHelper;
use App\Contact\ManageNotes\Web\ViewHelpers\ModuleNotesViewHelper;
use App\Contact\ManagePets\Web\ViewHelpers\ModulePetsViewHelper;
use App\Contact\ManagePhotos\Web\ViewHelpers\ModulePhotosViewHelper;
use App\Contact\ManagePronouns\Web\ViewHelpers\ModuleGenderPronounViewHelper;
use App\Contact\ManageRelationships\Web\ViewHelpers\ModuleFamilySummaryViewHelper;
use App\Contact\ManageRelationships\Web\ViewHelpers\ModuleRelationshipViewHelper;
Expand Down Expand Up @@ -242,6 +243,10 @@ public static function modules(TemplatePage $page, Contact $contact, User $user)
$data = ModuleDocumentsViewHelper::data($contact);
}

if ($module->type == Module::TYPE_PHOTOS) {
$data = ModulePhotosViewHelper::data($contact);
}

$modulesCollection->push([
'id' => $module->id,
'type' => $module->type,
Expand Down
Expand Up @@ -13,6 +13,7 @@ public static function data(Contact $contact): array
{
$documentsCollection = $contact->files()
->where('type', File::TYPE_DOCUMENT)
->orderBy('created_at', 'desc')
->get()
->map(function (File $file) use ($contact) {
return self::dto($file, $contact);
Expand All @@ -35,11 +36,11 @@ public static function dto(File $file, Contact $contact): array
{
return [
'id' => $file->id,
'download_url' => $file->cdn_url,
'name' => $file->name,
'mime_type' => $file->mime_type,
'size' => FileHelper::formatFileSize($file->size),
'url' => [
'download' => $file->cdn_url,
'destroy' => route('contact.document.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
Expand Down
74 changes: 74 additions & 0 deletions domains/Contact/ManagePhotos/Services/DestroyPhoto.php
@@ -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();
}
}
@@ -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);
}
}
@@ -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),
]);
}
}
@@ -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,
]),
],
];
}
}

0 comments on commit 57def1f

Please sign in to comment.