Added update event type endpoint
PUT /api/v1/summits/{id}/event-types/{event_type_id} Change-Id: I0041c40d94c9e6ec7ccd91c72091e79415d2e491
This commit is contained in:
parent
74513b5529
commit
7c22e7401e
|
@ -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',
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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');
|
||||
});
|
||||
|
|
|
@ -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']));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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',
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue