Skip to content

Commit b977da7

Browse files
committed
feature: Batch Actions
1 parent 22fa6d0 commit b977da7

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace Phpsa\LaravelApiController\Http\Api\Contracts;
3+
4+
use Illuminate\Support\Facades\DB;
5+
use Phpsa\LaravelApiController\Exceptions\ApiException;
6+
7+
trait HasBatchActions
8+
{
9+
10+
public function handleBatchStoreAction($request, array $extraParams)
11+
{
12+
$this->validateRequestType($request);
13+
$this->addCustomParams($extraParams);
14+
$this->authoriseUserAction('create');
15+
16+
$this->validate($this->request, $this->rulesForBatchCreate());
17+
18+
DB::beginTransaction();
19+
20+
$this->unguardIfNeeded();
21+
22+
try {
23+
$records = collect($this->request->get('data'), [])->map(function ($item) {
24+
$data = $this->qualifyStoreQuery($item);
25+
$insert = $this->addTableData($data);
26+
$diff = array_diff(array_keys($data), array_keys($insert));
27+
$item = self::$model->create($insert);
28+
29+
$this->storeRelated($item, $diff, $data);
30+
31+
return $item;
32+
});
33+
34+
DB::commit();
35+
36+
return $this->handleStoreResponse($records);
37+
} catch (\Exception $exception) {
38+
$message = config('app.debug') ? $exception->getMessage() : 'Failed to create Records';
39+
40+
DB::rollback();
41+
throw new ApiException($message, (int) $exception->getCode(), $exception);
42+
}
43+
}
44+
45+
46+
protected function handleBatchStoreResponse($items)
47+
{
48+
return $this->respondWithResource($this->/** @scrutinizer ignore-call */getResourceCollection(), $items, 201);
49+
}
50+
51+
protected function handleBatchUpdateResponse($items)
52+
{
53+
return $this->respondWithResource($this->/** @scrutinizer ignore-call */getResourceCollection(), $items, 200);
54+
}
55+
56+
protected function rulesForBatchCreate() : array
57+
{
58+
return [];
59+
}
60+
61+
protected function rulesForBatchUpdate() : array
62+
{
63+
return [];
64+
}
65+
}

src/Http/Api/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ public function handleStoreAction($request, array $extraParams = [])
135135
$this->addCustomParams($extraParams);
136136
$this->authoriseUserAction('create');
137137

138-
$this->validate($request, $this->rulesForCreate());
138+
$this->validate($this->request, $this->rulesForCreate());
139139

140-
$data = $this->qualifyStoreQuery($request->all());
140+
$data = $this->qualifyStoreQuery($this->request->all());
141141

142142
$insert = $this->addTableData($data);
143143

@@ -152,7 +152,7 @@ public function handleStoreAction($request, array $extraParams = [])
152152

153153
$this->storeRelated($item, $diff, $data);
154154

155-
event(new Created($item, $request));
155+
event(new Created($item, $this->request));
156156

157157
DB::commit();
158158

0 commit comments

Comments
 (0)