1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* BillController.php |
5
|
|
|
* Copyright (c) 2018 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of Firefly III. |
8
|
|
|
* |
9
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Api\V1\Controllers; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Api\V1\Requests\BillRequest; |
26
|
|
|
use FireflyIII\Models\Bill; |
27
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface; |
28
|
|
|
use FireflyIII\Transformers\BillTransformer; |
29
|
|
|
use Illuminate\Http\Request; |
30
|
|
|
use Illuminate\Support\Collection; |
31
|
|
|
use League\Fractal\Manager; |
32
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
33
|
|
|
use League\Fractal\Resource\Collection as FractalCollection; |
34
|
|
|
use League\Fractal\Resource\Item; |
35
|
|
|
use League\Fractal\Serializer\JsonApiSerializer; |
36
|
|
|
use Preferences; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class BillController |
40
|
|
|
*/ |
41
|
|
|
class BillController extends Controller |
42
|
|
|
{ |
43
|
|
|
/** @var BillRepositoryInterface */ |
44
|
|
|
private $repository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* BillController constructor. |
48
|
|
|
* |
49
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
parent::__construct(); |
54
|
|
|
$this->middleware( |
55
|
|
|
function ($request, $next) { |
56
|
|
|
/** @var BillRepositoryInterface repository */ |
57
|
|
|
$this->repository = app(BillRepositoryInterface::class); |
58
|
|
|
$this->repository->setUser(auth()->user()); |
59
|
|
|
|
60
|
|
|
return $next($request); |
61
|
|
|
} |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove the specified resource from storage. |
67
|
|
|
* |
68
|
|
|
* @param \FireflyIII\Models\Bill $bill |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Http\Response |
71
|
|
|
*/ |
72
|
|
|
public function delete(Bill $bill) |
73
|
|
|
{ |
74
|
|
|
$this->repository->destroy($bill); |
75
|
|
|
|
76
|
|
|
return response()->json([], 204); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Display a listing of the resource. |
81
|
|
|
* |
82
|
|
|
* @param Request $request |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Http\JsonResponse |
85
|
|
|
*/ |
86
|
|
|
public function index(Request $request) |
87
|
|
|
{ |
88
|
|
|
$pageSize = (int)Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data; |
|
|
|
|
89
|
|
|
$paginator = $this->repository->getPaginator($pageSize); |
90
|
|
|
/** @var Collection $bills */ |
91
|
|
|
$bills = $paginator->getCollection(); |
92
|
|
|
|
93
|
|
|
$manager = new Manager(); |
94
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
95
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
96
|
|
|
|
97
|
|
|
$resource = new FractalCollection($bills, new BillTransformer($this->parameters), 'bills'); |
98
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
99
|
|
|
|
100
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Request $request |
106
|
|
|
* @param Bill $bill |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Http\JsonResponse |
109
|
|
|
*/ |
110
|
|
|
public function show(Request $request, Bill $bill) |
111
|
|
|
{ |
112
|
|
|
$manager = new Manager(); |
113
|
|
|
// add include parameter: |
114
|
|
|
$include = $request->get('include') ?? ''; |
115
|
|
|
$manager->parseIncludes($include); |
116
|
|
|
|
117
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
118
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
119
|
|
|
|
120
|
|
|
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); |
121
|
|
|
|
122
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param BillRequest $request |
127
|
|
|
* |
128
|
|
|
* @return \Illuminate\Http\JsonResponse |
129
|
|
|
*/ |
130
|
|
|
public function store(BillRequest $request) |
131
|
|
|
{ |
132
|
|
|
$bill = $this->repository->store($request->getAll()); |
133
|
|
|
$manager = new Manager(); |
134
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
135
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
136
|
|
|
|
137
|
|
|
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); |
138
|
|
|
|
139
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param BillRequest $request |
146
|
|
|
* @param Bill $bill |
147
|
|
|
* |
148
|
|
|
* @return \Illuminate\Http\JsonResponse |
149
|
|
|
*/ |
150
|
|
|
public function update(BillRequest $request, Bill $bill) |
151
|
|
|
{ |
152
|
|
|
$data = $request->getAll(); |
153
|
|
|
$bill = $this->repository->update($bill, $data); |
154
|
|
|
$manager = new Manager(); |
155
|
|
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
156
|
|
|
$manager->setSerializer(new JsonApiSerializer($baseUrl)); |
157
|
|
|
|
158
|
|
|
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); |
159
|
|
|
|
160
|
|
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|