Added new endpoint to get my presentations per role and

per summit:

GET /api/v1/speakers/me/presentations/{role}/summits/{summit_id}

where role could be

* creator
* speaker
* moderator

required scopes

* /summits/read
* /summits/read/all

Change-Id: Ib454d90a1dcea66f5f1223b4ffa6a5cc101eda72
This commit is contained in:
Sebastian Marcet 2018-11-21 14:24:45 -03:00
parent 4c14b7c778
commit 51a67b1782
6 changed files with 157 additions and 17 deletions

View File

@ -927,7 +927,7 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
/**
* @param $role
* @param $selection_plan_id
* @return mixed
* @return mixedf
*/
public function getMySpeakerPresentationsByRoleAndBySelectionPlan($role, $selection_plan_id)
{
@ -983,6 +983,65 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
}
}
/**
* @param $role
* @param $summit_id
* @return mixed
*/
public function getMySpeakerPresentationsByRoleAndBySummit($role, $summit_id)
{
try {
$current_member_id = $this->resource_server_context->getCurrentUserExternalId();
if (is_null($current_member_id))
return $this->error403();
$member = $this->member_repository->getById($current_member_id);
if (is_null($member))
return $this->error403();
$speaker = $this->speaker_repository->getByMember($member);
if (is_null($speaker))
return $this->error403();
$summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id);
if (is_null($summit)) return $this->error404(['message' => 'missing selection summit']);
switch ($role) {
case 'creator':
$role = PresentationSpeaker::ROLE_CREATOR;
break;
case 'speaker':
$role = PresentationSpeaker::ROLE_SPEAKER;
break;
case 'moderator':
$role = PresentationSpeaker::ROLE_MODERATOR;
break;
}
$presentations = $speaker->getPresentationsBySummitAndRole($summit, $role);
$response = new PagingResponse
(
count($presentations),
count($presentations),
1,
1,
$presentations
);
return $this->ok($response->toArray($expand = Input::get('expand', '')));
} catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412(array($ex1->getMessage()));
} catch (EntityNotFoundException $ex2) {
Log::warning($ex2);
return $this->error404(array('message' => $ex2->getMessage()));
} catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $presentation_id
* @param $speaker_id

View File

@ -612,6 +612,13 @@ Route::group([
->where('role', 'creator|speaker|moderator');
});
});
Route::group(['prefix' => 'summits'], function(){
Route::group(['prefix' => '{summit_id}'], function(){
Route::get("", "OAuth2SummitSpeakersApiController@getMySpeakerPresentationsByRoleAndBySummit")
->where('role', 'creator|speaker|moderator');
});
});
});
});
});

View File

