mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Implement link type controller.
This commit is contained in:
parent
7749fb1a0b
commit
234e3f4ca5
197
app/Api/V1/Controllers/LinkTypeController.php
Normal file
197
app/Api/V1/Controllers/LinkTypeController.php
Normal file
@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/**
|
||||
* LinkTypeController.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Controllers;
|
||||
|
||||
use FireflyIII\Api\V1\Requests\LinkTypeRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\LinkType;
|
||||
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\Transformers\LinkTypeTransformer;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use League\Fractal\Manager;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\Serializer\JsonApiSerializer;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class LinkTypeController
|
||||
*/
|
||||
class LinkTypeController extends Controller
|
||||
{
|
||||
/** @var LinkTypeRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/** @var UserRepositoryInterface */
|
||||
private $userRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$this->repository = app(LinkTypeRepositoryInterface::class);
|
||||
$this->userRepository = app(UserRepositoryInterface::class);
|
||||
$this->repository->setUser($user);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the resource.
|
||||
*
|
||||
* @param LinkType $linkType
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function delete(LinkType $linkType): JsonResponse
|
||||
{
|
||||
if ($linkType->editable === false) {
|
||||
throw new FireflyException(sprintf('You cannot delete this link type (#%d, "%s")', $linkType->id, $linkType->name));
|
||||
}
|
||||
$this->repository->destroy($linkType, null);
|
||||
|
||||
return response()->json([], 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all of them.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse]
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
// create some objects:
|
||||
$manager = new Manager;
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||
|
||||
// get list of accounts. Count it and split it.
|
||||
$collection = $this->repository->get();
|
||||
$count = $collection->count();
|
||||
$linkTypes = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
||||
|
||||
// make paginator:
|
||||
$paginator = new LengthAwarePaginator($linkTypes, $count, $pageSize, $this->parameters->get('page'));
|
||||
$paginator->setPath(route('api.v1.link_types.index') . $this->buildParams());
|
||||
|
||||
// present to user.
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
$resource = new FractalCollection($linkTypes, new LinkTypeTransformer($this->parameters), 'link_types');
|
||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* List single resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param LinkType $linkType
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Request $request, LinkType $linkType): JsonResponse
|
||||
{
|
||||
$manager = new Manager;
|
||||
|
||||
// add include parameter:
|
||||
$include = $request->get('include') ?? '';
|
||||
$manager->parseIncludes($include);
|
||||
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
$resource = new Item($linkType, new LinkTypeTransformer($this->parameters), 'link_types');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store new object.
|
||||
*
|
||||
* @param LinkTypeRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(LinkTypeRequest $request): JsonResponse
|
||||
{
|
||||
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
|
||||
throw new FireflyException('You need the "owner"-role to do this.');
|
||||
}
|
||||
$data = $request->getAll();
|
||||
// if currency ID is 0, find the currency by the code:
|
||||
$linkType = $this->repository->store($data);
|
||||
$manager = new Manager;
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
$resource = new Item($linkType, new LinkTypeTransformer($this->parameters), 'link_types');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LinkTypeRequest $request
|
||||
* @param LinkType $linkType
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function update(LinkTypeRequest $request, LinkType $linkType): JsonResponse
|
||||
{
|
||||
if ($linkType->editable === false) {
|
||||
throw new FireflyException(sprintf('You cannot edit this link type (#%d, "%s")', $linkType->id, $linkType->name));
|
||||
}
|
||||
if (!$this->userRepository->hasRole(auth()->user(), 'owner')) {
|
||||
throw new FireflyException('You need the "owner"-role to do this.');
|
||||
}
|
||||
|
||||
$data = $request->getAll();
|
||||
$this->repository->update($linkType, $data);
|
||||
$manager = new Manager;
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
$resource = new Item($linkType, new LinkTypeTransformer($this->parameters), 'link_types');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
}
|
@ -112,7 +112,7 @@ class LinkController extends Controller
|
||||
public function destroy(Request $request, LinkTypeRepositoryInterface $repository, LinkType $linkType)
|
||||
{
|
||||
$name = $linkType->name;
|
||||
$moveTo = $repository->find((int)$request->get('move_link_type_before_delete'));
|
||||
$moveTo = $repository->findNull((int)$request->get('move_link_type_before_delete'));
|
||||
$repository->destroy($linkType, $moveTo);
|
||||
|
||||
$request->session()->flash('success', (string)trans('firefly.deleted_link_type', ['name' => $name]));
|
||||
|
@ -24,6 +24,7 @@ namespace FireflyIII\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@ -40,6 +41,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
*/
|
||||
class LinkType extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
/**
|
||||
* The attributes that should be casted to native types.
|
||||
*
|
||||
|
@ -57,9 +57,9 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(LinkType $linkType, LinkType $moveTo): bool
|
||||
public function destroy(LinkType $linkType, LinkType $moveTo = null): bool
|
||||
{
|
||||
if (null !== $moveTo->id) {
|
||||
if (null !== $moveTo) {
|
||||
TransactionJournalLink::where('link_type_id', $linkType->id)->update(['link_type_id' => $moveTo->id]);
|
||||
}
|
||||
$linkType->delete();
|
||||
@ -125,6 +125,16 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
|
||||
return $count + $opposingCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return LinkType|null
|
||||
*/
|
||||
public function findNull(int $id): ?LinkType
|
||||
{
|
||||
return LinkType::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* See if such a link already exists (and get it).
|
||||
*
|
||||
|
@ -45,7 +45,7 @@ interface LinkTypeRepositoryInterface
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(LinkType $linkType, LinkType $moveTo): bool;
|
||||
public function destroy(LinkType $linkType, LinkType $moveTo = null): bool;
|
||||
|
||||
/**
|
||||
* @param TransactionJournalLink $link
|
||||
@ -62,6 +62,13 @@ interface LinkTypeRepositoryInterface
|
||||
*/
|
||||
public function find(int $id): LinkType;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return LinkType|null
|
||||
*/
|
||||
public function findNull(int $id): ?LinkType;
|
||||
|
||||
/**
|
||||
* Find link type by name.
|
||||
*
|
||||
|
@ -156,6 +156,32 @@ Route::group(
|
||||
}
|
||||
);
|
||||
|
||||
Route::group(
|
||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'link_types', 'as' => 'api.v1.link_types.'],
|
||||
function () {
|
||||
|
||||
// Link Type API routes:
|
||||
Route::get('', ['uses' => 'LinkTypeController@index', 'as' => 'index']);
|
||||
Route::post('', ['uses' => 'LinkTypeController@store', 'as' => 'store']);
|
||||
Route::get('{linkType}', ['uses' => 'LinkTypeController@show', 'as' => 'show']);
|
||||
Route::put('{linkType}', ['uses' => 'LinkTypeController@update', 'as' => 'update']);
|
||||
Route::delete('{linkType}', ['uses' => 'LinkTypeController@delete', 'as' => 'delete']);
|
||||
}
|
||||
);
|
||||
|
||||
Route::group(
|
||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'piggy_banks', 'as' => 'api.v1.piggy_banks.'],
|
||||
function () {
|
||||
|
||||
// Piggy Bank API routes:
|
||||
Route::get('', ['uses' => 'PiggyBankController@index', 'as' => 'index']);
|
||||
Route::post('', ['uses' => 'PiggyBankController@store', 'as' => 'store']);
|
||||
Route::get('{piggyBank}', ['uses' => 'PiggyBankController@show', 'as' => 'show']);
|
||||
Route::put('{piggyBank}', ['uses' => 'PiggyBankController@update', 'as' => 'update']);
|
||||
Route::delete('{piggyBank}', ['uses' => 'PiggyBankController@delete', 'as' => 'delete']);
|
||||
}
|
||||
);
|
||||
|
||||
Route::group(
|
||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'currencies', 'as' => 'api.v1.currencies.'],
|
||||
function () {
|
||||
|
Loading…
Reference in New Issue
Block a user