diff --git a/app/Http/Controllers/Apis/Protected/Summit/EventTypeValidationRulesFactory.php b/app/Http/Controllers/Apis/Protected/Summit/EventTypeValidationRulesFactory.php index bb418890..cde9489e 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/EventTypeValidationRulesFactory.php +++ b/app/Http/Controllers/Apis/Protected/Summit/EventTypeValidationRulesFactory.php @@ -22,16 +22,17 @@ final class EventTypeValidationRulesFactory { /** * @param array $data + * @param boolean $update * @return array * @throws ValidationException */ - public static function build(array $data){ - if(!isset($data['class_name'])) + public static function build(array $data, $update = false){ + if (!isset($data['class_name'])) throw new ValidationException("class_name parameter is mandatory"); $class_name = trim($data['class_name']); - if(!in_array($class_name, SummitEventTypeConstants::$valid_class_names)){ + if (!in_array($class_name, SummitEventTypeConstants::$valid_class_names)) { throw new ValidationException( sprintf ( @@ -41,8 +42,14 @@ final class EventTypeValidationRulesFactory ); } + $name_rule = 'optional|string'; + if(!$update) { + + $name_rule = 'required|string'; + } + $base_rules = [ - 'name' => 'required|string', + 'name' => $name_rule, 'color' => 'sometimes|hex_color', 'black_out_times' => 'sometimes|boolean', 'use_sponsors' => 'sometimes|boolean', diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php index 5f48cbc5..e27359d3 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php @@ -225,4 +225,46 @@ final class OAuth2SummitsEventTypesApiController extends OAuth2ProtectedControll return $this->error500($ex); } } + + /** + * @param $summit_id + * @param $event_type_id + * @return mixed + */ + public function updateEventTypeBySummit($summit_id, $event_type_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 = EventTypeValidationRulesFactory::build($data->all(), true); + // 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 + ); + } + + $event_type = $this->event_type_service->updateEventType($summit, $event_type_id, $data->all()); + + return $this->updated(SerializerRegistry::getInstance()->getSerializer($event_type)->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); + } + } } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 67be8765..781fc700 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -293,9 +293,12 @@ Route::group([ }); // event types - Route::group(array('prefix' => 'event-types'), function () { + Route::group(['prefix' => 'event-types'], function () { Route::get('', 'OAuth2SummitsEventTypesApiController@getAllBySummit'); Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitsEventTypesApiController@addEventTypeBySummit']); + Route::group(['prefix' => '{event_type_id}'], function () { + Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitsEventTypesApiController@updateEventTypeBySummit']); + }); }); // external orders @@ -386,14 +389,14 @@ Route::group([ ], function () { // summits - Route::group(array('prefix' => 'summits'), function () { + Route::group(['prefix' => 'summits'], function () { - Route::group(array('prefix' => '{id}'), function () { + Route::group(['prefix' => '{id}'], function () { // events - Route::group(array('prefix' => 'events'), function () { + Route::group(['prefix' => 'events'], function () { - Route::group(array('prefix' => '{event_id}'), function () { + Route::group(['prefix' => '{event_id}'], function () { Route::post('/feedback', 'OAuth2SummitEventsApiController@addEventFeedbackByMember'); Route::put('/feedback', 'OAuth2SummitEventsApiController@updateEventFeedbackByMember'); }); diff --git a/app/Models/Foundation/Summit/Factories/SummitEventTypeFactory.php b/app/Models/Foundation/Summit/Factories/SummitEventTypeFactory.php index 12d88ce3..1482da8d 100644 --- a/app/Models/Foundation/Summit/Factories/SummitEventTypeFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitEventTypeFactory.php @@ -95,7 +95,8 @@ final class SummitEventTypeFactory break; } - $event_type->setType(trim($data['name'])); + if(isset($data['name'])) + $event_type->setType(trim($data['name'])); if(isset($data['color'])) $event_type->setColor(trim($data['color'])); diff --git a/app/Models/Foundation/Summit/Summit.php b/app/Models/Foundation/Summit/Summit.php index fbb00228..8bda8b56 100644 --- a/app/Models/Foundation/Summit/Summit.php +++ b/app/Models/Foundation/Summit/Summit.php @@ -728,6 +728,17 @@ class Summit extends SilverstripeBaseModel return $this->event_types->matching($criteria)->count() > 0; } + /** + * @param string $type + * @return SummitEventType|null + */ + public function getEventTypeByType($type){ + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq('type', $type)); + $event_type = $this->event_types->matching($criteria)->first(); + return $event_type === false ? null:$event_type; + } + /** * @param int $wifi_connection_id * @return SummitWIFIConnection|null diff --git a/app/Services/Model/ISummitEventTypeService.php b/app/Services/Model/ISummitEventTypeService.php index 80e6470b..1c5cd99c 100644 --- a/app/Services/Model/ISummitEventTypeService.php +++ b/app/Services/Model/ISummitEventTypeService.php @@ -15,7 +15,6 @@ use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; use models\summit\Summit; use models\summit\SummitEventType; - /** * Interface ISummitEventTypeService * @package App\Services\Model @@ -31,4 +30,14 @@ interface ISummitEventTypeService */ public function addEventType(Summit $summit, array $data); + /** + * @param Summit $summit + * @param int $event_type_id + * @param array $data + * @return SummitEventType + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateEventType(Summit $summit, $event_type_id, array $data); + } \ No newline at end of file diff --git a/app/Services/Model/SummitEventTypeService.php b/app/Services/Model/SummitEventTypeService.php index 69849da7..af73355a 100644 --- a/app/Services/Model/SummitEventTypeService.php +++ b/app/Services/Model/SummitEventTypeService.php @@ -19,7 +19,6 @@ use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; use models\summit\Summit; use models\summit\SummitEventType; - /** * Class SummitEventTypeService * @package App\Services @@ -78,4 +77,37 @@ final class SummitEventTypeService implements ISummitEventTypeService }); } + + /** + * @param Summit $summit + * @param int $event_type_id + * @param array $data + * @return SummitEventType + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateEventType(Summit $summit, $event_type_id, array $data) + { + return $this->tx_manager->transaction(function() use($summit, $event_type_id, $data){ + + $type = isset($data['name']) ? trim($data['name']) : null; + + $event_type = $summit->getEventType($event_type_id); + + if(is_null($event_type)) + throw new EntityNotFoundException(sprintf("event type id %s does not belongs to summit id %s", $event_type_id, $summit->getId())); + + if(!empty($type)) { + $old_event_type = $summit->getEventTypeByType($type); + if(!is_null($old_event_type) && $old_event_type->getId() != $event_type->getId()){ + throw new ValidationException(sprintf("name %s already belongs to another event type id %s", $type, $old_event_type->getId())); + } + } + + $event_type = SummitEventTypeFactory::populate($event_type, $summit, $data); + + return $event_type; + + }); + } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index ca6b892d..d0ae3081 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -555,6 +555,15 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteSummitData, $current_realm) ], ], + [ + 'name' => 'update-event-type', + 'route' => '/api/v1/summits/{id}/event-types/{event_type_id}', + 'http_method' => 'PUT', + 'scopes' => [ + sprintf(SummitScopes::WriteEventTypeData, $current_realm), + sprintf(SummitScopes::WriteSummitData, $current_realm) + ], + ], //tracks array( 'name' => 'get-tracks', diff --git a/tests/OAuth2EventTypesApiTest.php b/tests/OAuth2EventTypesApiTest.php index a4d1df3f..1e2a612d 100644 --- a/tests/OAuth2EventTypesApiTest.php +++ b/tests/OAuth2EventTypesApiTest.php @@ -113,4 +113,43 @@ final class OAuth2EventTypesApiTest extends ProtectedApiTest return $event_type; } + public function testUpdateEventType($summit_id = 23){ + + $new_event_type = $this->testAddEventType($summit_id); + + $params = [ + 'id' => $summit_id, + 'event_type_id' => $new_event_type->id, + ]; + + $data = [ + 'color' => "FFAAFF", + 'class_name' => \models\summit\SummitEventType::ClassName, + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitsEventTypesApiController@updateEventTypeBySummit", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $event_type = json_decode($content); + $this->assertTrue(!is_null($event_type)); + $this->assertTrue($event_type->color == '#FFAAFF'); + return $event_type; + } + + } \ No newline at end of file