add mobile api services: virtual-key-page and ticket-page

This commit is contained in:
Roland Schneider 2022-02-20 20:34:35 +01:00
parent ac5076534b
commit ae2924a4bd
5 changed files with 142 additions and 3 deletions

View File

@ -12,6 +12,7 @@ use yii\behaviors\TimestampBehavior;
* @property integer $id_card * @property integer $id_card
* @property integer $id_key * @property integer $id_key
* @property integer $id_user * @property integer $id_user
* @property string $virtual_key
* @property string $created_at * @property string $created_at
* @property string $updated_at * @property string $updated_at
*/ */

View File

@ -0,0 +1,44 @@
<?php
use yii\db\Migration;
/**
* Class m220220_190302_add_virtual_key_field_for_card_key_assignment_table
*/
class m220220_190302_add_virtual_key_field_for_card_key_assignment_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('card_key_assignment','virtual_key', $this->string());
$this->addColumn('card_key_assignment','direction_in_at', 'datetime');
$this->addColumn('card_key_assignment','direction_out_at', 'datetime');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m220220_190302_add_virtual_key_field_for_card_key_assignment_table cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m220220_190302_add_virtual_key_field_for_card_key_assignment_table cannot be reverted.\n";
return false;
}
*/
}

View File

@ -117,6 +117,7 @@ class KeyToggleForm extends Model
$assignment->id_key = $this->keyModel->id_key; $assignment->id_key = $this->keyModel->id_key;
/** @noinspection PhpUndefinedClassInspection */ /** @noinspection PhpUndefinedClassInspection */
$assignment->id_user = \Yii::$app->user->id; $assignment->id_user = \Yii::$app->user->id;
$assignment->virtual_key = uniqid(null,true);
$assignment->save(false); $assignment->save(false);
/** @noinspection PhpUndefinedClassInspection */ /** @noinspection PhpUndefinedClassInspection */
\Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' ); \Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' );

View File

@ -25,5 +25,14 @@ class ApiController extends RestController
return $apiManager->getCardPage(); return $apiManager->getCardPage();
} }
public function actionTicketPage(){
$apiManager = new ApiManager();
return $apiManager->getTicketPage();
}
public function actionVirtualKeyPage(){
$apiManager = new ApiManager();
return $apiManager->getVirtualKeyPage();
}
} }

View File

@ -1,20 +1,26 @@
<?php <?php
namespace mobileapi\manager; namespace mobileapi\manager;
use common\models\Card; use common\models\Card;
use common\models\CardKeyAssignment;
use common\models\Key;
use common\models\Ticket;
use Endroid\QrCode\QrCode; use Endroid\QrCode\QrCode;
use yii\web\NotFoundHttpException;
class ApiManager class ApiManager
{ {
public function getCardPage(){ public function getCardPage()
{
$customer = \Yii::$app->user->getIdentity(); $customer = \Yii::$app->user->getIdentity();
if ( $customer == null ){ if (!isset($customer)) {
throw new \yii\web\NotFoundHttpException(); throw new \yii\web\NotFoundHttpException();
} }
$card = Card::findOne($customer->id_customer_card); $card = Card::findOne($customer->id_customer_card);
if ( $card == null ){ if (!isset($card)) {
throw new \yii\web\NotFoundHttpException(); throw new \yii\web\NotFoundHttpException();
} }
@ -27,4 +33,82 @@ class ApiManager
]; ];
} }
function getTicketPage()
{
$customer = \Yii::$app->user->getIdentity();
if (!isset($customer)) {
throw new \yii\web\NotFoundHttpException();
}
$card = Card::findOne($customer->id_customer_card);
if (!isset($card)) {
throw new \yii\web\NotFoundHttpException();
}
$tickets = Ticket::readActive($card);
$result = [];
foreach ($tickets as $ticket) {
$result[] = [
'id_ticket' => $ticket->id_ticket,
'type' => [
'id_type' => $ticket->ticketType->id_ticket_type,
'name' => $ticket->ticketType->name,
],
'usage_count' => $ticket->usage_count,
'start' => $ticket->start,
'end' => $ticket->end
];
}
return $result;
}
public function getVirtualKeyPage()
{
$customer = \Yii::$app->user->getIdentity();
if (!isset($customer)) {
throw new \yii\web\NotFoundHttpException();
}
$card = Card::findOne($customer->id_customer_card);
if (!isset($card)) {
throw new \yii\web\NotFoundHttpException();
}
$keyObject = null;
try {
$keyAssignment = CardKeyAssignment::findOne(['id_card' => $card->id_card]);
if (!isset($keyAssignment)) {
throw new NotFoundHttpException();
}
$key = Key::findOne(['id_key' => $keyAssignment->id_key]);
if (!isset($key)) {
throw new NotFoundHttpException();
}
$qrCode = new QrCode($keyAssignment->virtual_key );
$keyObject = [
'qrcode' => $qrCode->writeDataUri(),
'created_at' => $card->created_at,
'key' => $key->number,
'id_key' => $key->id_key
];
} catch (\Exception $e) {
// failed to get key
\Yii::error('Failed to get virtual key: '.$e->getMessage());
}
return [
'key' => $keyObject
];
}
} }