. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers\Models\TransactionCurrency; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Models\TransactionCurrency\UpdateRequest; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Support\Http\Api\AccountFilter; use FireflyIII\Support\Http\Api\TransactionFilter; use FireflyIII\Transformers\CurrencyTransformer; use Illuminate\Http\JsonResponse; use JsonException; use League\Fractal\Resource\Item; /** * Class UpdateController */ class UpdateController extends Controller { use AccountFilter, TransactionFilter; private CurrencyRepositoryInterface $repository; private UserRepositoryInterface $userRepository; /** * CurrencyRepository constructor. * * @codeCoverageIgnore */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { $this->repository = app(CurrencyRepositoryInterface::class); $this->userRepository = app(UserRepositoryInterface::class); $this->repository->setUser(auth()->user()); return $next($request); } ); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/currencies/disableCurrency * * Disable a currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @codeCoverageIgnore */ public function disable(TransactionCurrency $currency): JsonResponse { // must be unused. if ($this->repository->currencyInUse($currency)) { return response()->json([], 409); } $this->repository->disable($currency); $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/currencies/enableCurrency * * Enable a currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @codeCoverageIgnore */ public function enable(TransactionCurrency $currency): JsonResponse { $this->repository->enable($currency); $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/currencies/defaultCurrency * * Make the currency a default currency. * * @param TransactionCurrency $currency * * @return JsonResponse * @throws FireflyException * @codeCoverageIgnore */ public function makeDefault(TransactionCurrency $currency): JsonResponse { $this->repository->enable($currency); app('preferences')->set('currencyPreference', $currency->code); app('preferences')->mark(); $manager = $this->getManager(); $this->parameters->set('defaultCurrency', $currency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/currencies/updateCurrency * * Update a currency. * * @param UpdateRequest $request * @param TransactionCurrency $currency * * @return JsonResponse * @throws FireflyException * @throws JsonException */ public function update(UpdateRequest $request, TransactionCurrency $currency): JsonResponse { $data = $request->getAll(); $currency = $this->repository->update($currency, $data); if (true === $request->boolean('default')) { app('preferences')->set('currencyPreference', $currency->code); app('preferences')->mark(); } $manager = $this->getManager(); $defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user()); $this->parameters->set('defaultCurrency', $defaultCurrency); /** @var CurrencyTransformer $transformer */ $transformer = app(CurrencyTransformer::class); $transformer->setParameters($this->parameters); $resource = new Item($currency, $transformer, 'currencies'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); } }