Skip to content

Commit

Permalink
feat: edit and delete a group (monicahq/chandler#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored May 6, 2023
1 parent 24786cd commit 543ba8f
Show file tree
Hide file tree
Showing 20 changed files with 445 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

namespace App\Domains\Contact\ManageGroups\Web\Controllers;

use App\Domains\Contact\ManageGroups\Services\DestroyGroup;
use App\Domains\Contact\ManageGroups\Services\UpdateGroup;
use App\Domains\Contact\ManageGroups\Web\ViewHelpers\GroupEditViewHelper;
use App\Domains\Contact\ManageGroups\Web\ViewHelpers\GroupIndexViewHelper;
use App\Domains\Contact\ManageGroups\Web\ViewHelpers\GroupShowViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Models\Vault;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Inertia\Inertia;
use Inertia\Response;

class GroupController extends Controller
{
public function index(Request $request, string $vaultId)
public function index(Request $request, string $vaultId): Response
{
$vault = Vault::findOrFail($vaultId);

Expand All @@ -24,7 +30,7 @@ public function index(Request $request, string $vaultId)
]);
}

public function show(Request $request, string $vaultId, int $groupId)
public function show(Request $request, string $vaultId, int $groupId): Response
{
$vault = Vault::findOrFail($vaultId);
$group = Group::with([
Expand All @@ -34,7 +40,66 @@ public function show(Request $request, string $vaultId, int $groupId)

return Inertia::render('Vault/Group/Show', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => GroupShowViewHelper::data($group, Auth::user()),
'data' => GroupShowViewHelper::data($group),
]);
}

public function edit(Request $request, string $vaultId, int $groupId): Response
{
Gate::authorize('vault-editor', $vaultId);

$vault = Vault::findOrFail($vaultId);
$group = Group::with([
'contacts',
'groupType',
])->findOrFail($groupId);

return Inertia::render('Vault/Group/Edit', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => GroupEditViewHelper::data($group),
]);
}

public function update(Request $request, string $vaultId, string $groupId)
{
Gate::authorize('vault-editor', $vaultId);

$data = [
'account_id' => Auth::user()->account_id,
'vault_id' => $vaultId,
'author_id' => Auth::id(),
'group_id' => $groupId,
'group_type_id' => $request->input('group_type_id'),
'name' => $request->input('name'),
];

$group = (new UpdateGroup())->execute($data);

return response()->json([
'data' => route('group.show', [
'vault' => $vaultId,
'group' => $group,
]),
], 200);
}

