Added endpoint update track by summit
PUT /api/v1/summits/{id}/tracks/{track_id} Payload * title (sometimes|string|max:50) * description (sometimes|string|max:500) * code (sometimes|string|max:5) * session_count (sometimes|integer) * alternate_count (sometimes|integer) * lightning_count (sometimes|integer) * lightning_alternate_count (sometimes|integer) * voting_visible (sometimes|boolean) * chair_visible (sometimes|boolean) Change-Id: I70ef15714b3620a351f138a7ea0bf723b70963f1
This commit is contained in:
parent
e602510d37
commit
68cab449dc
45
app/Events/TrackUpdated.php
Normal file
45
app/Events/TrackUpdated.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php namespace App\Events;
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use models\summit\PresentationCategory;
|
||||
/**
|
||||
* Class TrackUpdated
|
||||
* @package App\Events
|
||||
*/
|
||||
final class TrackUpdated extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @var PresentationCategory
|
||||
*/
|
||||
private $track;
|
||||
|
||||
/**
|
||||
* TrackUpdated constructor.
|
||||
* @param PresentationCategory $track
|
||||
*/
|
||||
public function __construct(PresentationCategory $track)
|
||||
{
|
||||
$this->track = $track;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PresentationCategory
|
||||
*/
|
||||
public function getTrack()
|
||||
{
|
||||
return $this->track;
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php namespace App\Factories\EntityEvents;
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use App\Events\TrackUpdated;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use models\summit\SummitEntityEvent;
|
||||
|
||||
/**
|
||||
* Class TrackUpdatedEntityEventFactory
|
||||
* @package App\Factories\EntityEvents
|
||||
*/
|
||||
final class TrackUpdatedEntityEventFactory
|
||||
{
|
||||
/**
|
||||
* @param TrackUpdated $event
|
||||
* @return SummitEntityEvent[]
|
||||
*/
|
||||
public static function build(TrackUpdated $event){
|
||||
$list = [];
|
||||
$resource_server_context = App::make(\models\oauth2\IResourceServerContext::class);
|
||||
$member_repository = App::make(\models\main\IMemberRepository::class);
|
||||
$owner_id = $resource_server_context->getCurrentUserExternalId();
|
||||
if(is_null($owner_id)) $owner_id = 0;
|
||||
|
||||
foreach($event->getTrack()->getRelatedPublishedSummitEvents() as $summit_event) {
|
||||
|
||||
$entity_event = new SummitEntityEvent;
|
||||
$entity_event->setEntityClassName($summit_event->getClassName());
|
||||
$entity_event->setEntityId($summit_event->getId());
|
||||
$entity_event->setType('UPDATE');
|
||||
|
||||
if ($owner_id > 0) {
|
||||
$member = $member_repository->getById($owner_id);
|
||||
$entity_event->setOwner($member);
|
||||
}
|
||||
|
||||
$entity_event->setSummit($summit_event->getSummit());
|
||||
$entity_event->setMetadata('');
|
||||
|
||||
$list[] = $entity_event;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
@ -337,4 +337,60 @@ final class OAuth2SummitTracksApiController extends OAuth2ProtectedController
|
||||
return $this->error500($ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $summit_id
|
||||
* @param $track_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateTrackBySummit($summit_id, $track_id){
|
||||
try {
|
||||
if(!Request::isJson()) return $this->error403();
|
||||
$data = Input::json();
|
||||
|
||||
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
||||
if (is_null($summit)) return $this->error404();
|
||||
|
||||
$rules = [
|
||||
'title' => 'sometimes|string|max:50',
|
||||
'description' => 'sometimes|string|max:500',
|
||||
'code' => 'sometimes|string|max:5',
|
||||
'session_count' => 'sometimes|integer',
|
||||
'alternate_count' => 'sometimes|integer',
|
||||
'lightning_count' => 'sometimes|integer',
|
||||
'lightning_alternate_count' => 'sometimes|integer',
|
||||
'voting_visible' => 'sometimes|boolean',
|
||||
'chair_visible' => 'sometimes|boolean',
|
||||
];
|
||||
|
||||
// Creates a Validator instance and validates the data.
|
||||
$validation = Validator::make($data->all(), $rules);
|
||||
|
||||
if ($validation->fails()) {
|
||||
$messages = $validation->messages()->toArray();
|
||||
|
||||
return $this->error412
|
||||
(
|
||||
$messages
|
||||
);
|
||||
}
|
||||
|
||||
$track = $this->track_service->updateTrack($summit, $track_id, $data->all());
|
||||
|
||||
return $this->updated(SerializerRegistry::getInstance()->getSerializer($track)->serialize());
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -349,6 +349,7 @@ Route::group([
|
||||
Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitTracksApiController@addTrackBySummit']);
|
||||
Route::group(['prefix' => '{track_id}'], function () {
|
||||
Route::get('', 'OAuth2SummitTracksApiController@getTrackBySummit');
|
||||
Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitTracksApiController@updateTrackBySummit']);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
use App\Models\Foundation\Summit\Events\Presentations\PresentationCategoryAllowedTag;
|
||||
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
||||
use Doctrine\ORM\Mapping AS ORM;
|
||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||
use models\utils\SilverstripeBaseModel;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
/**
|
||||
@ -289,4 +290,30 @@ class PresentationCategory extends SilverstripeBaseModel
|
||||
$this->slug = preg_replace('/\s+/', '-', strtolower($clean_title));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SummitEvent[]
|
||||
*/
|
||||
public function getRelatedPublishedSummitEvents(){
|
||||
$query = <<<SQL
|
||||
SELECT SummitEvent.*
|
||||
FROM SummitEvent
|
||||
WHERE
|
||||
SummitEvent.Published = 1
|
||||
AND SummitEvent.SummitID = :summit_id
|
||||
AND SummitEvent.CategoryID = :track_id
|
||||
SQL;
|
||||
|
||||
$rsm = new ResultSetMappingBuilder($this->getEM());
|
||||
$rsm->addRootEntityFromClassMetadata(\models\summit\SummitEvent::class, 'e');
|
||||
|
||||
// build rsm here
|
||||
$native_query = $this->getEM()->createNativeQuery($query, $rsm);
|
||||
|
||||
$native_query->setParameter("summit_id", $this->summit->getId());
|
||||
$native_query->setParameter("track_id", $this->id);
|
||||
|
||||
return $native_query->getResult();
|
||||
|
||||
}
|
||||
}
|
@ -874,7 +874,7 @@ class Summit extends SilverstripeBaseModel
|
||||
*/
|
||||
public function getPresentationCategoryByCode($category_code){
|
||||
$criteria = Criteria::create();
|
||||
$criteria->where(Criteria::expr()->eq('code', intval($category_code)));
|
||||
$criteria->where(Criteria::expr()->eq('code', trim($category_code)));
|
||||
$category = $this->presentation_categories->matching($criteria)->first();
|
||||
return $category === false ? null:$category;
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ use App\Factories\EntityEvents\PresentationSpeakerUpdatedEntityEventFactory;
|
||||
use App\Factories\EntityEvents\SummitEventCreatedEntityEventFactory;
|
||||
use App\Factories\EntityEvents\SummitEventDeletedEntityEventFactory;
|
||||
use App\Factories\EntityEvents\SummitEventUpdatedEntityEventFactory;
|
||||
use App\Factories\EntityEvents\TrackUpdatedEntityEventFactory;
|
||||
use App\Services\Utils\SCPFileUploader;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
@ -134,5 +135,10 @@ final class EventServiceProvider extends ServiceProvider
|
||||
EntityEventPersister::persist_list(PresentationSpeakerDeletedEntityEventFactory::build($event));
|
||||
});
|
||||
|
||||
Event::listen(\App\Events\TrackUpdated::class, function($event)
|
||||
{
|
||||
EntityEventPersister::persist_list(TrackUpdatedEntityEventFactory::build($event));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php namespace App\Services\Model;
|
||||
|
||||
/**
|
||||
* Copyright 2018 OpenStack Foundation
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -11,13 +12,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
use App\Events\TrackUpdated;
|
||||
use App\Models\Foundation\Summit\Factories\PresentationCategoryFactory;
|
||||
use App\Models\Foundation\Summit\Repositories\ISummitTrackRepository;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use libs\utils\ITransactionService;
|
||||
use models\exceptions\EntityNotFoundException;
|
||||
use models\exceptions\ValidationException;
|
||||
use models\summit\PresentationCategory;
|
||||
use models\summit\Summit;
|
||||
|
||||
/**
|
||||
* Class SummitTrackService
|
||||
* @package App\Services\Model
|
||||
@ -81,7 +85,33 @@ final class SummitTrackService implements ISummitTrackService
|
||||
*/
|
||||
public function updateTrack(Summit $summit, $track_id, array $data)
|
||||
{
|
||||
// TODO: Implement updateTrack() method.
|
||||
return $this->tx_service->transaction(function () use ($summit, $track_id, $data) {
|
||||
|
||||
$track = $summit->getPresentationCategory($track_id);
|
||||
|
||||
if (is_null($track))
|
||||
throw new EntityNotFoundException
|
||||
(
|
||||
sprintf("track id %s does not belong to summit id %s", $track_id, $summit->getId())
|
||||
);
|
||||
|
||||
if (isset($data['code'])) {
|
||||
$former_track = $summit->getPresentationCategoryByCode($data['code']);
|
||||
if (!is_null($former_track) && $former_track->getId() != $track_id)
|
||||
throw new ValidationException(sprintf("track id %s already has code %s assigned on summit id %s", $former_track->getId(), $data['code'], $summit->getId()));
|
||||
}
|
||||
|
||||
if (isset($data['title'])) {
|
||||
$former_track = $summit->getPresentationCategoryByTitle($data['title']);
|
||||
if (!is_null($former_track) && $former_track->getId() != $track_id)
|
||||
throw new ValidationException(sprintf("track id %s already has title %s assigned on summit id %s", $former_track->getId(), $data['title'], $summit->getId()));
|
||||
}
|
||||
|
||||
return PresentationCategoryFactory::populate($track, $data);
|
||||
|
||||
Event::fire(new TrackUpdated($track));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -637,6 +637,15 @@ class ApiEndpointsSeeder extends Seeder
|
||||
sprintf(SummitScopes::WriteSummitData, $current_realm)
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'update-tracks',
|
||||
'route' => '/api/v1/summits/{id}/tracks/{track_id}',
|
||||
'http_method' => 'PUT',
|
||||
'scopes' => [
|
||||
sprintf(SummitScopes::WriteTracksData, $current_realm),
|
||||
sprintf(SummitScopes::WriteSummitData, $current_realm)
|
||||
],
|
||||
],
|
||||
// track groups
|
||||
array(
|
||||
'name' => 'get-track-groups',
|
||||
|
@ -17,6 +17,10 @@
|
||||
*/
|
||||
final class OAuth2TracksApiTest extends ProtectedApiTest
|
||||
{
|
||||
/**
|
||||
* @param int $summit_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function testGetTracksByTitle($summit_id = 23){
|
||||
$params = [
|
||||
|
||||
@ -50,6 +54,11 @@ final class OAuth2TracksApiTest extends ProtectedApiTest
|
||||
return $tracks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $summit_id
|
||||
* @param int $track_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function testGetTracksById($summit_id = 23, $track_id = 152){
|
||||
$params = [
|
||||
|
||||
@ -79,6 +88,9 @@ final class OAuth2TracksApiTest extends ProtectedApiTest
|
||||
return $track;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $summit_id
|
||||
*/
|
||||
public function testGetTracksByTitleCSV($summit_id = 23){
|
||||
$params = [
|
||||
|
||||
@ -109,6 +121,10 @@ final class OAuth2TracksApiTest extends ProtectedApiTest
|
||||
$this->assertTrue(!empty($csv));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $summit_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function testAddTrack($summit_id = 23){
|
||||
$params = [
|
||||
'id' => $summit_id,
|
||||
@ -143,4 +159,48 @@ final class OAuth2TracksApiTest extends ProtectedApiTest
|
||||
$this->assertTrue(!is_null($track));
|
||||
return $track;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $summit_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function testUpdateTrack($summit_id = 23){
|
||||
|
||||
$new_track = $this->testAddTrack($summit_id);
|
||||
|
||||
$params = [
|
||||
'id' => $summit_id,
|
||||
'track_id' => $new_track->id
|
||||
];
|
||||
|
||||
$name = str_random(16).'_track';
|
||||
$data = [
|
||||
'description' => 'test desc updated',
|
||||
'code' => 'SMX' ,
|
||||
];
|
||||
|
||||
$headers = [
|
||||
"HTTP_Authorization" => " Bearer " . $this->access_token,
|
||||
"CONTENT_TYPE" => "application/json"
|
||||
];
|
||||
|
||||
$response = $this->action(
|
||||
"PUT",
|
||||
"OAuth2SummitTracksApiController@updateTrackBySummit",
|
||||
$params,
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
$headers,
|
||||
json_encode($data)
|
||||
);
|
||||
|
||||
$content = $response->getContent();
|
||||
$this->assertResponseStatus(201);
|
||||
$track = json_decode($content);
|
||||
$this->assertTrue(!is_null($track));
|
||||
$this->assertTrue(!empty($track->description) && $track->description == 'test desc updated');
|
||||
$this->assertTrue(!empty($track->code) && $track->code == 'SMX');
|
||||
return $track;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user