1
|
|
|
@php echo "<?php"; |
|
|
|
|
2
|
|
|
@endphp |
3
|
|
|
|
4
|
|
|
namespace {{ $controllerNamespace }}; |
5
|
|
|
@if($export) |
6
|
|
|
use App\Exports\{{$exportBaseName}}; |
7
|
|
|
use Maatwebsite\Excel\Excel |
8
|
|
|
@endif |
9
|
|
|
use App\Http\Controllers\Controller; |
10
|
|
|
use App\Http\Requests\{{ $modelWithNamespaceFromDefault }}\Index{{ $modelBaseName }}; |
11
|
|
|
use App\Http\Requests\{{ $modelWithNamespaceFromDefault }}\Store{{ $modelBaseName }}; |
12
|
|
|
use App\Http\Requests\{{ $modelWithNamespaceFromDefault }}\Update{{ $modelBaseName }}; |
13
|
|
|
use App\Http\Requests\{{ $modelWithNamespaceFromDefault }}\Destroy{{ $modelBaseName }}; |
14
|
|
|
use {{$modelFullName}}; |
15
|
|
|
use {{$repoFullName}}; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use Savannabits\JetstreamInertiaGenerator\Helpers\ApiResponse; |
18
|
|
|
use Savannabits\Pagetables\Column; |
19
|
|
|
use Savannabits\Pagetables\Pagetables; |
20
|
|
|
use Yajra\DataTables\DataTables; |
21
|
|
|
|
22
|
|
|
class {{ $controllerBaseName }} extends Controller |
23
|
|
|
{ |
24
|
|
|
private ApiResponse $api; |
25
|
|
|
private {{$repoBaseName}} $repo; |
26
|
|
|
public function __construct(ApiResponse $apiResponse, {{$repoBaseName}} $repo) |
27
|
|
|
{ |
28
|
|
|
$this->api = $apiResponse; |
29
|
|
|
$this->repo = $repo; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Display a listing of the resource (paginated). |
34
|
|
|
* {{"@"}}retcolumnsToQueryurn \Illuminate\Http\JsonResponse |
35
|
|
|
*/ |
36
|
|
|
public function index(Index{{ $modelBaseName }} $request) |
37
|
|
|
{ |
38
|
|
|
$query = {{$modelBaseName}}::query(); // You can extend this however you want. |
39
|
|
|
$cols = [ |
40
|
|
|
@foreach($columnsToQuery as $col)Column::name('{{$col}}')->title('{{str_replace('_',' ',Str::title($col))}}')->sort()->searchable(), |
41
|
|
|
@endforeach |
42
|
|
|
|
43
|
|
|
Column::name('actions')->title('')->raw() |
44
|
|
|
]; |
45
|
|
|
$data = Pagetables::of($query)->columns($cols)->make(true); |
46
|
|
|
return $this->api->success()->message("List of {{$modelPlural}}")->payload($data)->send(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function dt(Request $request) { |
50
|
|
|
$query = {{$modelBaseName}}::query()->select({{$modelBaseName}}::getModel()->getTable().'.*'); // You can extend this however you want. |
51
|
|
|
return $this->repo::dt($query); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* Store a newly created resource in storage. |
55
|
|
|
* |
56
|
|
|
* {{"@"}}param Store{{$modelBaseName}} $request |
57
|
|
|
* {{"@"}}return \Illuminate\Http\JsonResponse |
58
|
|
|
*/ |
59
|
|
|
public function store(Store{{$modelBaseName}} $request) |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$data = $request->sanitizedObject(); |
63
|
|
|
${{$modelVariableName}} = $this->repo::store($data); |
64
|
|
|
return $this->api->success()->message('{{$modelTitle}} Created')->payload(${{$modelVariableName}})->send(); |
65
|
|
|
} catch (\Throwable $exception) { |
66
|
|
|
\Log::error($exception); |
67
|
|
|
return $this->api->failed()->message($exception->getMessage())->payload([])->code(500)->send(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Display the specified resource. |
73
|
|
|
* |
74
|
|
|
* {{"@"}}param Request $request |
75
|
|
|
* {{"@"}}param {{$modelBaseName}} ${{$modelVariableName}} |
76
|
|
|
* {{"@"}}return \Illuminate\Http\JsonResponse |
77
|
|
|
*/ |
78
|
|
|
public function show(Request $request, {{$modelBaseName}} ${{$modelVariableName}}) |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$payload = $this->repo::init(${{$modelVariableName}})->show($request); |
82
|
|
|
return $this->api->success() |
83
|
|
|
->message("{{$modelTitle}} ${{$modelVariableName}}->id") |
84
|
|
|
->payload($payload)->send(); |
85
|
|
|
} catch (\Throwable $exception) { |
86
|
|
|
\Log::error($exception); |
87
|
|
|
return $this->api->failed()->message($exception->getMessage())->send(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Update the specified resource in storage. |
93
|
|
|
* |
94
|
|
|
* {{"@"}}param Update{{$modelBaseName}} $request |
95
|
|
|
* {{"@"}}param {$modelBaseName} ${{$modelVariableName}} |
96
|
|
|
* {{"@"}}return \Illuminate\Http\JsonResponse |
97
|
|
|
*/ |
98
|
|
|
public function update(Update{{$modelBaseName}} $request, {{$modelBaseName}} ${{$modelVariableName}}) |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$data = $request->sanitizedObject(); |
102
|
|
|
$res = $this->repo::init(${{$modelVariableName}})->update($data); |
103
|
|
|
return $this->api->success()->message("{{$modelTitle}} has been updated")->payload($res)->code(200)->send(); |
104
|
|
|
} catch (\Throwable $exception) { |
105
|
|
|
\Log::error($exception); |
106
|
|
|
return $this->api->failed()->code(400)->message($exception->getMessage())->send(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Remove the specified resource from storage. |
112
|
|
|
* |
113
|
|
|
* {{"@"}}param {{$modelBaseName}} ${{$modelVariableName}} |
114
|
|
|
* {{"@"}}return \Illuminate\Http\JsonResponse |
115
|
|
|
* {{"@"}}throws \Exception |
116
|
|
|
*/ |
117
|
|
|
public function destroy(Destroy{{$modelBaseName}} $request, {{$modelBaseName}} ${{$modelVariableName}}) |
118
|
|
|
{ |
119
|
|
|
$res = $this->repo::init(${{$modelVariableName}})->destroy(); |
120
|
|
|
return $this->api->success()->message("{{$modelTitle}} has been deleted")->payload($res)->code(200)->send(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
@if($export) |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Export entities |
127
|
|
|
* |
128
|
|
|
* {{'@'}}return BinaryFileResponse|null |
129
|
|
|
*/ |
130
|
|
|
public function export(): ?BinaryFileResponse |
131
|
|
|
{ |
132
|
|
|
return Excel::download(app({{ $exportBaseName }}::class), '{{ str_plural($modelVariableName) }}.xlsx'); |
133
|
|
|
} |
134
|
|
|
@endif |
135
|
|
|
} |
136
|
|
|
|