Expand API options for available budgets and journal links

This commit is contained in:
James Cole
2018-08-11 18:27:45 +02:00
parent a803dfc7fa
commit 95ce72fce7
5 changed files with 83 additions and 18 deletions

View File

@@ -153,6 +153,9 @@ class AvailableBudgetController extends Controller
{ {
$data = $request->getAll(); $data = $request->getAll();
$currency = $this->currencyRepository->findNull($data['transaction_currency_id']); $currency = $this->currencyRepository->findNull($data['transaction_currency_id']);
if (null === $currency) {
$this->currencyRepository->findByCodeNull($data['transaction_currency_code']);
}
if (null === $currency) { if (null === $currency) {
throw new FireflyException('Could not find the indicated currency.'); throw new FireflyException('Could not find the indicated currency.');
} }

View File

@@ -175,8 +175,7 @@ class JournalLinkController extends Controller
$data['direction'] = 'inward'; $data['direction'] = 'inward';
$journalLink = $this->repository->storeLink($data, $inward, $outward); $journalLink = $this->repository->storeLink($data, $inward, $outward);
$resource = new Item($journalLink, new JournalLinkTransformer($this->parameters), 'journal_links');
$resource = new Item($journalLink, new JournalLinkTransformer($this->parameters), 'journal_links');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');

View File

@@ -47,10 +47,11 @@ class AvailableBudgetRequest extends Request
public function getAll(): array public function getAll(): array
{ {
return [ return [
'transaction_currency_id' => $this->integer('transaction_currency_id'), 'transaction_currency_id' => $this->integer('currency_id'),
'amount' => $this->string('amount'), 'transaction_currency_code' => $this->string('currency_code'),
'start_date' => $this->date('start_date'), 'amount' => $this->string('amount'),
'end_date' => $this->date('end_date'), 'start_date' => $this->date('start_date'),
'end_date' => $this->date('end_date'),
]; ];
} }
@@ -62,10 +63,11 @@ class AvailableBudgetRequest extends Request
public function rules(): array public function rules(): array
{ {
$rules = [ $rules = [
'transaction_currency_id' => 'required|numeric|exists:transaction_currencies,id', 'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code',
'amount' => 'required|numeric|more:0', 'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id',
'start_date' => 'required|date|before:end_date', 'amount' => 'required|numeric|more:0',
'end_date' => 'required|date|after:start_date', 'start_date' => 'required|date|before:end_date',
'end_date' => 'required|date|after:start_date',
]; ];
return $rules; return $rules;

View File

@@ -23,6 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests; namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use Illuminate\Validation\Validator;
/** /**
* *
@@ -49,10 +52,11 @@ class JournalLinkRequest extends Request
public function getAll(): array public function getAll(): array
{ {
return [ return [
'link_type_id' => $this->integer('link_type_id'), 'link_type_id' => $this->integer('link_type_id'),
'inward_id' => $this->integer('inward_id'), 'link_type_name' => $this->string('link_type_name'),
'outward_id' => $this->integer('outward_id'), 'inward_id' => $this->integer('inward_id'),
'notes' => $this->string('notes'), 'outward_id' => $this->integer('outward_id'),
'notes' => $this->string('notes'),
]; ];
} }
@@ -65,11 +69,63 @@ class JournalLinkRequest extends Request
public function rules(): array public function rules(): array
{ {
return [ return [
'link_type_id' => 'required|exists:link_types,id', 'link_type_id' => 'exists:link_types,id|required_without:link_type_name',
'inward_id' => 'required|belongsToUser:transaction_journals,id', 'link_type_name' => 'exists:link_types,name|required_without:link_type_id',
'outward_id' => 'required|belongsToUser:transaction_journals,id', 'inward_id' => 'required|belongsToUser:transaction_journals,id',
'notes' => 'between:0,65000', 'outward_id' => 'required|belongsToUser:transaction_journals,id',
'notes' => 'between:0,65000',
]; ];
} }
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->validateExistingLink($validator);
}
);
}
/**
* @param Validator $validator
*/
private function validateExistingLink(Validator $validator): void
{
/** @var LinkTypeRepositoryInterface $repository */
$repository = app(LinkTypeRepositoryInterface::class);
$repository->setUser(auth()->user());
/** @var JournalRepositoryInterface $journalRepos */
$journalRepos = app(JournalRepositoryInterface::class);
$journalRepos->setUser(auth()->user());
$data = $validator->getData();
$inwardId = (int)($data['inward_id'] ?? 0);
$outwardId = (int)($data['outward_id'] ?? 0);
$inward = $journalRepos->findNull($inwardId);
$outward = $journalRepos->findNull($outwardId);
if (null === $inward) {
$validator->errors()->add('inward_id', 'Invalid inward ID.');
return;
}
if (null === $outward) {
$validator->errors()->add('outward_id', 'Invalid outward ID.');
return;
}
if ($repository->findLink($inward, $outward)) {
$validator->errors()->add('outward_id', 'Already have a link between inward and outward.');
$validator->errors()->add('inward_id', 'Already have a link between inward and outward.');
}
}
} }

View File

@@ -230,6 +230,11 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink
{ {
$linkType = $this->findNull((int)($information['link_type_id'] ?? 0)); $linkType = $this->findNull((int)($information['link_type_id'] ?? 0));
if (null === $linkType) {
$linkType = $this->findByName($information['link_type_name']);
}
if (null === $linkType) { if (null === $linkType) {
return null; return null;
} }