diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitAttendeesApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitAttendeesApiController.php index 3c027fec..04a9c4d6 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitAttendeesApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitAttendeesApiController.php @@ -615,4 +615,37 @@ final class OAuth2SummitAttendeesApiController extends OAuth2ProtectedController return $this->error500($ex); } } + + /** + * @param $summit_id + * @param $attendee_id + * @param $ticket_id + * @return mixed + */ + public function deleteAttendeeTicket($summit_id, $attendee_id, $ticket_id){ + try { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $attendee = $this->attendee_repository->getById($attendee_id); + if(is_null($attendee)) return $this->error404(); + + $ticket = $this->attendee_service->deleteAttendeeTicket($attendee, $ticket_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 73505018..78c829f1 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -175,6 +175,7 @@ Route::group([ Route::group(array('prefix' => 'tickets'), function () { Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitAttendeesApiController@addAttendeeTicket']); + Route::delete('{ticket_id}', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitAttendeesApiController@deleteAttendeeTicket']); }); }); }); diff --git a/app/Models/Foundation/Summit/Attendees/SummitAttendee.php b/app/Models/Foundation/Summit/Attendees/SummitAttendee.php index 53d740ed..4a857f68 100644 --- a/app/Models/Foundation/Summit/Attendees/SummitAttendee.php +++ b/app/Models/Foundation/Summit/Attendees/SummitAttendee.php @@ -237,5 +237,25 @@ class SummitAttendee extends SilverstripeBaseModel return $this->member->getRsvpByEvent($event_id); } + /** + * @param int $ticket_id + * @return SummitAttendeeTicket + */ + public function getTicketById($ticket_id){ + $ticket = $this->tickets->matching( + $criteria = Criteria::create() + ->where(Criteria::expr()->eq("id", $ticket_id)) + )->first(); + return $ticket ? $ticket : null; + } + + /** + * @param SummitAttendeeTicket $ticket + * @return $this + */ + public function removeTicket(SummitAttendeeTicket $ticket){ + $this->tickets->removeElement($ticket); + return $this; + } } \ No newline at end of file diff --git a/app/Services/Model/AttendeeService.php b/app/Services/Model/AttendeeService.php index c25c5c00..e480802e 100644 --- a/app/Services/Model/AttendeeService.php +++ b/app/Services/Model/AttendeeService.php @@ -242,4 +242,22 @@ final class AttendeeService implements IAttendeeService return SummitAttendeeTicketFactory::build($attendee, $type, $data); }); } + + /** + * @param SummitAttendee $attendee + * @param int $ticket_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return SummitAttendeeTicket + */ + public function deleteAttendeeTicket(SummitAttendee $attendee, $ticket_id) + { + return $this->tx_service->transaction(function() use($attendee, $ticket_id){ + $ticket = $attendee->getTicketById($ticket_id); + if(is_null($ticket)){ + throw new EntityNotFoundException(sprintf("ticket id %s does not belongs to attendee id %s", $ticket_id, $attendee->getId())); + } + $attendee->removeTicket($ticket); + }); + } } \ No newline at end of file diff --git a/app/Services/Model/IAttendeeService.php b/app/Services/Model/IAttendeeService.php index 6ecf62a5..fe72204a 100644 --- a/app/Services/Model/IAttendeeService.php +++ b/app/Services/Model/IAttendeeService.php @@ -58,4 +58,13 @@ interface IAttendeeService * @return SummitAttendeeTicket */ public function addAttendeeTicket(SummitAttendee $attendee, array $data); + + /** + * @param SummitAttendee $attendee + * @param int $ticket_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return SummitAttendeeTicket + */ + public function deleteAttendeeTicket(SummitAttendee $attendee, $ticket_id); } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 0bd2ac09..faa060a7 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -188,6 +188,14 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteAttendeesData, $current_realm), ], ), + array( + 'name' => 'delete-attendee-ticket', + 'route' => '/api/v1/summits/{id}/attendees/{attendee_id}/tickets/{ticket_id}', + 'http_method' => 'DELETE', + 'scopes' => [ + sprintf(SummitScopes::WriteAttendeesData, $current_realm), + ], + ), // speakers array( 'name' => 'get-speakers', diff --git a/tests/OAuth2AttendeesApiTest.php b/tests/OAuth2AttendeesApiTest.php index 54a6a9a8..a6ee2968 100644 --- a/tests/OAuth2AttendeesApiTest.php +++ b/tests/OAuth2AttendeesApiTest.php @@ -74,7 +74,7 @@ class OAuth2AttendeesApiTest extends ProtectedApiTest $this->assertTrue(!is_null($attendee)); } - public function testGetAttendeeByID($attendee_id = 12641){ + public function testGetAttendeeByID($attendee_id = 12642){ $params = [ 'id' => 23, @@ -267,4 +267,29 @@ class OAuth2AttendeesApiTest extends ProtectedApiTest $this->assertTrue(!is_null($ticket)); return $ticket; } + + public function testDeleteAttendeeTicket(){ + $params = [ + 'id' => 23, + 'attendee_id' => 12642, + 'ticket_id' => 14161 + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "DELETE", + "OAuth2SummitAttendeesApiController@deleteAttendeeTicket", + $params, + [], + [], + [], + $headers + ); + + $this->assertResponseStatus(204); + } } \ No newline at end of file