Add to my schedule optimization
updated algorithm to check if an event is or not on attendee schedule Change-Id: I5b95216901c164af76734f3002e03856e028ca2c
This commit is contained in:
parent
ab9f0d0b61
commit
b0390a5924
@ -172,10 +172,11 @@ class SummitAttendee extends SilverstripeBaseModel
|
||||
*/
|
||||
public function add2Schedule(SummitEvent $event)
|
||||
{
|
||||
$schedule = $this->getScheduleByEvent($event);
|
||||
|
||||
if($schedule !== false)
|
||||
throw new ValidationException(sprintf('Event %s already belongs to attendee %s schedule.', $event->getId(), $this->getId()));
|
||||
if($this->isOnSchedule($event))
|
||||
throw new ValidationException
|
||||
(
|
||||
sprintf('Event %s already belongs to attendee %s schedule.', $event->getId(), $this->getId())
|
||||
);
|
||||
|
||||
$schedule = new SummitAttendeeSchedule;
|
||||
|
||||
@ -192,12 +193,12 @@ class SummitAttendee extends SilverstripeBaseModel
|
||||
public function removeFromSchedule(SummitEvent $event)
|
||||
{
|
||||
$schedule = $this->getScheduleByEvent($event);
|
||||
if($schedule === false)
|
||||
|
||||
if(is_null($schedule))
|
||||
throw new ValidationException
|
||||
(
|
||||
sprintf('Event %s does not belongs to attendee %s schedule.', $event->getId(), $this->getId())
|
||||
);
|
||||
|
||||
$this->schedule->removeElement($schedule);
|
||||
$schedule->clearAttendee();
|
||||
}
|
||||
@ -208,17 +209,37 @@ class SummitAttendee extends SilverstripeBaseModel
|
||||
*/
|
||||
public function isOnSchedule(SummitEvent $event)
|
||||
{
|
||||
return $this->getScheduleByEvent($event) !== false;
|
||||
$sql = <<<SQL
|
||||
SELECT COUNT(SummitEventID) AS QTY
|
||||
FROM SummitAttendee_Schedule
|
||||
INNER JOIN SummitEvent ON SummitEvent.ID = SummitAttendee_Schedule.SummitEventID
|
||||
WHERE SummitAttendeeID = :attendee_id AND SummitEvent.Published = 1 AND SummitEvent.ID = :event_id
|
||||
SQL;
|
||||
|
||||
$stmt = $this->prepareRawSQL($sql);
|
||||
$stmt->execute([
|
||||
'attendee_id' => $this->getId(),
|
||||
'event_id' => $event->getId()
|
||||
]);
|
||||
$res = $stmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||
return count($res) > 0 ? intval($res[0]) > 0 : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SummitEvent $event
|
||||
* @return SummitAttendeeSchedule
|
||||
* @return null| SummitAttendeeSchedule
|
||||
*/
|
||||
public function getScheduleByEvent(SummitEvent $event){
|
||||
return $this->schedule->filter(function($e) use($event){
|
||||
return $e->getEvent()->getId() == $event->getId();
|
||||
})->first();
|
||||
|
||||
$query = $this->createQuery("SELECT s from models\summit\SummitAttendeeSchedule s
|
||||
JOIN s.attendee a
|
||||
JOIN s.event e
|
||||
WHERE a.id = :attendee_id and e.published = 1 and e.id = :event_id
|
||||
");
|
||||
return $query
|
||||
->setParameter('attendee_id', $this->getIdentifier())
|
||||
->setParameter('event_id', $event->getIdentifier())
|
||||
->getSingleResult();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,7 +250,7 @@ class SummitAttendee extends SilverstripeBaseModel
|
||||
{
|
||||
$schedule = $this->getScheduleByEvent($event);
|
||||
|
||||
if($schedule === false)
|
||||
if(is_null($schedule))
|
||||
throw new ValidationException(sprintf('Event %s does not belongs to attendee %s schedule.', $event->ID, $this->ID));
|
||||
$schedule->setIsCheckedIn(true);
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ final class OAuth2SummitApiTest extends ProtectedApiTest
|
||||
$this->assertTrue(!is_null($attendee));
|
||||
}
|
||||
|
||||
public function testCurrentSummitMyAttendeeAddToSchedule($event_id = 7202, $summit_id = 6)
|
||||
public function testCurrentSummitMyAttendeeAddToSchedule($event_id = 16645, $summit_id = 7)
|
||||
{
|
||||
$params = array
|
||||
(
|
||||
@ -266,9 +266,9 @@ final class OAuth2SummitApiTest extends ProtectedApiTest
|
||||
|
||||
public function testCurrentSummitMyAttendeeScheduleUnset()
|
||||
{
|
||||
$event_id = 7863;
|
||||
$summit_id = 6;
|
||||
$this->testCurrentSummitMyAttendeeAddToSchedule($event_id, $summit_id);
|
||||
$event_id = 16645;
|
||||
$summit_id = 7;
|
||||
//$this->testCurrentSummitMyAttendeeAddToSchedule($event_id, $summit_id);
|
||||
$params = array
|
||||
(
|
||||
'id' => $summit_id,
|
||||
|
@ -50,7 +50,7 @@ class AccessTokenServiceStub implements IAccessTokenService
|
||||
$url . '/summits/read-notifications',
|
||||
);
|
||||
|
||||
return AccessToken::createFromParams('123456789', implode(' ', $scopes), '1', $realm, '1','11624', 3600, 'WEB_APPLICATION', '', '');
|
||||
return AccessToken::createFromParams('123456789', implode(' ', $scopes), '1', $realm, '1','13867', 3600, 'WEB_APPLICATION', '', '');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user