MaterialController.php
2.33 KB
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
<?php
namespace App\Http\Container\AdminSection\Controllers\System;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\MaterialRequest;
use App\Models\SystemMaterial;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
class MaterialController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function index(Request $request): Response
{
$pageSize = $request->get('pageSize', 30);
$filter = $request->except('page', 'pageSize');
$data = SystemMaterial::filter($filter)->paginate($pageSize);
return $this->successWithData($data);
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\MaterialRequest $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function store(MaterialRequest $request): Response
{
$attributes = $request->safe()->offsetGet('data');
$ids = [];
foreach ($attributes as $attribute) {
$ids[] = SystemMaterial::query()->create($attribute)?->getKey();
}
OperationLog::admin()->createAction()->subject(new SystemMaterial(['id' => 0]))->content('素材:%s', implode(',', $ids));
return $this->success(ApiCode::CREATE_SUCCESS);
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\MaterialRequest $request
* @param \App\Models\SystemMaterial $material
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(MaterialRequest $request, SystemMaterial $material): Response
{
$material->update($request->validated());
OperationLog::admin()->updateAction()->subject($material)->content('素材:%s', $material->getKey());
return $this->success(ApiCode::UPDATE_SUCCESS);
}
/**
* @param \App\Models\SystemMaterial $material
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function destroy(SystemMaterial $material): Response
{
$material->delete();
OperationLog::admin()->deleteAction()->subject($material)->content('素材:%s', $material->getKey());
return $this->success(ApiCode::DELETE_SUCCESS);
}
}