123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Controller;
-
- use App\Utility\EprosumeModel;
- use App\Utility\WalletEprosumeModel;
-
- use Cake\Core\Configure;
- use Cake\Http\Exception\ForbiddenException;
- use Cake\Http\Exception\NotFoundException;
- use Cake\View\Exception\MissingTemplateException;
-
- use Cake\Event\Event;
-
- class ApiController extends SSPController
- {
- private $user;
-
- public function initialize()
- {
- $this->EprosumeModel = new EprosumeModel();
- $this->WalletEprosumeModel = new WalletEprosumeModel();
- $this->loadComponent('ApiAuth', [
- 'className' => 'Auth',
- 'authenticate' => [
- 'Basic' => [
- 'fields' => ['username' => 'username', 'password' => 'password']
- ]
- ],
- 'userModel' => 'Users'
- ]);
- }
-
- public function beforeFilter(Event $event)
- {
- if(!$this->user = $this->ApiAuth->identify()) {
- return $this->response
- ->withStatus(401);
- }
- if(
- $this->request->getEnv('REQUEST_METHOD') == "POST"
- &&
- $this->user["role"] != "admin"
- ) {
- return $this->response
- ->withStatus(403);
- }
- }
-
- public function blockID($block_id) {
- $block = $this->EprosumeModel->blocksList(['id' => $block_id])->current();
- return $block
- ? $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- json_encode(
- (function($array) {
- array_walk(
- $array,
- function(&$v,$k) use(&$array) {
- if(!is_string($k)) {
- unset($array[$k]);
- }
- }
- );
- return $array;
- })($block)
- )
- )
- : $this->response
- ->withStatus(404)
- ->withType('application/json')
- ;
- }
-
- public function meter()
- {
- return $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- json_encode(
- array_values(
- array_replace_recursive(
- $this->EprosumeModel->meterList(),
- $this->EprosumeModel->actualPrice([])
- )
- )
- )
- )
- ;
- }
-
- public function meterID($meter_id)
- {
- return $this->response
- ->withStatus(200)
- ->withType('application/json')
- ->withStringBody(
- json_encode(
- array_merge(
- $this->EprosumeModel->balanceMeter($meter_id),
- $this->EprosumeModel->actualPrice($meter_id)[$meter_id] ?? []
- )
- )
- )
- ;
- }
-
- public function transfer()
- {
- if( $this->request->getEnv('REQUEST_METHOD') == "POST") {
- $request = $this->getRequestInput();
- return $this->doTransfer(
- $request["meter"] ?? null,
- $request["amount"] ?? null,
- $request["asset"] ?? null,
- $request["memo"] ?? null
- );
- } else {
- return $this->response->withStatus(400);
- }
- }
-
- public function price()
- {
- if( $this->request->getEnv('REQUEST_METHOD') == "POST") {
- $request = $this->getRequestInput();
- return $this->doSetPrice(
- $request["meter"] ?? null,
- $request["buyPrice"] ?? null,
- $request["sellPrice"] ?? null
- );
- } else if( $this->request->getEnv('REQUEST_METHOD') == "GET") {
- return $this->priceHistory();
- }
- return null;
- }
-
-
- public function history()
- {
- return $this->SSPResponse("history", null);
- }
-
- public function payment()
- {
- return $this->SSPResponse("payment", null);
- }
-
- public function priceHistory()
- {
- return $this->SSPResponse("priceHistory", null);
- }
-
- }
|