mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-03 20:57:19 -06:00
Add new tag-cloud API endpoint.
This commit is contained in:
parent
7144798377
commit
770e44d1bd
@ -23,7 +23,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Api\V1\Requests\TagRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||
use FireflyIII\Models\Tag;
|
||||
@ -71,6 +73,55 @@ class TagController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function cloud(Request $request): JsonResponse
|
||||
{
|
||||
// parameters for cloud:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
|
||||
// get all tags:
|
||||
$tags = $this->repository->get();
|
||||
$min = null;
|
||||
$max = 0;
|
||||
$return = [
|
||||
'tags' => [],
|
||||
];
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
$earned = (float)$this->repository->earnedInPeriod($tag, $start, $end);
|
||||
$spent = (float)$this->repository->spentInPeriod($tag, $start, $end);
|
||||
$size = ($spent * -1) + $earned;
|
||||
$min = $min ?? $size;
|
||||
if ($size > 0) {
|
||||
$max = $size > $max ? $size : $max;
|
||||
$return['tags'][] = [
|
||||
'tag' => $tag->tag,
|
||||
'id' => $tag->id,
|
||||
'size' => $size,
|
||||
];
|
||||
}
|
||||
}
|
||||
foreach ($return['tags'] as $index => $info) {
|
||||
$return['tags'][$index]['relative'] = $return['tags'][$index]['size'] / $max;
|
||||
}
|
||||
$return['min'] = $min;
|
||||
$return['max'] = $max;
|
||||
|
||||
|
||||
return response()->json($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the resource.
|
||||
*
|
||||
@ -121,7 +172,6 @@ class TagController extends Controller
|
||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user