public function destroy(Request $request, string $vaultId, string $groupId): JsonResponse
{
Gate::authorize('vault-editor', $vaultId);

$data = [
'account_id' => Auth::user()->account_id,
'vault_id' => $vaultId,
'author_id' => Auth::id(),
'group_id' => $groupId,
];

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

return response()->json([
'data' => route('group.index', [
'vault' => $vaultId,
]),
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Domains\Contact\ManageGroups\Web\ViewHelpers;

use App\Models\Group;
use App\Models\GroupType;

class GroupEditViewHelper
{
public static function data(Group $group): array
{
$groupTypes = $group->vault->account->groupTypes()
->orderBy('position')
->get()
->map(fn (GroupType $groupType) => [
'id' => $groupType->id,
'name' => $groupType->label,
]);

return [
'id' => $group->id,
'name' => $group->name,
'group_type_id' => $group->group_type_id,
'group_types' => $groupTypes,
'url' => [
'back' => route('group.show', [
'vault' => $group->vault_id,
'group' => $group->id,
]),
'update' => route('group.update', [
'vault' => $group->vault_id,
'group' => $group->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Contact;
use App\Models\Group;
use App\Models\GroupTypeRole;
use App\Models\User;

class GroupShowViewHelper
{
Expand All @@ -16,7 +15,7 @@ class GroupShowViewHelper
* So we need to group contacts by roles if they exist, or list them
* alphabetically otherwise.
*/
public static function data(Group $group, User $user): array
public static function data(Group $group): array
{
$rolesCollection = $group->groupType->groupTypeRoles()
->orderBy('position')
Expand Down Expand Up @@ -75,6 +74,16 @@ public static function data(Group $group, User $user): array
'label' => $group->groupType->label,
],
'roles' => $rolesCollection,
'url' => [
'edit' => route('group.edit', [
'vault' => $group->vault_id,
'group' => $group->id,
]),
'destroy' => route('group.destroy', [
'vault' => $group->vault_id,
'group' => $group->id,
]),
],
];
}
}
5 changes: 5 additions & 0 deletions lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"deleted a note": "hat eine Notiz gelöscht",
"deleted a pet": "hat ein Haustier gelöscht",
"Deleted author": "Autor gelöscht",
"Delete group": "Gruppe löschen",
"Delete journal": "Tagebuch löschen",
"Delete Team": "Team löschen",
"Delete the address": "Adresse löschen",
Expand Down Expand Up @@ -379,6 +380,7 @@
"Edit a journal": "Ein Journal bearbeiten",
"Edit a post": "Beitrag bearbeiten",
"edited a note": "hat eine Notiz bearbeitet",
"Edit group": "Gruppe bearbeiten",
"Edit journal": "Tagebuch bearbeiten",
"Edit journal information": "Tagebuchinformationen bearbeiten",
"Edit journal metrics": "Tagebuch-Metriken bearbeiten",
Expand All @@ -388,6 +390,7 @@
"Edit post": "Beitrag bearbeiten",
"Edit Profile": "Profil bearbeiten",
"Edit slice of life": "Schnitzel des Lebens bearbeiten",
"Edit the group": "Die Gruppe bearbeiten",
"Edit the slice of life": "Das Schnitzel des Lebens bearbeiten",
"Email": "E-Mail",
"Email address": "E-Mail-Adresse",
Expand Down Expand Up @@ -921,6 +924,8 @@
"The goal has been deleted": "Das Ziel wurde gelöscht",
"The goal has been edited": "Das Ziel wurde bearbeitet",
"The group has been added": "Die Gruppe wurde hinzugefügt",
"The group has been deleted": "Die Gruppe wurde gelöscht",
"The group has been updated": "Die Gruppe wurde aktualisiert",
"The group type has been created": "Der Gruppentyp wurde erstellt",
"The group type has been deleted": "Der Gruppentyp wurde gelöscht",
"The group type has been updated": "Der Gruppentyp wurde aktualisiert",
Expand Down
5 changes: 0 additions & 5 deletions lang/de/vault.php

This file was deleted.

5 changes: 5 additions & 0 deletions lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"deleted a note": "eliminó una nota",
"deleted a pet": "eliminó una mascota",
"Deleted author": "Autor eliminado",
"Delete group": "Eliminar grupo",
"Delete journal": "Eliminar diario",
"Delete Team": "Borrar equipo",
"Delete the address": "Eliminar la dirección",
Expand Down Expand Up @@ -379,6 +380,7 @@
"Edit a journal": "Editar un diario",
"Edit a post": "Editar una publicación.",
"edited a note": "editó una nota",
"Edit group": "Editar grupo",
"Edit journal": "Editar diario",
"Edit journal information": "Editar información del diario",
"Edit journal metrics": "Editar métricas del diario",
Expand All @@ -388,6 +390,7 @@
"Edit post": "Editar publicación.",
"Edit Profile": "Editar perfil",
"Edit slice of life": "Editar fragmento de vida.",
"Edit the group": "Editar el grupo",
"Edit the slice of life": "Editar el fragmento de vida.",
"Email": "Correo electrónico",
"Email address": "Dirección de correo electrónico",
Expand Down Expand Up @@ -921,6 +924,8 @@
"The goal has been deleted": "La meta ha sido eliminada",
"The goal has been edited": "La meta ha sido editada",
"The group has been added": "El grupo ha sido añadido",
"The group has been deleted": "El grupo ha sido eliminado",
"The group has been updated": "El grupo ha sido actualizado",
"The group type has been created": "El tipo de grupo ha sido creado",
"The group type has been deleted": "El tipo de grupo ha sido eliminado",
"The group type has been updated": "El tipo de grupo ha sido actualizado",
Expand Down
5 changes: 0 additions & 5 deletions lang/es/vault.php

This file was deleted.

5 changes: 5 additions & 0 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"deleted a note": "a supprimé une note",
"deleted a pet": "a supprimé un animal de compagnie",
"Deleted author": "Auteur supprimé",
"Delete group": "Supprimer le groupe",
"Delete journal": "Supprimer le journal",
"Delete Team": "Supprimer l’équipe",
"Delete the address": "Supprimer l’adresse",
Expand Down Expand Up @@ -379,6 +380,7 @@
"Edit a journal": "Modifier un journal",
"Edit a post": "Modifier une publication",
"edited a note": "a modifié une note",
"Edit group": "Modifier le groupe",
"Edit journal": "Modifier le journal",
"Edit journal information": "Modifier les informations du journal",
"Edit journal metrics": "Modifier les métriques du journal",
Expand All @@ -388,6 +390,7 @@
"Edit post": "Modifier la publication",
"Edit Profile": "Éditer le profil",
"Edit slice of life": "Modifier la tranche de vie",
"Edit the group": "Modifier le groupe",
"Edit the slice of life": "Modifier la tranche de vie",
"Email": "E-mail",
"Email address": "Adresse email",
Expand Down Expand Up @@ -921,6 +924,8 @@
"The goal has been deleted": "L’objectif a été supprimé",
"The goal has been edited": "L’objectif a été modifié",
"The group has been added": "Le groupe a été ajouté",
"The group has been deleted": "Le groupe a été supprimé",
"The group has been updated": "Le groupe a été mis à jour",
"The group type has been created": "Le type de groupe a été créé",
"The group type has been deleted": "Le type de groupe a été supprimé",
"The group type has been updated": "Le type de groupe a été mis à jour",
Expand Down
5 changes: 0 additions & 5 deletions lang/fr/vault.php

This file was deleted.

5 changes: 5 additions & 0 deletions lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"deleted a note": "ha eliminato una nota",
"deleted a pet": "ha eliminato un animale domestico",
"Deleted author": "Autore eliminato",
"Delete group": "Elimina gruppo",
"Delete journal": "Elimina diario",
"Delete Team": "Elimina Team",
"Delete the address": "Elimina l'indirizzo",
Expand Down Expand Up @@ -379,6 +380,7 @@
"Edit a journal": "Modifica un diario",
"Edit a post": "Modifica un post",
"edited a note": "ha modificato una nota",
"Edit group": "Modifica gruppo",
"Edit journal": "Modifica diario",
"Edit journal information": "Modifica informazioni diario",
"Edit journal metrics": "Modifica metriche diario",
Expand All @@ -388,6 +390,7 @@
"Edit post": "Modifica post",
"Edit Profile": "Modifica Profilo",
"Edit slice of life": "Modifica pezzo di vita",
"Edit the group": "Modifica il gruppo",
"Edit the slice of life": "Modifica il pezzo di vita",
"Email": "Email",
"Email address": "Indirizzo email",
Expand Down Expand Up @@ -921,6 +924,8 @@
"The goal has been deleted": "L'obiettivo è stato eliminato",
"The goal has been edited": "L'obiettivo è stato modificato",
"The group has been added": "Il gruppo è stato aggiunto",
"The group has been deleted": "Il gruppo è stato eliminato",
"The group has been updated": "Il gruppo è stato aggiornato",
"The group type has been created": "Il tipo di gruppo è stato creato.",
"The group type has been deleted": "Il tipo di gruppo è stato eliminato.",
"The group type has been updated": "Il tipo di gruppo è stato aggiornato.",
Expand Down
5 changes: 0 additions & 5 deletions lang/it/vault.php

This file was deleted.

5 changes: 5 additions & 0 deletions lang/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"deleted a note": "excluiu uma nota",
"deleted a pet": "excluiu um animal de estimação",
"Deleted author": "Autor excluído",
"Delete group": "Excluir grupo",
"Delete journal": "Excluir diário",
"Delete Team": "Eliminar Equipa",
"Delete the address": "Excluir o endereço",
Expand Down Expand Up @@ -379,6 +380,7 @@
"Edit a journal": "Editar um diário",
"Edit a post": "Editar um post",
"edited a note": "editou uma nota",
"Edit group": "Editar grupo",
"Edit journal": "Editar diário",
"Edit journal information": "Editar informações do diário",
"Edit journal metrics": "Editar métricas do diário",
Expand All @@ -388,6 +390,7 @@
"Edit post": "Editar post",
"Edit Profile": "Editar Perfil",
"Edit slice of life": "Editar pedacinho da vida",
"Edit the group": "Editar o grupo",
"Edit the slice of life": "Editar o pedacinho da vida",
"Email": "E-mail",
"Email address": "Endereço de email",
Expand Down Expand Up @@ -921,6 +924,8 @@
"The goal has been deleted": "A meta foi excluída",
"The goal has been edited": "A meta foi editada",
"The group has been added": "O grupo foi adicionado",
"The group has been deleted": "O grupo foi excluído",
"The group has been updated": "O grupo foi atualizado",
"The group type has been created": "O tipo de grupo foi criado",
"The group type has been deleted": "O tipo de grupo foi excluído",
"The group type has been updated": "O tipo de grupo foi atualizado",
Expand Down
5 changes: 0 additions & 5 deletions lang/pt/vault.php

This file was deleted.

Loading

0 comments on commit 543ba8f

Please sign in to comment.