-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserController.php
245 lines (213 loc) · 6 KB
/
UserController.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\User as DataDb;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Http\Requests\UserStoreRequest as StoreValidation;
use App\Http\Requests\UserUpdateRequest as UpdateValidation;
use App\Http\Requests\UserImportRequest as ImportValidation;
use App\Exports\UserExport;
use App\Exports\UserImportTemplate;
use App\Imports\UserImport;
use Maatwebsite\Excel\Facades\Excel;
class UserController extends Controller
{
/**
* The url of resources.
*
* @var string
*/
private $type;
/**
* __construct function.
*/
public function __construct()
{
$this->type = 'user';
}
/**
* Show list resource.
*
* @param Request $request
*/
public function index(Request $request)
{
if ($request->ajax()) {
return datatables(DataDb::query())
->addIndexColumn()
->editColumn('created_at', function ($row) {
return [
'display' => $row->created_at->isoFormat('DD MMMM Y HH:mm:ss'),
'timestamp' => $row->created_at->timestamp,
];
})
->editColumn('updated_at', function ($row) {
return [
'display' => $row->updated_at->isoFormat('DD MMMM Y HH:mm:ss'),
'timestamp' => $row->updated_at->timestamp,
];
})
->toJson();
}
return view($this->type . '.index', [
'type' => $this->type,
]);
}
/**
* Show the form for creating a new resource.
*
*/
public function create()
{
return view($this->type . '.create', [
'type' => $this->type,
]);
}
/**
* Store a newly created resource in storage.
*
* @param StoreValidation $request
*/
public function store(StoreValidation $request)
{
try {
DB::beginTransaction();
if ($request->ajax()) {
$request->validated();
$data = new DataDb();
$data->uuid = (string) Str::uuid();
$data->name = $request->name;
$data->username = $request->username;
$data->phone = $request->phone;
$data->email = $request->email;
$data->password = $request->password;
if ($request->has('status')) {
$data->is_active = '1';
$data->activated_at = now()->toDateTimeString();
} else {
$data->is_active = '0';
$data->deactivated_at = now()->toDateTimeString();
}
$data->save();
DB::commit();
return response()->json(['success' => 'save success']);
}
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* Display the specified resource.
*
* @param DataDb $data
*/
public function show(DataDb $data)
{
return view($this->type . '.show', [
'type' => $this->type,
'data' => $data,
]);
}
/**
* Show the form for editing the specified resource.
*
* @param DataDb $data
*/
public function edit(DataDb $data)
{
return view($this->type . '.edit', [
'type' => $this->type,
'data' => $data,
]);
}
/**
* Update the specified resource in storage.
*
* @param UpdateValidation $request
* @param DataDb $data
*/
public function update(UpdateValidation $request, DataDb $data)
{
try {
DB::beginTransaction();
if ($request->ajax()) {
$request->validated();
$data->name = $request->name;
$data->username = $request->username;
$data->phone = $request->phone;
$data->email = $request->email;
if ($request->has('password')) {
$data->password = $request->password;
}
if ($request->has('status')) {
$data->is_active = '1';
$data->activated_at = now()->toDateTimeString();
} else {
$data->is_active = '0';
$data->deactivated_at = now()->toDateTimeString();
}
$data->save();
DB::commit();
return response()->json(['success' => 'update success']);
}
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
/**
* Remove the specified resource from storage.
*
* @param DataDb $data
*/
public function destroy(DataDb $data)
{
// delete book
$data->delete();
return response()->json(['success' => 'delete success']);
}
/**
* export data.
*/
public function export()
{
return new UserExport($this->type);
}
/**
* import Template excel data.
*/
public function importTemplate()
{
return new UserImportTemplate($this->type);
}
/**
* import form.
*/
public function importForm()
{
return view($this->type . '.import', [
'type' => $this->type,
]);
}
/**
* store import data.
*/
public function importStore(ImportValidation $request)
{
try {
if ($request->ajax()) {
$request->validated();
Excel::import(new UserImport(), $request->file('file'));
return response()->json(['success' => 'update success']);
}
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
throw $e;
} catch (\Exception $e) {
throw $e;
}
}
}