Web Interfaces of PROSUME

ApiController.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Controller;
  3. use App\Utility\EprosumeModel;
  4. use App\Utility\WalletEprosumeModel;
  5. use Cake\Core\Configure;
  6. use Cake\Http\Exception\ForbiddenException;
  7. use Cake\Http\Exception\NotFoundException;
  8. use Cake\View\Exception\MissingTemplateException;
  9. use Cake\Event\Event;
  10. class ApiController extends SSPController
  11. {
  12. private $user;
  13. public function initialize()
  14. {
  15. $this->EprosumeModel = new EprosumeModel();
  16. $this->WalletEprosumeModel = new WalletEprosumeModel();
  17. $this->loadComponent('ApiAuth', [
  18. 'className' => 'Auth',
  19. 'authenticate' => [
  20. 'Basic' => [
  21. 'fields' => ['username' => 'username', 'password' => 'password']
  22. ]
  23. ],
  24. 'userModel' => 'Users'
  25. ]);
  26. }
  27. public function beforeFilter(Event $event)
  28. {
  29. if(!$this->user = $this->ApiAuth->identify()) {
  30. return $this->response
  31. ->withStatus(401);
  32. }
  33. if(
  34. $this->request->getEnv('REQUEST_METHOD') == "POST"
  35. &&
  36. $this->user["role"] != "admin"
  37. ) {
  38. return $this->response
  39. ->withStatus(403);
  40. }
  41. }
  42. public function blockID($block_id) {
  43. $block = $this->EprosumeModel->blocksList(['id' => $block_id])->current();
  44. return $block
  45. ? $this->response
  46. ->withStatus(200)
  47. ->withType('application/json')
  48. ->withStringBody(
  49. json_encode(
  50. (function($array) {
  51. array_walk(
  52. $array,
  53. function(&$v,$k) use(&$array) {
  54. if(!is_string($k)) {
  55. unset($array[$k]);
  56. }
  57. }
  58. );
  59. return $array;
  60. })($block)
  61. )
  62. )
  63. : $this->response
  64. ->withStatus(404)
  65. ->withType('application/json')
  66. ;
  67. }
  68. public function meter()
  69. {
  70. return $this->response
  71. ->withStatus(200)
  72. ->withType('application/json')
  73. ->withStringBody(
  74. json_encode(
  75. array_values(
  76. array_replace_recursive(
  77. $this->EprosumeModel->meterList(),
  78. $this->EprosumeModel->actualPrice([])
  79. )
  80. )
  81. )
  82. )
  83. ;
  84. }
  85. public function meterID($meter_id)
  86. {
  87. return $this->response
  88. ->withStatus(200)
  89. ->withType('application/json')
  90. ->withStringBody(
  91. json_encode(
  92. array_merge(
  93. $this->EprosumeModel->balanceMeter($meter_id),
  94. $this->EprosumeModel->actualPrice($meter_id)[$meter_id] ?? []
  95. )
  96. )
  97. )
  98. ;
  99. }
  100. public function transfer()
  101. {
  102. if( $this->request->getEnv('REQUEST_METHOD') == "POST") {
  103. $request = $this->getRequestInput();
  104. return $this->doTransfer(
  105. $request["meter"] ?? null,
  106. $request["amount"] ?? null,
  107. $request["asset"] ?? null,
  108. $request["memo"] ?? null
  109. );
  110. } else {
  111. return $this->response->withStatus(400);
  112. }
  113. }
  114. public function price()
  115. {
  116. if( $this->request->getEnv('REQUEST_METHOD') == "POST") {
  117. $request = $this->getRequestInput();
  118. return $this->doSetPrice(
  119. $request["meter"] ?? null,
  120. $request["buyPrice"] ?? null,
  121. $request["sellPrice"] ?? null
  122. );
  123. } else if( $this->request->getEnv('REQUEST_METHOD') == "GET") {
  124. return $this->priceHistory();
  125. }
  126. return null;
  127. }
  128. public function history()
  129. {
  130. return $this->SSPResponse("history", null);
  131. }
  132. public function payment()
  133. {
  134. return $this->SSPResponse("payment", null);
  135. }
  136. public function priceHistory()
  137. {
  138. return $this->SSPResponse("priceHistory", null);
  139. }
  140. }