@ -405,7 +405,7 @@ class PresentationSpeaker extends SilverstripeBaseModel
/**
* @param SelectionPlan $selectionPlan
* @param $role
* @param string $role
* @return array
*/
public function getPresentationsBySelectionPlanAndRole(SelectionPlan $selectionPlan, $role){
@ -431,6 +431,33 @@ class PresentationSpeaker extends SilverstripeBaseModel
return [];
}
/**
* @param Summit $summit
* @param string $role
* @return array
*/
public function getPresentationsBySummitAndRole(Summit $summit, $role){
if($role == self::ROLE_SPEAKER){
$res = $this->presentations->filter(function(Presentation $presentation) use($summit){
if($presentation->getSummit()->getId() != $summit->getId()) return false;
if($presentation->getModeratorId() == $this->getId()) return false;
if($presentation->getCreatorId() == $this->getMemberId()) return false;
});
return $res->toArray();
}
if($role == self::ROLE_CREATOR){
return $summit->getCreatedPresentations($this);
}
if($role == self::ROLE_MODERATOR){
return $summit->getModeratedPresentationsBy($this);
}
return [];
}
/**
* @param Summit $summit
* @param string $role

View File

@ -853,38 +853,46 @@ class Summit extends SilverstripeBaseModel
/**
* @param PresentationSpeaker $speaker
* @param SelectionPlan $selectionPlan
* @param SelectionPlan|null $selectionPlan
* @return array
*/
public function getModeratedPresentationsBy(PresentationSpeaker $speaker, SelectionPlan $selectionPlan){
public function getModeratedPresentationsBy(PresentationSpeaker $speaker, SelectionPlan $selectionPlan = null){
$query = $this->createQuery("SELECT p from models\summit\Presentation p
JOIN p.summit s
JOIN p.moderator m
JOIN p.selection_plan sp
WHERE s.id = :summit_id and m.id = :moderator_id and sp.id = :selection_plan_id");
return $query
->setParameter('summit_id', $this->getIdentifier())
->setParameter('moderator_id', $speaker->getIdentifier())
->setParameter('selection_plan_id', $selectionPlan->getIdentifier())
->getResult();
$query = $query
->setParameter('summit_id', $this->getIdentifier())
->setParameter('moderator_id', $speaker->getIdentifier());
if(!is_null($selectionPlan)){
$query = $query->setParameter('selection_plan_id', $selectionPlan->getIdentifier());
}
return $query->getResult();
}
/**
* @param PresentationSpeaker $speaker
* @param SelectionPlan $selectionPlan
* @param SelectionPlan|null $selectionPlan
* @return array
*/
public function getCreatedPresentations(PresentationSpeaker $speaker, SelectionPlan $selectionPlan){
public function getCreatedPresentations(PresentationSpeaker $speaker, SelectionPlan $selectionPlan = null){
$query = $this->createQuery("SELECT p from models\summit\Presentation p
JOIN p.summit s
JOIN p.creator c
JOIN p.selection_plan sp
WHERE s.id = :summit_id and c.id = :creator_id and sp.id = :selection_plan_id");
return $query
$query = $query
->setParameter('summit_id', $this->getIdentifier())
->setParameter('creator_id', $speaker->getMemberId())
->setParameter('selection_plan_id', $selectionPlan->getIdentifier())
->getResult();
->setParameter('creator_id', $speaker->getMemberId());
if(!is_null($selectionPlan)){
$query = $query->setParameter('selection_plan_id', $selectionPlan->getIdentifier());
}
return $query->getResult();
}
/**

View File

@ -355,7 +355,7 @@ class ApiEndpointsSeeder extends Seeder
sprintf(SummitScopes::ReadAllSummitData, $current_realm)
],
),
array(
[
'name' => 'get-my-speaker-presentations-by-role-by-selection-plan',
'route' => '/api/v1/speakers/me/presentations/{role}/selection-plans/{selection_plan_id}',
'http_method' => 'GET',
@ -363,7 +363,18 @@ class ApiEndpointsSeeder extends Seeder
sprintf(SummitScopes::ReadSummitData, $current_realm),
sprintf(SummitScopes::ReadAllSummitData, $current_realm)
],
),
],
[
'name' => 'get-my-speaker-presentations-by-role-by-summit',
'route' => '/api/v1/speakers/me/presentations/{role}/summits/{summit_id}',
'http_method' => 'GET',
'scopes' => [
sprintf(SummitScopes::ReadSummitData, $current_realm),
sprintf(SummitScopes::ReadAllSummitData, $current_realm)
],
],
[
'name' => 'add-speaker-2-my-presentation',
'route' => '/api/v1/speakers/me/presentations/{presentation_id}/speakers/{speaker_id}',

View File

@ -594,4 +594,32 @@ final class OAuth2SpeakersApiTest extends ProtectedApiTest
$presentations = json_decode($content);
$this->assertTrue(!is_null($presentations));
}
public function testGetMySpeakerPresentationsByRoleAndBySummit($summit_id = 7)
{
$params = [
'role' => 'speaker',
'summit_id' => $summit_id,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitSpeakersApiController@getMySpeakerPresentationsByRoleAndBySummit",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$presentations = json_decode($content);
$this->assertTrue(!is_null($presentations));
}
}