-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCollectionsController.php
More file actions
87 lines (64 loc) · 2.46 KB
/
Copy pathCollectionsController.php
File metadata and controls
87 lines (64 loc) · 2.46 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Tv2regionerne\StatamicPrivateApi\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Statamic\Facades;
use Statamic\Http\Controllers\CP\Collections\CollectionsController as CpController;
use Statamic\Query\ItemQueryBuilder;
use Tv2regionerne\StatamicPrivateApi\Http\Resources\CollectionResource;
use Tv2regionerne\StatamicPrivateApi\Traits\VerifiesPrivateAPI;
class CollectionsController extends ApiController
{
use VerifiesPrivateAPI;
public function index()
{
abort_if(! $this->resourcesAllowed('collections', ''), 404);
$query = (new ItemQueryBuilder)->withItems(Facades\Collection::all());
return CollectionResource::collection(
$this->filterSortAndPaginate($query)
);
}
public function show($collection)
{
$collection = $this->collectionFromHandle($collection);
$this->authorize('view', $collection);
return CollectionResource::make($collection);
}
public function store(Request $request)
{
try {
(new CpController($request))->store($request);
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
$collection = $this->collectionFromHandle($request->input('handle'));
return CollectionResource::make($collection);
}
public function update(Request $request, $handle)
{
$collection = $this->collectionFromHandle($handle);
$originalData = collect($collection->fileData())->merge($request->all());
$request->merge($originalData->all());
try {
(new CpController($request))->update($request, $collection);
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
$collection = $this->collectionFromHandle($handle);
return CollectionResource::make($collection);
}
public function destroy(Request $request, $collection)
{
$collection = $this->collectionFromHandle($collection);
return (new CpController($request))->destroy($collection);
}
private function collectionFromHandle($collection)
{
$collection = is_string($collection) ? Facades\Collection::find($collection) : $collection;
if (! $collection) {
abort(404);
}
abort_if(! $this->resourcesAllowed('collections', $collection->handle()), 404);
return $collection;
}
}