add customer api

This commit is contained in:
2019-10-08 22:33:25 +02:00
committed by Roland Schneider
parent 9aee187d11
commit 1300bfc752
33 changed files with 1164 additions and 246 deletions

View File

@@ -0,0 +1,131 @@
<?php
namespace common\components;
class HttpStatus
{
// const CONTINUE = 100;
const SWITCHING_PROTOCOLS = 101;
const PROCESSING = 102; // RFC2518
const OK = 200;
const CREATED = 201;
const ACCEPTED = 202;
const NON_AUTHORITATIVE_INFORMATION = 203;
const NO_CONTENT = 204;
const RESET_CONTENT = 205;
const PARTIAL_CONTENT = 206;
const MULTI_STATUS = 207; // RFC4918
const ALREADY_REPORTED = 208; // RFC5842
const IM_USED = 226; // RFC3229
const MULTIPLE_CHOICES = 300;
const MOVED_PERMANENTLY = 301;
const FOUND = 302;
const SEE_OTHER = 303;
const NOT_MODIFIED = 304;
const USE_PROXY = 305;
const RESERVED = 306;
const TEMPORARY_REDIRECT = 307;
const PERMANENTLY_REDIRECT = 308; // RFC7238
const BAD_REQUEST = 400;
const UNAUTHORIZED = 401;
const PAYMENT_REQUIRED = 402;
const FORBIDDEN = 403;
const NOT_FOUND = 404;
const METHOD_NOT_ALLOWED = 405;
const NOT_ACCEPTABLE = 406;
const PROXY_AUTHENTICATION_REQUIRED = 407;
const REQUEST_TIMEOUT = 408;
const CONFLICT = 409;
const GONE = 410;
const LENGTH_REQUIRED = 411;
const PRECONDITION_FAILED = 412;
const REQUEST_ENTITY_TOO_LARGE = 413;
const REQUEST_URI_TOO_LONG = 414;
const UNSUPPORTED_MEDIA_TYPE = 415;
const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
const EXPECTATION_FAILED = 417;
const I_AM_A_TEAPOT = 418; // RFC2324
const UNPROCESSABLE_ENTITY = 422; // RFC4918
const LOCKED = 423; // RFC4918
const FAILED_DEPENDENCY = 424; // RFC4918
const RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817
const UPGRADE_REQUIRED = 426; // RFC2817
const PRECONDITION_REQUIRED = 428; // RFC6585
const TOO_MANY_REQUESTS = 429; // RFC6585
const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585
const INTERNAL_SERVER_ERROR = 500;
const NOT_IMPLEMENTED = 501;
const BAD_GATEWAY = 502;
const SERVICE_UNAVAILABLE = 503;
const GATEWAY_TIMEOUT = 504;
const VERSION_NOT_SUPPORTED = 505;
const VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295
const INSUFFICIENT_STORAGE = 507; // RFC4918
const LOOP_DETECTED = 508; // RFC5842
const NOT_EXTENDED = 510; // RFC2774
const NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
}

View File

