improve customer api & gui
This commit is contained in:
@@ -9,12 +9,18 @@
|
||||
namespace customerapi\controllers;
|
||||
|
||||
|
||||
use common\helpers\AppArrayHelper;
|
||||
use common\models\Event;
|
||||
use common\models\EventRegistration;
|
||||
use common\models\EventType;
|
||||
use common\models\Trainer;
|
||||
use common\modules\event\manager\EventManager;
|
||||
use customerapi\manager\EventRegistrationManager;
|
||||
use customerapi\models\available\EventInterval;
|
||||
use customerapi\models\available\EventAvailable;
|
||||
use customerapi\models\available\EventTypeAvailable;
|
||||
use customerapi\models\available\RoomAvailable;
|
||||
use customerapi\models\available\TrainerAvailable;
|
||||
use customerapi\models\DayToDisplay;
|
||||
use customerapi\models\details\EventDetailsView;
|
||||
use customerapi\models\details\EventRegistrationView;
|
||||
@@ -23,6 +29,7 @@ use customerapi\models\details\RoomDetailsView;
|
||||
use customerapi\models\details\TrainerDetailsView;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Query;
|
||||
use yii\web\Response;
|
||||
|
||||
@@ -58,37 +65,42 @@ class EventController extends \customerapi\controllers\CustomerApiController
|
||||
$dates[] = $dayToDisplay;
|
||||
}
|
||||
|
||||
// get events between active dates
|
||||
$query = EventAvailable::find();
|
||||
$query = $query->select(
|
||||
[
|
||||
'{{event}}.*',
|
||||
// 'COUNT({{event_registration}}.id) AS reservationCount'
|
||||
]);
|
||||
$eventManager = new EventManager();
|
||||
$events = $eventManager->findActiveEvents($interval);
|
||||
$trainers = AppArrayHelper::objectArrayToMapById( TrainerAvailable::find()->all() );
|
||||
$rooms = AppArrayHelper::objectArrayToMapById( RoomAvailable::find()->all() );
|
||||
$eventTypes = AppArrayHelper::objectArrayToMapById( EventTypeAvailable::find()->all() );
|
||||
|
||||
$paramEventStartMax = clone $interval->lastActiveDate;
|
||||
$paramEventStartMax = $paramEventStartMax->modify('+1 day');
|
||||
$paramEventStartMax = $paramEventStartMax->getTimestamp();
|
||||
|
||||
$events = $query
|
||||
->innerJoinWith('trainer')
|
||||
->innerJoinWith('eventType')
|
||||
->innerJoinWith('room')
|
||||
->andWhere(['>=', 'event.start', $interval->firstActiveDate->getTimestamp()])
|
||||
->andWhere(['<', 'event.start', $paramEventStartMax])
|
||||
->andWhere(['event.active' => '1'])
|
||||
->all();
|
||||
|
||||
$availableEvents = [];
|
||||
|
||||
/** @var $event /common/models/Event $event */
|
||||
foreach ($events as $event ){
|
||||
$availableEvent = EventAvailable::fromEvent($event);
|
||||
$availableEvent->trainer = $trainers[$event->id_trainer];
|
||||
$availableEvent->room = $rooms[$event->id_room];
|
||||
$availableEvent->eventType = $eventTypes[$event->id_event_type];
|
||||
|
||||
$totalRegistrations = $eventManager->findActiveRegistrations($event->id);
|
||||
$customerRegistration = EventRegistration::filterForCustomer($totalRegistrations,\Yii::$app->user->id);
|
||||
$availableEvent->registrationCount = count($totalRegistrations);
|
||||
$availableEvent->eventRegistrations = $customerRegistration;
|
||||
$availableEvent->hasFreeSeats = $availableEvent->registrationCount < $availableEvent->seat_count;
|
||||
|
||||
|
||||
$availableEvents[] = $availableEvent;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// set events per day
|
||||
/** @var EventAvailable $event */
|
||||
foreach ($events as $event) {
|
||||
foreach ($availableEvents as $event) {
|
||||
$eventDay = new DateTime();
|
||||
$eventDay->setTimestamp($event->start);
|
||||
$eventDay->setTime(0, 0);
|
||||
|
||||
$event->reservationCount = $event->getEventRegistrationCount();
|
||||
$event->hasFreeSeats = $event->reservationCount < $event->seat_count;
|
||||
|
||||
/** @var DayToDisplay $date */
|
||||
foreach ($dates as $date) {
|
||||
if ($date->date === $eventDay->getTimestamp()) {
|
||||
@@ -122,11 +134,13 @@ class EventController extends \customerapi\controllers\CustomerApiController
|
||||
$paramEventStartMax = $paramEventStartMax->modify('+1 day');
|
||||
$paramEventStartMax = $paramEventStartMax->getTimestamp();
|
||||
|
||||
|
||||
/** @var ActiveQuery $query */
|
||||
return $query
|
||||
->innerJoinWith('trainer')
|
||||
->innerJoinWith('eventType')
|
||||
->innerJoinWith('room')
|
||||
->joinWith('activeEventRegistrations','event.id = activeEventRegistrations.id_event')
|
||||
->joinWith('activeEventRegistrations')
|
||||
->andWhere(['>=', 'event.start', $interval->firstActiveDate->getTimestamp()])
|
||||
->andWhere(['<', 'event.start', $paramEventStartMax])
|
||||
->andWhere(['event.active' => '1']);
|
||||
@@ -141,14 +155,10 @@ class EventController extends \customerapi\controllers\CustomerApiController
|
||||
public function actionEvent($id_event)
|
||||
{
|
||||
$interval = EventInterval::createInterval();
|
||||
$manger = new EventManager();
|
||||
try {
|
||||
$query = Event::find();
|
||||
$query = $this->buildEventQuery($query, $interval);
|
||||
$query = $query->andWhere(['event.id' => $id_event]);
|
||||
|
||||
/** @var Event $event */
|
||||
$event = $query->one();
|
||||
|
||||
$event = $manger->findActiveEvent($id_event,$interval);
|
||||
$result = new EventDetailsView();
|
||||
$result->id = $event->id;
|
||||
$result->start = $event->start;
|
||||
|
||||
@@ -29,7 +29,7 @@ class EventRegistrationController extends CustomerApiController
|
||||
public function actionIndex()
|
||||
{
|
||||
$registrationManager = new EventRegistrationManager();
|
||||
return $this->asJson( $registrationManager->findCustomerRegistrationsWithEvent() );
|
||||
return $this->asJson( $registrationManager->findCustomerRegistrationsWithEvent(\Yii::$app->user->id) );
|
||||
}
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
@@ -42,7 +42,7 @@ class EventRegistrationController extends CustomerApiController
|
||||
{
|
||||
$registrationManager = new EventRegistrationManager();
|
||||
return $this->asJson(
|
||||
$registrationManager->findRegistration($id_registration)
|
||||
$registrationManager->findRegistration($id_registration,\Yii::$app->user->id)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
namespace customerapi\manager;
|
||||
|
||||
use common\models\EventRegistration;
|
||||
use common\modules\event\manager\EventManager;
|
||||
use customerapi\models\available\EventInterval;
|
||||
use customerapi\models\registrations\EventRegistrationAvailable;
|
||||
use yii\db\ActiveQuery;
|
||||
use customerapi\models\dto\EventDTO;
|
||||
use customerapi\models\dto\EventRegistrationDTO;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class EventRegistrationManager
|
||||
{
|
||||
@@ -12,44 +14,55 @@ class EventRegistrationManager
|
||||
/** Get Customer registrations
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function findCustomerRegistrationsWithEvent()
|
||||
{
|
||||
return $this->prepareQueryFindRegistrationsForCustomer()->all();
|
||||
}
|
||||
|
||||
public function findCustomerRegistrations($clazz= EventRegistration::class )
|
||||
public function findCustomerRegistrationsWithEvent($idCustomer)
|
||||
{
|
||||
$interval = EventInterval::createInterval();
|
||||
return EventRegistration::find()
|
||||
->innerJoinWith('event')
|
||||
->andWhere(['and',
|
||||
['>=', 'event.start', $interval->firstActiveDate->getTimestamp()],
|
||||
['id_customer' => \Yii::$app->user->getId()]
|
||||
])->all();
|
||||
}
|
||||
$now = new \DateTime();
|
||||
$eventRegistrations = EventRegistration::find()
|
||||
->andWhere(['id_customer' => $idCustomer])
|
||||
// ->andWhere(['>=', 'event.start', $now->getTimestamp()])
|
||||
->all();
|
||||
|
||||
|
||||
public function findRegistration($id_registration)
|
||||
{
|
||||
return $this->prepareQueryFindRegistrationsForCustomer()
|
||||
->andWhere(['event_registration.id' => $id_registration])
|
||||
->one();
|
||||
$result = [];
|
||||
$eventManager = new EventManager();
|
||||
foreach ($eventRegistrations as $registration ){
|
||||
$dto = EventRegistrationDTO::fromEventRegistration($registration);
|
||||
|
||||
$event = $eventManager->findActiveEvent($registration->id_event, $interval,true);
|
||||
if ( isset($event)){
|
||||
$dto->event = EventDTO::fromEventWithRelatedObjects($event);
|
||||
$result[] = $dto;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare a query to get registrations for customer
|
||||
* @return ActiveQuery query
|
||||
* @throws \Exception
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
private function prepareQueryFindRegistrationsForCustomer(){
|
||||
public function findRegistration($id_registration, $id_customer)
|
||||
{
|
||||
$interval = EventInterval::createInterval();
|
||||
return EventRegistrationAvailable::find()
|
||||
->innerJoinWith('event')
|
||||
->andWhere(['and',
|
||||
['>=', 'event.start', $interval->firstActiveDate->getTimestamp()],
|
||||
['id_customer' => \Yii::$app->user->getId()]
|
||||
]);
|
||||
|
||||
$registration = EventRegistration::findOne(['id' => $id_registration, 'id_customer' => $id_customer]);
|
||||
|
||||
if ( !isset($registration)){
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$eventManager = new EventManager();
|
||||
$event = $eventManager->findActiveEvent($registration->id_event,$interval,true);
|
||||
|
||||
if ( !isset($event)){
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
return EventRegistrationDTO::fromEventRegistrationWithEvent($registration,$event);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,54 +5,72 @@ namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\Event;
|
||||
use common\models\EventRegistration;
|
||||
use common\models\EventType;
|
||||
use common\models\Room;
|
||||
use common\models\Trainer;
|
||||
use yii\base\Model;
|
||||
|
||||
class EventAvailable extends Event
|
||||
class EventAvailable extends Model
|
||||
{
|
||||
|
||||
public $reservationCount;
|
||||
public $id;
|
||||
public $start;
|
||||
public $end;
|
||||
public $seat_count;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
public $deleted_at;
|
||||
public $active;
|
||||
public $eventType;
|
||||
public $trainer;
|
||||
public $room;
|
||||
public $eventRegistrations;
|
||||
|
||||
public $registrationCount;
|
||||
public $hasFreeSeats;
|
||||
|
||||
protected function getTrainerClass()
|
||||
/**
|
||||
* @param $event /common/models/Event
|
||||
*/
|
||||
public static function fromEvent($event)
|
||||
{
|
||||
// override trainer class to have more control
|
||||
// about json fields
|
||||
return TrainerAvailable::class;
|
||||
}
|
||||
$available = new EventAvailable();
|
||||
$available->id = $event->id;
|
||||
$available->start = $event->start;
|
||||
$available->end = $event->end;
|
||||
$available->seat_count = $event->seat_count;
|
||||
$available->created_at = $event->created_at;
|
||||
$available->updated_at = $event->updated_at;
|
||||
$available->deleted_at = $event->deleted_at;
|
||||
$available->active = $event->active;
|
||||
|
||||
protected function getEventTypeClass()
|
||||
{
|
||||
return EventTypeAvailable::class;
|
||||
return $available;
|
||||
}
|
||||
|
||||
protected function getRoomClass()
|
||||
{
|
||||
return RoomAvailable::class;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function fields()
|
||||
{
|
||||
$fields = [
|
||||
"id" => "id",
|
||||
"start" => "start",
|
||||
"end" => "end",
|
||||
"seat_count" => "seat_count",
|
||||
"active" => "active",
|
||||
"reservationCount" => "reservationCount",
|
||||
"hasFreeSeats" => "hasFreeSeats",
|
||||
"id" => "id",
|
||||
"start" => "start",
|
||||
"end" => "end",
|
||||
"seat_count" => "seat_count",
|
||||
"active" => "active",
|
||||
"reservationCount" => "registrationCount",
|
||||
"hasFreeSeats" => "hasFreeSeats",
|
||||
];
|
||||
$fields['trainer'] = 'trainer';
|
||||
$fields['eventType'] = 'eventType';
|
||||
$fields['room'] = 'room';
|
||||
$fields['registrations'] = 'activeEventRegistrationsForCustomer';
|
||||
$fields['registrations'] = 'eventRegistrations';
|
||||
return $fields;
|
||||
}
|
||||
|
||||
|
||||
function extraFields()
|
||||
{
|
||||
$extra= parent::extraFields();
|
||||
$extra = parent::extraFields();
|
||||
$extra[] = 'trainer';
|
||||
return $extra;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ class EventTypeAvailable extends EventType
|
||||
{
|
||||
return [
|
||||
'id' => 'id',
|
||||
'name' => 'name'
|
||||
'name' => 'name',
|
||||
'theme' => 'theme'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
70
customerapi/models/dto/EventDTO.php
Normal file
70
customerapi/models/dto/EventDTO.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace customerapi\models\dto;
|
||||
|
||||
use customerapi\models\available\EventAvailable;
|
||||
|
||||
class EventDTO extends \yii\base\Model
|
||||
{
|
||||
|
||||
public $id;
|
||||
public $start;
|
||||
public $end;
|
||||
public $seat_count;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
public $deleted_at;
|
||||
public $active;
|
||||
public $eventType;
|
||||
public $trainer;
|
||||
public $room;
|
||||
public $eventRegistrations;
|
||||
|
||||
public $registrationCount;
|
||||
public $hasFreeSeats;
|
||||
|
||||
function fields()
|
||||
{
|
||||
$fields = [
|
||||
"id" => "id",
|
||||
"start" => "start",
|
||||
"end" => "end",
|
||||
"seat_count" => "seat_count",
|
||||
"active" => "active",
|
||||
"reservationCount" => "registrationCount",
|
||||
"hasFreeSeats" => "hasFreeSeats",
|
||||
"registrations" => "eventRegistrations",
|
||||
];
|
||||
$fields['trainer'] = 'trainer';
|
||||
$fields['eventType'] = 'eventType';
|
||||
$fields['room'] = 'room';
|
||||
// $fields['registrations'] = 'eventRegistrations';
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $event /common/models/Event
|
||||
*/
|
||||
public static function fromEvent($event)
|
||||
{
|
||||
$dto = new EventDTO();
|
||||
$dto->id = $event->id;
|
||||
$dto->start = $event->start;
|
||||
$dto->end = $event->end;
|
||||
$dto->seat_count = $event->seat_count;
|
||||
$dto->created_at = $event->created_at;
|
||||
$dto->updated_at = $event->updated_at;
|
||||
$dto->deleted_at = $event->deleted_at;
|
||||
$dto->active = $event->active;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
public static function fromEventWithRelatedObjects($event){
|
||||
$eventDto = EventDTO::fromEvent($event);
|
||||
$eventDto->trainer = TrainerDTO::fromTrainer($event->trainer);
|
||||
$eventDto->room = RoomDTO::fromRoom($event->room);
|
||||
$eventDto->eventType = EventTypeDTO::fromEventType($event->eventType);
|
||||
return $eventDto;
|
||||
}
|
||||
|
||||
}
|
||||
63
customerapi/models/dto/EventRegistrationDTO.php
Normal file
63
customerapi/models/dto/EventRegistrationDTO.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace customerapi\models\dto;
|
||||
|
||||
use common\helpers\AppDateTimeHelper;
|
||||
use yii\base\Model;
|
||||
|
||||
class EventRegistrationDTO extends Model
|
||||
{
|
||||
|
||||
public $id;
|
||||
public $id_event;
|
||||
public $id_customer;
|
||||
public $id_card;
|
||||
public $id_ticket;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
public $canceled_at;
|
||||
public $deleted_at;
|
||||
|
||||
public $event;
|
||||
|
||||
function fields()
|
||||
{
|
||||
$fields = [
|
||||
'id' => 'id',
|
||||
'id_event' => 'id_event',
|
||||
'id_customer' => 'id_customer',
|
||||
'id_card' => 'id_card',
|
||||
'id_ticket' => 'id_ticket',
|
||||
'created_at' => 'created_at',
|
||||
'updated_at' => 'updated_at',
|
||||
'canceled_at' => 'canceled_at',
|
||||
'deleted_at' => 'deleted_at',
|
||||
];
|
||||
$fields['event'] = 'event';
|
||||
return $fields;
|
||||
}
|
||||
|
||||
|
||||
public static function fromEventRegistration($registration){
|
||||
$dto = new EventRegistrationDTO();
|
||||
|
||||
$dto->id = $registration->id;
|
||||
$dto->id_event = $registration->id_event;
|
||||
$dto->id_customer = $registration->id_customer;
|
||||
$dto->id_card = $registration->id_card;
|
||||
$dto->id_ticket = $registration->id_ticket;
|
||||
$dto->created_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger( $registration->created_at);
|
||||
$dto->updated_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger($registration->updated_at);
|
||||
$dto->canceled_at =AppDateTimeHelper::convertMySqlDatetimeToPhpInteger( $registration->canceled_at);
|
||||
$dto->deleted_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger($registration->deleted_at);
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
public static function fromEventRegistrationWithEvent($registration,$event){
|
||||
$registrationDto = EventRegistrationDTO::fromEventRegistration($registration);
|
||||
$registrationDto->event = EventDTO::fromEventWithRelatedObjects($event);
|
||||
return $registrationDto;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
23
customerapi/models/dto/EventTypeDTO.php
Normal file
23
customerapi/models/dto/EventTypeDTO.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace customerapi\models\dto;
|
||||
|
||||
use yii\base\Model;
|
||||
|
||||
class EventTypeDTO extends Model
|
||||
{
|
||||
public $id;
|
||||
public $name;
|
||||
public $theme;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
|
||||
public static function fromEventType($eventType){
|
||||
$dto = new EventTypeDTO();
|
||||
$dto->id = $eventType->id;
|
||||
$dto->name = $eventType->name;
|
||||
$dto->theme = $eventType->theme;
|
||||
$dto->created_at = $eventType->created_at;
|
||||
$dto->updated_at = $eventType->updated_at;
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
27
customerapi/models/dto/RoomDTO.php
Normal file
27
customerapi/models/dto/RoomDTO.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace customerapi\models\dto;
|
||||
|
||||
use yii\base\Model;
|
||||
|
||||
class RoomDTO extends Model
|
||||
{
|
||||
|
||||
|
||||
public $id;
|
||||
public $name;
|
||||
public $seat_count;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
|
||||
public static function fromRoom($room)
|
||||
{
|
||||
$dto = new RoomDTO();
|
||||
$dto->id = $room->id;
|
||||
$dto->name = $room->name;
|
||||
$dto->seat_count = $room->seat_count;
|
||||
$dto->created_at = $room->created_at;
|
||||
$dto->updated_at = $room->updated_at;
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
32
customerapi/models/dto/TrainerDTO.php
Normal file
32
customerapi/models/dto/TrainerDTO.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace customerapi\models\dto;
|
||||
|
||||
|
||||
use yii\base\Model;
|
||||
|
||||
class TrainerDTO extends Model
|
||||
{
|
||||
public $id;
|
||||
public $name;
|
||||
public $phone;
|
||||
public $email;
|
||||
public $password;
|
||||
public $active;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
|
||||
public static function fromTrainer($trainer){
|
||||
$dto = new TrainerDTO();
|
||||
$dto->id = $trainer->id;
|
||||
$dto->name = $trainer->name;
|
||||
$dto->phone = $trainer->phone;
|
||||
$dto->email = $trainer->email;
|
||||
$dto->password = $trainer->password;
|
||||
$dto->active = $trainer->active;
|
||||
$dto->created_at = $trainer->created_at;
|
||||
$dto->updated_at = $trainer->updated_at;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user