API 1.0.0
This commit is contained in:
@ -15,66 +15,104 @@ class Currencies {
|
||||
$output = compact('currencies');
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function show(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
public function get(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'currency' => $currency->asArray()
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => null
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$fields = [
|
||||
'code',
|
||||
'name'
|
||||
];
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$data = array_intersect_key((array) $post, array_combine($fields, $fields));
|
||||
$currency = $factory->find(Currency::class)->where([['code', $data['code']]])->one();
|
||||
if (!$currency) {
|
||||
$currency = $factory->create(Currency::class, $data);
|
||||
$currency->save();
|
||||
$currencies = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$currencies []= Currency::add($factory, $obj);
|
||||
}
|
||||
} else {
|
||||
$currencies []= Currency::add($factory, $post);
|
||||
}
|
||||
$output = [
|
||||
'post_data' => $post,
|
||||
'input' => $data,
|
||||
'currency' => $currency->asArray()
|
||||
'currencies' => $currencies
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$fields = [
|
||||
'code',
|
||||
'name'
|
||||
];
|
||||
$post = json_decode($request->getBody()->getContents());
|
||||
$data = array_intersect_key((array) $post, array_combine($fields, $fields));
|
||||
$edited = false;
|
||||
foreach ($data as $field => $value) {
|
||||
if ($currency->{$field} != $value) {
|
||||
$currency->{$field} = $value;
|
||||
$edited = true;
|
||||
}
|
||||
}
|
||||
if ($edited) {
|
||||
$currency->save();
|
||||
}
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'post_data' => $post,
|
||||
'input' => $data,
|
||||
'currency' => $currency->asArray()
|
||||
'currency' => null,
|
||||
'edited' => $edited
|
||||
];
|
||||
if ($currency) {
|
||||
$edited = $currency->edit($post);
|
||||
$output['currency'] = $currency->asArray();
|
||||
$output['edited'] = $edited;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function delete(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, 'deleted' => false];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$status = $currency->delete();
|
||||
$output['deleted'] = $status;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function getValues(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||
$output = [
|
||||
'get_data' => compact('currency_id'),
|
||||
'currency' => $currency->asArray()
|
||||
'currency' => null,
|
||||
'values' => []
|
||||
];
|
||||
$status = $currency->delete();
|
||||
$output['deleted'] = $status;
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
if ($currency->values()) {
|
||||
$output['values'] = array_map(function($item) {
|
||||
return $item->asArray();
|
||||
}, $currency->values());
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function addValues(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,
|
||||
'values' => []
|
||||
];
|
||||
if ($currency) {
|
||||
$output['currency'] = $currency->asArray();
|
||||
$values = [];
|
||||
if (is_array($post)) {
|
||||
foreach ($post as $obj) {
|
||||
if (!is_object($obj)) {
|
||||
continue;
|
||||
}
|
||||
$values []= $currency->addValue($obj);
|
||||
}
|
||||
} else {
|
||||
$values []= $currency->addValue($post);
|
||||
}
|
||||
$output['values'] = $values;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user