-
-
Notifications
You must be signed in to change notification settings - Fork 743
/
OrganizationsController.php
107 lines (94 loc) · 3.67 KB
/
OrganizationsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace App\Http\Controllers;
use App\Models\Organization;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
use Inertia\Response;
class OrganizationsController extends Controller
{
public function index(): Response
{
return Inertia::render('Organizations/Index', [
'filters' => Request::all('search', 'trashed'),
'organizations' => Auth::user()->account->organizations()
->orderBy('name')
->filter(Request::only('search', 'trashed'))
->paginate(10)
->withQueryString()
->through(fn ($organization) => [
'id' => $organization->id,
'name' => $organization->name,
'phone' => $organization->phone,
'city' => $organization->city,
'deleted_at' => $organization->deleted_at,
]),
]);
}
public function create(): Response
{
return Inertia::render('Organizations/Create');
}
public function store(): RedirectResponse
{
Auth::user()->account->organizations()->create(
Request::validate([
'name' => ['required', 'max:100'],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
return Redirect::route('organizations')->with('success', 'Organization created.');
}
public function edit(Organization $organization): Response
{
return Inertia::render('Organizations/Edit', [
'organization' => [
'id' => $organization->id,
'name' => $organization->name,
'email' => $organization->email,
'phone' => $organization->phone,
'address' => $organization->address,
'city' => $organization->city,
'region' => $organization->region,
'country' => $organization->country,
'postal_code' => $organization->postal_code,
'deleted_at' => $organization->deleted_at,
'contacts' => $organization->contacts()->orderByName()->get()->map->only('id', 'name', 'city', 'phone'),
],
]);
}
public function update(Organization $organization): RedirectResponse
{
$organization->update(
Request::validate([
'name' => ['required', 'max:100'],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
return Redirect::back()->with('success', 'Organization updated.');
}
public function destroy(Organization $organization): RedirectResponse
{
$organization->delete();
return Redirect::back()->with('success', 'Organization deleted.');
}
public function restore(Organization $organization): RedirectResponse
{
$organization->restore();
return Redirect::back()->with('success', 'Organization restored.');
}
}