Sources
This commit is contained in:
@ -130,4 +130,47 @@ class Currencies {
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getSources(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => null,
|
||||
'sources' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->sources()) {
|
||||
$output['sources'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->sources());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function addSources(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'post_data' => $post,
|
||||
'currency' => null,
|
||||
'sources' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$sources = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$sources []= $currency->addSource($obj);
|
||||
}
|
||||
} else {
|
||||
$sources []= $currency->addSource($post);
|
||||
}
|
||||
$output['sources'] = $sources;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
73
app/common/Controller/Sources.php
Normal file
73
app/common/Controller/Sources.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Money\Source;
|
||||
|
||||
class Sources {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$sources = $factory->find(Source::class)->array();
|
||||
$output = compact('sources');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'url'),
|
||||
'source' => null
|
||||
];
|
||||
if ($source) {
|
||||
$output['source'] = $source->asArray();
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$sources = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$sources []= Source::add($factory, $obj);
|
||||
}
|
||||
} else {
|
||||
$sources []= Source::add($factory, $post);
|
||||
}
|
||||
$output = [
|
||||
'post_data' => $post,
|
||||
'sources' => $sources
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $url) {
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$output = [
|
||||
'get_data' => compact('currency_id', 'url'),
|
||||
'post_data' => $post
|
||||
];
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
||||
$edited = false;
|
||||
if ($source) {
|
||||
$edited = $source->edit($post);
|
||||
$output['source'] = $source->asArray();
|
||||
$output['edited'] = $edited;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $url): Response {
|
||||
$source = $factory->find(Source::class)->where([['currency_id', $currency_id], ['url', $url]])->one();
|
||||
$output = ['get_data' => compact('currency_id', 'url'), 'source' => null, 'deleted' => false];
|
||||
if ($source) {
|
||||
$output['source'] = $source->asArray();
|
||||
$status = $source->delete();
|
||||
$output['deleted'] = $status;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user