diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php index cc1f8726..dd44452a 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php @@ -393,4 +393,33 @@ final class OAuth2SummitTracksApiController extends OAuth2ProtectedController return $this->error500($ex); } } + + /** + * @param $summit_id + * @param $track_id + * @return mixed + */ + public function deleteTrackBySummit($summit_id, $track_id){ + try { + $summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $this->track_service->deleteTrack($summit, $track_id); + + return $this->deleted(); + } + 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); + } + } } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 1ceeecec..2de69a49 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -350,6 +350,7 @@ Route::group([ Route::group(['prefix' => '{track_id}'], function () { Route::get('', 'OAuth2SummitTracksApiController@getTrackBySummit'); Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitTracksApiController@updateTrackBySummit']); + Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitTracksApiController@deleteTrackBySummit']); }); }); diff --git a/app/ModelSerializers/SerializerRegistry.php b/app/ModelSerializers/SerializerRegistry.php index 25867395..dfcce9e1 100644 --- a/app/ModelSerializers/SerializerRegistry.php +++ b/app/ModelSerializers/SerializerRegistry.php @@ -36,9 +36,7 @@ use App\ModelSerializers\Marketplace\SpokenLanguageSerializer; use App\ModelSerializers\Marketplace\SupportChannelTypeSerializer; use App\ModelSerializers\Software\OpenStackComponentSerializer; use App\ModelSerializers\Software\OpenStackReleaseSerializer; -use App\ModelSerializers\Summit\Presentation\PresentationCategoryAllowedTagSerializer; use Libs\ModelSerializers\IModelSerializer; -use models\summit\SummitRegistrationPromoCode; use ModelSerializers\ChatTeams\ChatTeamInvitationSerializer; use ModelSerializers\ChatTeams\ChatTeamMemberSerializer; use ModelSerializers\ChatTeams\ChatTeamPushNotificationMessageSerializer; @@ -51,7 +49,6 @@ use ModelSerializers\Locations\SummitVenueFloorSerializer; use ModelSerializers\Locations\SummitVenueRoomSerializer; use ModelSerializers\Locations\SummitVenueSerializer; use App\ModelSerializers\Marketplace\ApplianceSerializer; -use ModelSerializers\SummitScheduleEmptySpotSerializer; /** * Class SerializerRegistry * @package ModelSerializers @@ -90,7 +87,6 @@ final class SerializerRegistry $this->registry['PresentationType'] = PresentationTypeSerializer::class; $this->registry['SummitTicketType'] = SummitTicketTypeSerializer::class; $this->registry['PresentationCategory'] = PresentationCategorySerializer::class; - $this->registry['PresentationCategoryAllowedTag'] = PresentationCategoryAllowedTagSerializer::class; $this->registry['PresentationCategoryGroup'] = PresentationCategoryGroupSerializer::class; $this->registry['PrivatePresentationCategoryGroup'] = PrivatePresentationCategoryGroupSerializer::class; $this->registry['Tag'] = TagSerializer::class; diff --git a/app/ModelSerializers/Summit/Presentation/PresentationCategoryAllowedTagSerializer.php b/app/ModelSerializers/Summit/Presentation/PresentationCategoryAllowedTagSerializer.php deleted file mode 100644 index 974910df..00000000 --- a/app/ModelSerializers/Summit/Presentation/PresentationCategoryAllowedTagSerializer.php +++ /dev/null @@ -1,42 +0,0 @@ - 'is_default:json_boolean', - ]; - - /** - * @param null $expand - * @param array $fields - * @param array $relations - * @param array $params - * @return array - */ - public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [] ) - { - $values = parent::serialize($expand, $fields, $relations, $params); - $allowed_tag = $this->object; - if(!$allowed_tag instanceof PresentationCategoryAllowedTag) return []; - $values['tag'] = $allowed_tag->getTag()->getTag(); - $values['id'] = $allowed_tag->getTag()->getId(); - return $values; - } -} \ No newline at end of file diff --git a/app/ModelSerializers/Summit/Presentation/PresentationCategorySerializer.php b/app/ModelSerializers/Summit/Presentation/PresentationCategorySerializer.php index 41376036..76d24fee 100644 --- a/app/ModelSerializers/Summit/Presentation/PresentationCategorySerializer.php +++ b/app/ModelSerializers/Summit/Presentation/PresentationCategorySerializer.php @@ -53,8 +53,8 @@ final class PresentationCategorySerializer extends SilverStripeSerializer } $values['track_groups'] = $groups; - foreach($category->getAllowedTags() as $allowed_tag){ - $allowed_tag[] = intval($allowed_tag->getTag()->getId()); + foreach($category->getAllowedTags() as $tag){ + $allowed_tag[] = $tag->getId(); } $values['track_groups'] = $groups; @@ -78,8 +78,8 @@ final class PresentationCategorySerializer extends SilverStripeSerializer case 'allowed_tags': { $allowed_tag = []; unset($values['allowed_tags']); - foreach ($category->getAllowedTags() as $allowed_tag) { - $allowed_tag[] = SerializerRegistry::getInstance()->getSerializer($allowed_tag)->serialize(null, [], ['none']); + foreach ($category->getAllowedTags() as $tag) { + $allowed_tag[] = SerializerRegistry::getInstance()->getSerializer($tag)->serialize(null, [], ['none']); } $values['allowed_tags'] = $allowed_tag; } diff --git a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php b/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php index c69ac2bc..3e4cb947 100644 --- a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php +++ b/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php @@ -11,7 +11,6 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ -use App\Models\Foundation\Summit\Events\Presentations\PresentationCategoryAllowedTag; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Mapping AS ORM; use Doctrine\ORM\Query\ResultSetMappingBuilder; @@ -143,12 +142,14 @@ class PresentationCategory extends SilverstripeBaseModel private $groups; /** - * @ORM\OneToMany(targetEntity="App\Models\Foundation\Summit\Events\Presentations\PresentationCategoryAllowedTag", mappedBy="track", cascade={"persist"}, orphanRemoval=true) - * @var PresentationCategoryAllowedTag[] + * @ORM\ManyToMany(targetEntity="models\main\Tag", cascade={"persist"}) + * @ORM\JoinTable(name="PresentationCategory_AllowedTags", + * joinColumns={@ORM\JoinColumn(name="PresentationCategoryID", referencedColumnName="ID")}, + * inverseJoinColumns={@ORM\JoinColumn(name="TagID", referencedColumnName="ID")} + * ) */ protected $allowed_tags; - public function __construct() { parent::__construct(); @@ -179,7 +180,7 @@ class PresentationCategory extends SilverstripeBaseModel } /** - * @return PresentationCategoryAllowedTag[] + * @return Tag[] */ public function getAllowedTags(){ return $this->allowed_tags; @@ -296,24 +297,21 @@ class PresentationCategory extends SilverstripeBaseModel */ public function getRelatedPublishedSummitEvents(){ $query = <<getEM()); - $rsm->addRootEntityFromClassMetadata(\models\summit\SummitEvent::class, 'e'); + $native_query = $this->getEM()->createQuery($query); - // build rsm here - $native_query = $this->getEM()->createNativeQuery($query, $rsm); + $native_query->setParameter("summit", $this->summit); + $native_query->setParameter("track", $this); - $native_query->setParameter("summit_id", $this->summit->getId()); - $native_query->setParameter("track_id", $this->id); - - return $native_query->getResult(); + $res = $native_query->getResult(); + return $res; } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategoryAllowedTag.php b/app/Models/Foundation/Summit/Events/Presentations/PresentationCategoryAllowedTag.php deleted file mode 100644 index 7f522929..00000000 --- a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategoryAllowedTag.php +++ /dev/null @@ -1,93 +0,0 @@ -tag; - } - - /** - * @param Tag $tag - */ - public function setTag($tag) - { - $this->tag = $tag; - } - - /** - * @return PresentationCategory - */ - public function getTrack() - { - return $this->track; - } - - /** - * @param PresentationCategory $track - */ - public function setTrack($track) - { - $this->track = $track; - } - - /** - * @return bool - */ - public function isDefault() - { - return $this->is_default; - } - - /** - * @param bool $is_default - */ - public function setIsDefault($is_default) - { - $this->is_default = $is_default; - } -} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Factories/PresentationCategoryFactory.php b/app/Models/Foundation/Summit/Factories/PresentationCategoryFactory.php index a813b7cf..1031a1c8 100644 --- a/app/Models/Foundation/Summit/Factories/PresentationCategoryFactory.php +++ b/app/Models/Foundation/Summit/Factories/PresentationCategoryFactory.php @@ -40,7 +40,7 @@ final class PresentationCategoryFactory if(isset($data['title'])) $track->setTitle(trim($data['title'])); - if(isset($data['code'])) + if(isset($data['code']) && !empty($data['code'])) $track->setCode(trim($data['code'])); if(isset($data['description'])) diff --git a/app/Services/Model/ISummitTrackService.php b/app/Services/Model/ISummitTrackService.php index f36d4150..7e130262 100644 --- a/app/Services/Model/ISummitTrackService.php +++ b/app/Services/Model/ISummitTrackService.php @@ -47,6 +47,6 @@ interface ISummitTrackService * @throws EntityNotFoundException * @throws ValidationException */ - public function deleteEventType(Summit $summit, $track_id); + public function deleteTrack(Summit $summit, $track_id); } \ No newline at end of file diff --git a/app/Services/Model/SummitTrackService.php b/app/Services/Model/SummitTrackService.php index 8318ce0d..2a4c5711 100644 --- a/app/Services/Model/SummitTrackService.php +++ b/app/Services/Model/SummitTrackService.php @@ -60,9 +60,11 @@ final class SummitTrackService implements ISummitTrackService { return $this->tx_service->transaction(function () use ($summit, $data) { - $former_track = $summit->getPresentationCategoryByCode($data['code']); - if (!is_null($former_track)) - throw new ValidationException(sprintf("track id %s already has code %s assigned on summit id %s", $former_track->getId(), $data['code'], $summit->getId())); + if (!empty($data['code'])) { + $former_track = $summit->getPresentationCategoryByCode(trim($data['code'])); + if (!is_null($former_track)) + throw new ValidationException(sprintf("track id %s already has code %s assigned on summit id %s", $former_track->getId(), $data['code'], $summit->getId())); + } $former_track = $summit->getPresentationCategoryByTitle($data['title']); if (!is_null($former_track)) @@ -95,7 +97,7 @@ final class SummitTrackService implements ISummitTrackService sprintf("track id %s does not belong to summit id %s", $track_id, $summit->getId()) ); - if (isset($data['code'])) { + if (isset($data['code']) && !empty($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())); @@ -121,8 +123,28 @@ final class SummitTrackService implements ISummitTrackService * @throws EntityNotFoundException * @throws ValidationException */ - public function deleteEventType(Summit $summit, $track_id) + public function deleteTrack(Summit $summit, $track_id) { - // TODO: Implement deleteEventType() method. + return $this->tx_service->transaction(function () use ($summit, $track_id) { + + $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()) + ); + + $summit_events = $track->getRelatedPublishedSummitEvents(); + + if(count($summit_events) > 0){ + throw new ValidationException + ( + sprintf("track id %s could not be deleted bc its assigned to published events on summit id %s", $track_id, $summit->getId()) + ); + } + + $this->repository->delete($track); + }); } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 2cb2181c..328a4358 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -629,7 +629,7 @@ class ApiEndpointsSeeder extends Seeder ], ], [ - 'name' => 'add-tracks', + 'name' => 'add-track', 'route' => '/api/v1/summits/{id}/tracks', 'http_method' => 'POST', 'scopes' => [ @@ -638,7 +638,7 @@ class ApiEndpointsSeeder extends Seeder ], ], [ - 'name' => 'update-tracks', + 'name' => 'update-track', 'route' => '/api/v1/summits/{id}/tracks/{track_id}', 'http_method' => 'PUT', 'scopes' => [ @@ -646,6 +646,15 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteSummitData, $current_realm) ], ], + [ + 'name' => 'delete-track', + 'route' => '/api/v1/summits/{id}/tracks/{track_id}', + 'http_method' => 'DELETE', + 'scopes' => [ + sprintf(SummitScopes::WriteTracksData, $current_realm), + sprintf(SummitScopes::WriteSummitData, $current_realm) + ], + ], // track groups array( 'name' => 'get-track-groups', diff --git a/tests/OAuth2TracksApiTest.php b/tests/OAuth2TracksApiTest.php index 58d40ad6..28bc4a60 100644 --- a/tests/OAuth2TracksApiTest.php +++ b/tests/OAuth2TracksApiTest.php @@ -29,7 +29,7 @@ final class OAuth2TracksApiTest extends ProtectedApiTest 'per_page' => 10, 'filter' => 'title=@con', 'order' => '+code', - 'expand' => 'track_groups' + 'expand' => 'track_groups,allowed_tags' ]; $headers = [ @@ -134,7 +134,7 @@ final class OAuth2TracksApiTest extends ProtectedApiTest $data = [ 'title' => $name, 'description' => 'test desc', - 'code' => 'SM', + 'code' => '', ]; $headers = [ @@ -203,4 +203,62 @@ final class OAuth2TracksApiTest extends ProtectedApiTest $this->assertTrue(!empty($track->code) && $track->code == 'SMX'); return $track; } + + /** + * @param int $summit_id + * @return mixed + */ + public function testDeleteNewTrack($summit_id = 23){ + + $new_track = $this->testAddTrack($summit_id); + + $params = [ + 'id' => $summit_id, + 'track_id' => $new_track->id + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "DELETE", + "OAuth2SummitTracksApiController@deleteTrackBySummit", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(204); + } + + public function testDeleteOldTrack($summit_id = 23){ + + $params = [ + 'id' => $summit_id, + 'track_id' => 155 + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "DELETE", + "OAuth2SummitTracksApiController@deleteTrackBySummit", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(412); + } } \ No newline at end of file