@@ -3,10 +3,16 @@
namespace common\manager;
use common\models\Card;
use common\models\CardEventRegistrationForm;
use common\models\Customer;
use common\models\Event;
use common\models\EventRegistration;
use common\models\Ticket;
use Exception;
use Throwable;
use Yii;
use yii\base\Object;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
@@ -18,7 +24,7 @@ use yii\web\ServerErrorHttpException;
* Date: 2018.12.17.
* Time: 6:12
*/
class EventRegistrationManager extends \yii\base\Object
class EventRegistrationManager extends Object
{
const CARD_NOT_FOUND = 1;
const CUSTOMER_NOT_FOUND = 2;
@@ -31,70 +37,70 @@ class EventRegistrationManager extends \yii\base\Object
const EVENT_UNAVAILABLE = 9;
public static $STATES = [
self::CARD_NOT_FOUND => "CARD_NOT_FOUND",
self::CUSTOMER_NOT_FOUND => "CUSTOMER_NOT_FOUND",
self::TICKET_NOT_FOUND => "TICKET_NOT_FOUND",
self::NO_FREE_SEATS => "NO_FREE_SEATS",
self::EVENT_TYPE_NOT_FOUND => "EVENT_TYPE_NOT_FOUND",
self::TICKET_INSUFFICIENT => "TICKET_INSUFFICIENT",
self::UNKNOWN_ERROR => "UNKNOWN_ERROR",
self::MAX_SEAT_COUNT_EXCEEDED => "MAX_SEAT_COUNT_EXCEEDED",
self::EVENT_UNAVAILABLE => "EVENT_UNAVAILABLE",
self::CARD_NOT_FOUND => 'CARD_NOT_FOUND',
self::CUSTOMER_NOT_FOUND => 'CUSTOMER_NOT_FOUND',
self::TICKET_NOT_FOUND => 'TICKET_NOT_FOUND',
self::NO_FREE_SEATS => 'NO_FREE_SEATS',
self::EVENT_TYPE_NOT_FOUND => 'EVENT_TYPE_NOT_FOUND',
self::TICKET_INSUFFICIENT => 'TICKET_INSUFFICIENT',
self::UNKNOWN_ERROR => 'UNKNOWN_ERROR',
self::MAX_SEAT_COUNT_EXCEEDED => 'MAX_SEAT_COUNT_EXCEEDED',
self::EVENT_UNAVAILABLE => 'EVENT_UNAVAILABLE',
];
/**
* @param \common\models\CardEventRegistrationForm $cardEventForm
* @throws \yii\db\Exception
* @param CardEventRegistrationForm $cardEventForm
* @throws Exception
*/
public function registerCard($cardEventForm)
{
$db = \Yii::$app->db;
$db = Yii::$app->db;
$tx = $db->beginTransaction();
try {
if ($cardEventForm->validate()) {
/** @var \common\models\Card $card */
/** @var Card $card */
$card = Card::readCard($cardEventForm->card_number, false);
if (!isset($card)) {
throw new NotFoundHttpException("Card not found: " . $cardEventForm->card_number, self::CARD_NOT_FOUND);
throw new NotFoundHttpException('Card not found: ' . $cardEventForm->card_number, self::CARD_NOT_FOUND);
}
if ($card->isFree()) {
throw new NotFoundHttpException("Customer not found", self::CUSTOMER_NOT_FOUND);
throw new NotFoundHttpException('Customer not found', self::CUSTOMER_NOT_FOUND);
}
$activeTickets = $card->getActiveTickets();
if (sizeof($activeTickets) == 0) {
throw new NotFoundHttpException("Ticket not found", self::TICKET_NOT_FOUND);
if (count($activeTickets) === 0) {
throw new NotFoundHttpException('Ticket not found', self::TICKET_NOT_FOUND);
}
/** @var \common\models\Event $event */
/** @var Event $event */
$event = Event::find()->andWhere(['id' => $cardEventForm->event_id])->one();
if (!isset($event)) {
throw new NotFoundHttpException("Event not found: " . $cardEventForm->event_id);
throw new NotFoundHttpException('Event not found: ' . $cardEventForm->event_id);
}
if ( isset($event->deleted_at)){
throw new BadRequestHttpException("Event deleted", self::EVENT_UNAVAILABLE);
throw new BadRequestHttpException('Event deleted', self::EVENT_UNAVAILABLE);
}
if (!$event->hasFreeSeats()) {
throw new BadRequestHttpException("No free seats", self::NO_FREE_SEATS);
throw new BadRequestHttpException('No free seats', self::NO_FREE_SEATS);
}
$eventType = $event->eventType;
if (!isset($eventType)) {
throw new ServerErrorHttpException("Event type not found", self::EVENT_TYPE_NOT_FOUND);
throw new ServerErrorHttpException('Event type not found', self::EVENT_TYPE_NOT_FOUND);
}
$selectedTicket = $eventType->findTicketAllowingEventType($activeTickets);
if (!isset($selectedTicket)) {
throw new NotFoundHttpException("Ticket not found", self::TICKET_INSUFFICIENT);
throw new NotFoundHttpException('Ticket not found', self::TICKET_INSUFFICIENT);
}
if ($selectedTicket->hasOpenReservationCount()) {
@@ -106,16 +112,17 @@ class EventRegistrationManager extends \yii\base\Object
$registration->id_event = $event->id;
$registration->id_card = $card->id_card;
$registration->id_ticket = $selectedTicket->id_ticket;
$registration->id_customer = $card->customer->id_customer;
try {
$registration->save(false);
} catch (\Throwable $exception) {
throw new ServerErrorHttpException("Failed to save", self::UNKNOWN_ERROR);
} catch (Throwable $exception) {
throw new ServerErrorHttpException('Failed to save', self::UNKNOWN_ERROR);
}
$cardEventForm->registration = $registration;
}
$tx->commit();
} catch (\Exception $exception) {
} catch (Exception $exception) {
$tx->rollBack();
throw $exception;
}
@@ -126,21 +133,21 @@ class EventRegistrationManager extends \yii\base\Object
{
$query = new Query();
$query->select([
"event_registration.id as event_registration_id",
"event_registration.created_at as event_registration_created_at",
"event_registration.canceled_at as event_registration_canceled_at",
"event_registration.deleted_at as event_registration_deleted_at",
"card.id_card as card_id_card",
"card.number as card_number",
"customer.id_customer as customer_id_customer",
"customer.name as customer_name",
"customer.email as customer_email",
'event_registration.id as event_registration_id',
'event_registration.created_at as event_registration_created_at',
'event_registration.canceled_at as event_registration_canceled_at',
'event_registration.deleted_at as event_registration_deleted_at',
'card.id_card as card_id_card',
'card.number as card_number',
'customer.id_customer as customer_id_customer',
'customer.name as customer_name',
'customer.email as customer_email',
]);
$query->from(EventRegistration::tableName());
$query->innerJoin(Event::tableName(), "event_registration.id_event = event.id");
$query->innerJoin(Card::tableName(), "event_registration.id_card = card.id_card");
$query->innerJoin(Customer::tableName(), "customer.id_customer_card = card.id_card");
$query->andWhere(["event_registration.id_event" => $idEvent]);
$query->innerJoin(Event::tableName(), 'event_registration.id_event = event.id');
$query->innerJoin(Card::tableName(), 'event_registration.id_card = card.id_card');
$query->innerJoin(Customer::tableName(), 'customer.id_customer_card = card.id_card');
$query->andWhere(['event_registration.id_event' => $idEvent]);
return $query;
}
@@ -148,21 +155,21 @@ class EventRegistrationManager extends \yii\base\Object
/**
* @param $idRegistration
* @return array|EventRegistration|\yii\db\ActiveRecord|null
* @return array|EventRegistration|ActiveRecord|null
* @throws NotFoundHttpException
*/
public function loadRegistration($idRegistration)
{
$registration = EventRegistration::find()->andWhere(['id' => $idRegistration])->one();
if ($registration == null) {
if ( $registration === null) {
throw new NotFoundHttpException('The requested registration does not exist.');
}
return $registration;
}
/**
* @param \common\models\EventRegistration $registration
* @param EventRegistration $registration
* @throws ServerErrorHttpException
*/
public function cancelRegistration($registration)
@@ -181,7 +188,7 @@ class EventRegistrationManager extends \yii\base\Object
}
/**
* @param \common\models\EventRegistration $registration
* @param EventRegistration $registration
* @throws ServerErrorHttpException
*/
public function deleteRegistration($registration)
@@ -202,13 +209,13 @@ class EventRegistrationManager extends \yii\base\Object
/**
* Delete an event
* @param \common\models\Event $event
* @throws \yii\db\Exception
* @throws \Exception
* @param Event $event
* @throws Exception
* @throws Throwable
*/
public function deleteEvent($event)
{
$db = \Yii::$app->db;
$db = Yii::$app->db;
$tx = $db->beginTransaction();
try {
@@ -217,20 +224,20 @@ class EventRegistrationManager extends \yii\base\Object
// if even has no registrations
// we can simply delete it from db
// ////////////////////////////////
if (count($registrations) == 0) {
if ( count($registrations) === 0 ) {
$event->delete();
} else {
$event->deleted_at = date('Y-m-d H:i:s');
$event->save(false);
foreach ($registrations as $registration) {
if (!isset($registration->deleted_at)) {
/** @var \common\models\EventRegistration $registration */
/** @var EventRegistration $registration */
$this->deleteRegistration($registration);
}
}
}
$tx->commit();
} catch (\Exception $e) {
} catch (Exception $e) {
$tx->rollBack();
throw $e;
}

View File

@@ -1,6 +1,7 @@
<?php
namespace common\models;
use common\components\Helper;
use yii\base\Model;
/**
* Created by IntelliJ IDEA.
@@ -9,7 +10,7 @@ use common\components\Helper;
* Time: 6:14
*/
class CardEventRegistrationForm extends \yii\base\Model
class CardEventRegistrationForm extends Model
{
public $card_number;
public $event_id;
@@ -41,4 +42,4 @@ class CardEventRegistrationForm extends \yii\base\Model
}
}
}

View File

@@ -211,13 +211,13 @@ class Customer extends BaseFitnessActiveRecord implements IdentityInterface
/**
* Finds an identity by the given ID.
* @param string|integer $id the ID to be looked for
* @return void the identity object that matches the given ID.
* @return Customer
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentity($id)
{
Customer::findOne($id);
return self::findOne(['email' => $id]);
}
/**

View File

@@ -111,18 +111,18 @@ class Event extends \yii\db\ActiveRecord
}
public function getEventType(){
return $this->hasOne(EventType::className(),['id' => 'id_event_type']);
return $this->hasOne($this->getEventTypeClass(),['id' => 'id_event_type']);
}
public function getTrainer(){
return $this->hasOne(Trainer::className(),['id' => 'id_trainer']);
return $this->hasOne($this->getTrainerClass(),['id' => 'id_trainer']);
}
/**
* @return Room|\yii\db\ActiveQuery
*/
public function getRoom() {
return $this->hasOne(Room::className(),['id' => 'id_room']);
return $this->hasOne($this->getRoomClass(),['id' => 'id_room']);
}
/**
@@ -139,16 +139,24 @@ class Event extends \yii\db\ActiveRecord
return $this->hasMany(EventRegistration::className(),['id_event' => 'id']);
}
/**
* @return \common\models\EventRegistration[]|\yii\db\ActiveQuery
*/
public function getActiveEventRegistrations(){
return $this->hasMany(EventRegistration::className(),['id_event' => 'id'])->andWhere(
[
'event_registration.canceled_at' => null,
'event_registration.deleted_at' => null,
]
);
}
/**
* @return \common\models\EventRegistration[]|\yii\db\ActiveQuery
*/
public function getEventRegistrationCount(){
return sizeof($this->getEventRegistrations()
->andWhere( [
'canceled_at' => null,
'deleted_at' => null,
])
->all());
return sizeof($this->getActiveEventRegistrations()->all());
}
public function getOpenSeatCount(){
@@ -183,4 +191,16 @@ class Event extends \yii\db\ActiveRecord
return true;
}
protected function getTrainerClass(){
return Trainer::class;
}
protected function getEventTypeClass(){
return EventType::class;
}
protected function getRoomClass(){
return Room::class;
}
}

View File

@@ -65,4 +65,14 @@ class EventRegistration extends \yii\db\ActiveRecord
],
parent::behaviors());
}
public function getEvent(){
return $this->hasOne(Event::class,['id' => 'id_event']);
}
public function getCustomer(){
return $this->hasOne(Customer::class,['id' => 'id_customer']);
}
}