Permalink
Browse files

Module, versioning and better file structure to API

  • Loading branch information...
1 parent b379cfe commit 5a8ff5e5c175ae37354c525c51a3e0dbac844bbb @abagayev abagayev committed Jan 18, 2016
View
@@ -88,6 +88,9 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties
+### Vagrant ###
+.vagrant
+
### Hryvna ###
engine/assets/*
@@ -98,4 +101,4 @@ public/hryvna.today/index.html
public/hryvna.today/js/data.js
log
old
-engine/config/params.php
+engine/config/params.php
View
@@ -108,6 +108,7 @@ $script = <<SCRIPT
# Update the server
echo "Update server..."
+ add-apt-repository -y ppa:ondrej/php5-5.6
apt-get update -qq && apt-get upgrade -qq -y --fix-missing
# Install basic tools
@@ -0,0 +1,46 @@
+<?php
+
+namespace app\api\v1;
+
+use Yii;
+use yii\web\Controller;
+
+use app\models;
+
+class BaseController extends Controller
+{
+
+ /**
+ * Method to check and prepare param from query
+ * @param string $param_key
+ * @param string $default_value
+ * @param int $filters
+ * @return string
+ */
+ protected function prepareParam($param_key, $default_value = null, $filters = FILTER_SANITIZE_STRING)
+ {
+ $param_value = Yii::$app->getRequest()->getQueryParam($param_key, $default_value);
+ return filter_var($param_value, $filters);
+ }
+
+ /**
+ * Method to check and prepare param from query
+ * @param string $param_key
+ * @return integer
+ */
+ protected function prepareParamInt($param_key)
+ {
+ return $this->prepareParam($param_key, 0, FILTER_SANITIZE_NUMBER_INT);
+ }
+
+ /**
+ * Method to check and prepare param from query
+ * @param string $param_key
+ * @return float
+ */
+ protected function prepareParamFloat($param_key)
+ {
+ return $this->prepareParam($param_key, 0, FILTER_SANITIZE_NUMBER_FLOAT);
+ }
+
+}
@@ -0,0 +1,37 @@
+<?php
+
+namespace app\api\v1;
+
+use Yii;
+use app\models\api\Log;
+
+class Module extends \yii\base\Module
+{
+
+ /**
+ * @inheritdoc
+ */
+ public function afterAction($action, $result)
+ {
+ // save log
+
+ $log = new Log;
+ $log->status = Log::STATUS_SUCCESS;
+ $log->save();
+
+ // generate answer
+
+ $data = [
+ 'status' => Log::STATUS_SUCCESS,
+ 'data' => $result
+ ];
+
+ // add api version to headers
+
+ $headers = Yii::$app->response->headers;
+ $headers->set('Api-version', 1);
+
+ return parent::afterAction($action, $data);
+ }
+
+}
@@ -1,10 +1,12 @@
<?php
-namespace app\components;
+namespace app\api\v1\components;
use Yii;
use app\models;
+use app\models\api\Log;
+
class ApiErrorHandler extends \yii\web\ErrorHandler
{
@@ -15,8 +17,8 @@ class ApiErrorHandler extends \yii\web\ErrorHandler
protected function renderException($exception)
{
- $log = new models\api\Log;
- $log->status = models\api\Log::STATUS_ERROR;
+ $log = new Log;
+ $log->status = Log::STATUS_ERROR;
$log->save();
if (Yii::$app->has('response')) {
@@ -52,7 +54,7 @@ protected function convertExceptionToArray(\Exception $exception) {
return [
- 'status' => models\api\Log::STATUS_ERROR,
+ 'status' => Log::STATUS_ERROR,
'message' => $message
];
}
@@ -0,0 +1,33 @@
+<?php
+
+namespace app\api\v1\controllers;
+
+use Yii;
+use app\api\v1\BaseController;
+
+use app\models;
+use app\components;
+
+use app\hryvna\Dashboard;
+
+class ConvertController extends BaseController
+{
+ public function actionIndex()
+ {
+ $sum = $this->prepareParamFloat('sum');
+ if (empty($sum)) return 0;
+
+ $from = $this->prepareParamInt('from');
+ $to = $this->prepareParamInt('to');
+
+ $dashboard = new Dashboard([$from, $to]);
+ $avgs = $dashboard->getAvg();
+
+ // @todo cleanup this mess
+
+ $from_k = ($from == 980) ? 1 : $avgs[$from]['commercial']['buy']['value'];
+ $to_k = ($to == 980) ? 1 : $avgs[$to]['commercial']['buy']['value'];
+
+ return $sum * $from_k / $to_k;
+ }
+}
@@ -0,0 +1,26 @@
+<?php
+
+namespace app\api\v1\controllers;
+
+use Yii;
+use app\api\v1\BaseController;
+
+class ListController extends BaseController
+{
+
+ public function actionCurrencies()
+ {
+ return \app\models\Currency::find()->orderBy('id')->all();
+ }
+
+ public function actionBanks()
+ {
+ return \app\models\Bank::find()->orderBy('id')->all();
+ }
+
+ public function actionBank_types()
+ {
+ return \app\models\BankType::find()->orderBy('id')->all();
+ }
+
+}
@@ -0,0 +1,83 @@
+<?php
+
+namespace app\api\v1\controllers;
+
+use Yii;
+use yii\helpers\ArrayHelper;
+use app\api\v1\BaseController;
+
+use app\models;
+use app\components;
+
+use app\models\Currency;
+
+use app\hryvna\Dashboard;
+
+class RatesController extends BaseController
+{
+
+ private function getDashboard(array $currencies = null)
+ {
+ if (empty($currencies)) {
+
+ $currencies = ArrayHelper::getColumn(
+ Currency::find()->orderBy('id')->all(),
+ 'id');
+ ;
+ }
+
+ return new Dashboard($currencies, Dashboard::FLAG_CALCULATE_DIFF);
+ }
+
+ /**
+ * Giving average exchanges for one day
+ * This is using on first screen of site
+ * @return array
+ */
+ public function actionToday()
+ {
+ $dashboard = $this->getDashboard();
+ return $dashboard->getAvg();
+ }
+
+ /**
+ * Getting history of exchanges
+ * @return array
+ */
+ public function actionAverages()
+ {
+ $dashboard = $this->getDashboard();
+ return $dashboard->getAvgHistory();
+ }
+
+ /**
+ * Getting exchanges in banks for selected period
+ * @return array
+ */
+ public function actionBanks()
+ {
+ $dashboard = $this->getDashboard();
+ return $dashboard->getBanksHistory();
+ }
+
+ /**
+ * Getting information from first page of site with one request
+ * @return array
+ */
+ public function actionLanding()
+ {
+ $currencies = ArrayHelper::getColumn(
+ models\SiteCurrency::find()->orderBy('rate')->all(),
+ 'currency_id'
+ );
+
+ $dashboard = $this->getDashboard($currencies);
+
+ return [
+ 'avg' => $dashboard->getAvg(),
+ 'days' => $dashboard->getAvgHistory(),
+ 'banks' => $dashboard->getBanksHistory()
+ ];
+ }
+
+}
@@ -1,9 +1,9 @@
<?php
-$params = require(__DIR__ . '/params.php');
+//$params = require(__DIR__ . '/params.php');
$config = [
- 'id' => 'basic',
+ 'id' => 'api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
@@ -19,7 +19,7 @@
'enableSession' => false,
],
'errorHandler' => [
- 'class' => 'app\components\ApiErrorHandler',
+ 'class' => 'app\api\v1\components\ApiErrorHandler'
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
@@ -46,7 +46,11 @@
'format' => 'json',
],
],
- 'params' => $params,
+ 'modules' => [
+ 'v1' => [
+ 'class' => 'app\api\v1\Module',
+ ],
+ ],
];
if (YII_ENV_DEV) {
@@ -56,11 +60,11 @@
'class' => 'yii\debug\Module',
];
- $config['bootstrap'][] = 'gii';
- $config['modules']['gii'] = [
- 'class' => 'yii\gii\Module',
- 'allowedIPs' => ['127.0.0.1', '10.0.0.1'],
- ];
+// $config['bootstrap'][] = 'gii';
+// $config['modules']['gii'] = [
+// 'class' => 'yii\gii\Module',
+// 'allowedIPs' => ['127.0.0.1', '10.0.0.1'],
+// ];
}
return $config;
Oops, something went wrong.

0 comments on commit 5a8ff5e

Please sign in to comment.