App 0.1.0
This commit is contained in:
17
app/common/Controller/API.php
Normal file
17
app/common/Controller/API.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Money\Common\Controller;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use ProVM\Common\Define\Controller\Json;
|
||||||
|
|
||||||
|
class API {
|
||||||
|
use Json;
|
||||||
|
|
||||||
|
public function __invoke(Request $request, Response $response): Response {
|
||||||
|
$output = [
|
||||||
|
'version' => '1.0.0'
|
||||||
|
];
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
80
app/common/Controller/Currencies.php
Normal file
80
app/common/Controller/Currencies.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?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\Currency;
|
||||||
|
|
||||||
|
class Currencies {
|
||||||
|
use Json;
|
||||||
|
|
||||||
|
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||||
|
$currencies = $factory->find(Currency::class)->order('name')->array();
|
||||||
|
$output = compact('currencies');
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
public function show(Request $request, Response $response, ModelFactory $factory, $currency_id): Response {
|
||||||
|
$currency = $factory->find(Currency::class)->one($currency_id);
|
||||||
|
$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();
|
||||||
|
}
|
||||||
|
$output = [
|
||||||
|
'post_data' => $post,
|
||||||
|
'input' => $data,
|
||||||
|
'currency' => $currency->asArray()
|
||||||
|
];
|
||||||
|
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()
|
||||||
|
];
|
||||||
|
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' => $currency->asArray()
|
||||||
|
];
|
||||||
|
$status = $currency->delete();
|
||||||
|
$output['deleted'] = $status;
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
39
app/composer.json
Normal file
39
app/composer.json
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"name": "provm/money",
|
||||||
|
"description": "Aplicacion web que revisa los valores de distintas monedas",
|
||||||
|
"type": "project",
|
||||||
|
"require": {
|
||||||
|
"slim/slim": "^4.7",
|
||||||
|
"nyholm/psr7": "^1.4",
|
||||||
|
"nyholm/psr7-server": "^1.0",
|
||||||
|
"php-di/slim-bridge": "^3.1",
|
||||||
|
"provm/models": "dev-master",
|
||||||
|
"vlucas/phpdotenv": "^5.3"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Aldarien",
|
||||||
|
"email": "aldarien85@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^8.5"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"ProVM\\Money\\Common\\": "common",
|
||||||
|
"ProVM\\Common\\": "../provm/common",
|
||||||
|
"ProVM\\Money\\": "../src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "http://git.provm.cl/ProVM/models.git"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"config": {
|
||||||
|
"secure-http": false
|
||||||
|
}
|
||||||
|
}
|
7
app/public/index.php
Normal file
7
app/public/index.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
include_once implode(DIRECTORY_SEPARATOR, [
|
||||||
|
dirname(__DIR__),
|
||||||
|
'setup',
|
||||||
|
'app.php'
|
||||||
|
]);
|
||||||
|
$app->run();
|
6
app/resources/routes/api.php
Normal file
6
app/resources/routes/api.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
use ProVM\Money\Common\Controller\API;
|
||||||
|
|
||||||
|
include_once 'currencies.php';
|
||||||
|
|
||||||
|
$app->get('/', API::class);
|
11
app/resources/routes/currencies.php
Normal file
11
app/resources/routes/currencies.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
use ProVM\Money\Common\Controller\Currencies;
|
||||||
|
|
||||||
|
$app->group('/currencies', function($app) {
|
||||||
|
$app->post('/add[/]', [Currencies::class, 'add']);
|
||||||
|
$app->get('[/]', Currencies::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
$app->group('/currency/{currency_id}', function($app) {
|
||||||
|
$app->get('[/]', [Currencies::class, 'show']);
|
||||||
|
});
|
18
app/setup/api/settings.php
Normal file
18
app/setup/api/settings.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
use Psr\Container\ContainerInterface as Container;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'locations' => DI\decorate(function($prev, Container $container) {
|
||||||
|
$arr = (array) $prev;
|
||||||
|
$arr['base'] = dirname(__DIR__, 2);
|
||||||
|
$arr['resources'] = implode(DIRECTORY_SEPARATOR, [
|
||||||
|
$arr['base'],
|
||||||
|
'resources'
|
||||||
|
]);
|
||||||
|
$arr['routes'] = implode(DIRECTORY_SEPARATOR, [
|
||||||
|
$arr['resources'],
|
||||||
|
'routes'
|
||||||
|
]);
|
||||||
|
return (object) $arr;
|
||||||
|
})
|
||||||
|
];
|
64
app/setup/app.php
Normal file
64
app/setup/app.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
use DI\ContainerBuilder as Builder;
|
||||||
|
use DI\Bridge\Slim\Bridge;
|
||||||
|
|
||||||
|
include_once 'composer.php';
|
||||||
|
|
||||||
|
$builder = new Builder();
|
||||||
|
|
||||||
|
$folders = [
|
||||||
|
implode(DIRECTORY_SEPARATOR, [
|
||||||
|
dirname(__DIR__, 2),
|
||||||
|
'setup',
|
||||||
|
'env'
|
||||||
|
]),
|
||||||
|
implode(DIRECTORY_SEPARATOR, [
|
||||||
|
dirname(__DIR__, 2),
|
||||||
|
'setup',
|
||||||
|
'common'
|
||||||
|
]),
|
||||||
|
implode(DIRECTORY_SEPARATOR, [
|
||||||
|
__DIR__,
|
||||||
|
'api'
|
||||||
|
])
|
||||||
|
];
|
||||||
|
$files = [
|
||||||
|
'settings',
|
||||||
|
'setups'
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
foreach ($folders as $folder) {
|
||||||
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||||
|
$folder,
|
||||||
|
$file . '.php'
|
||||||
|
]);
|
||||||
|
if (!file_exists($filename)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$builder->addDefinitions($filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$container = $builder->build();
|
||||||
|
$app = Bridge::create($container);
|
||||||
|
include_once 'databases.php';
|
||||||
|
|
||||||
|
if ($container->has('base_url')) {
|
||||||
|
$app->setBasePath($container->get('base_url'));
|
||||||
|
}
|
||||||
|
$app->addRoutingMiddleware();
|
||||||
|
|
||||||
|
foreach ($folders as $folder) {
|
||||||
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||||
|
$folder,
|
||||||
|
'middleware.php'
|
||||||
|
]);
|
||||||
|
if (!file_exists($filename)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
include_once $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
include_once 'router.php';
|
||||||
|
$app->addErrorMiddleware(true, true, true);
|
6
app/setup/composer.php
Normal file
6
app/setup/composer.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
include_once implode(DIRECTORY_SEPARATOR, [
|
||||||
|
dirname(__DIR__),
|
||||||
|
'vendor',
|
||||||
|
'autoload.php'
|
||||||
|
]);
|
36
app/setup/databases.php
Normal file
36
app/setup/databases.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
$databases = $app->getContainer()->get('databases');
|
||||||
|
foreach ($databases as $name => $settings) {
|
||||||
|
switch($settings->system) {
|
||||||
|
case 'mysql':
|
||||||
|
$dsn = implode(':', [
|
||||||
|
'mysql',
|
||||||
|
implode(';', [
|
||||||
|
implode('=', [
|
||||||
|
'host',
|
||||||
|
$settings->host->name
|
||||||
|
]),
|
||||||
|
implode('=', [
|
||||||
|
'dbname',
|
||||||
|
$settings->name
|
||||||
|
])
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
if (isset($settings->host->port)) {
|
||||||
|
$dsn .= ';' . implode('=', [
|
||||||
|
'port',
|
||||||
|
$settings->host->port
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ORM::configure($dsn, null, $name);
|
||||||
|
switch ($settings->system) {
|
||||||
|
case 'mysql':
|
||||||
|
ORM::configure('username', $settings->user->name, $name);
|
||||||
|
ORM::configure('password', $settings->user->password, $name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($databases->short_names)) {
|
||||||
|
Model::$short_table_names = $databases->short_names;
|
||||||
|
}
|
5
app/setup/router.php
Normal file
5
app/setup/router.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
include_once implode(DIRECTORY_SEPARATOR, [
|
||||||
|
$app->getContainer()->get('locations')->routes,
|
||||||
|
'api.php'
|
||||||
|
]);
|
Reference in New Issue
Block a user