Commit 5fd107cd authored by Andrea Ruggiero's avatar Andrea Ruggiero
Browse files

Add api for un-booking

parent 52f6b769
Pipeline #1526 failed with stage
in 0 seconds
......@@ -49,6 +49,33 @@ class EventController extends Controller
return ApiResponse::success($response);
}
public function getBookedEvents(EventRepository $eventRepository, BookingRepository $bookingRepository,
SessionRepository $sessionRepository, Request $request) {
$events = $eventRepository->all();
$response = [];
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
foreach ($events as $event) {
if ($bookingRepository->getBookingByEventAndUser($user->user_id, $event->event_id) === null) continue;
$response[] = [
"id" => $event->event_id,
"title" => $event->title,
"speaker" => $event->speaker,
"date" => $event->date,
"start" => $event->start,
"end" => $event->end,
"place" => $event->place,
"type" => $event->type,
"theme" => $event->theme,
"capacity" => $event->capacity,
"bookings" => count($bookingRepository->getBookingsByEvent($event->event_id))
];
}
return ApiResponse::success($response);
}
public function postBooking($id, Request $request, EventRepository $eventRepository,
SessionRepository $sessionRepository, BookingRepository $bookingRepository) {
$event = $eventRepository->get($id);
......@@ -64,6 +91,21 @@ class EventController extends Controller
return ApiResponse::success();
}
public function deleteBooking($id, Request $request, EventRepository $eventRepository,
SessionRepository $sessionRepository, BookingRepository $bookingRepository) {
$event = $eventRepository->get($id);
if ($event === null) return ApiResponse::error('Evento non trovato');
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
if ($bookingRepository->getBookingByEventAndUser($user->user_id, $event->event_id) === null)
return ApiResponse::error('Non ti sei mai prenotato!');
$bookingRepository->deleteBooking($user->user_id, $event->event_id);
return ApiResponse::success();
}
public function getEvent($id, EventRepository $eventRepository, Request $request,
BookingRepository $bookingRepository, SessionRepository $sessionRepository) {
$event = $eventRepository->get($id);
......
......@@ -27,9 +27,10 @@ $app->group(['prefix' => '/api/v1', 'namespace' => 'App\Http\Controllers'], func
* Events
*/
$app->get('/events', 'EventController@getEvents');
$app->post('/events/subscribe', 'EventController@postSubscribe');
$app->get('/event/{id}', 'EventController@getEvent');
$app->get('/events/booked', 'EventController@getBookedEvents');
$app->post('/event/{id}/book', 'EventController@postBooking');
$app->delete('/event/{id}/unbook', 'EventController@deleteBooking');
/*
* Restaurants
......
......@@ -12,6 +12,10 @@ class BookingRepository extends BaseRepository
return $this->db->insert(['user_id' => $user_id, 'event_id' => $event_id]);
}
public function deleteBooking($user_id, $event_id) {
return $this->db->where('event_id', $event_id)->where('user_id', $user_id)->delete();
}
public function getBookingsByEvent($event_id) {
return $this->db->where('event_id', $event_id)->get();
}
......
......@@ -171,6 +171,70 @@
}
}
},
"/events/booked" : {
"get" : {
"tags" : [ "Events" ],
"summary" : "/events/booked",
"description" : "",
"operationId" : "GET_events_booked",
"consumes" : [ "application/json" ],
"produces" : [ "application/json" ],
"parameters" : [ ],
"responses" : {
"200" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_200"
},
"examples" : {
"application/json" : {
"ok" : true,
"data": [
{
"id": 1,
"title": "???",
"speaker": "???",
"date": "2016-06-26",
"start": "02:30:00",
"end": "03:00:00",
"place": "Gym palace",
"type": "User digest",
"theme": "Technical",
"capacity": 127,
"bookings": 0
},
{
"id": 2,
"title": "(Dis)information-graphics – Problems, challenges and prospects of visual information in Wikipedia",
"speaker": "Martin Kraft",
"date": "2016-06-25",
"start": "04:30:00",
"end": "05:00:00",
"place": "Meeting",
"type": "Critical issue",
"theme": "Technical",
"capacity": 100,
"bookings": 1
}
]
}
}
},
"default" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_default"
},
"examples" : {
"application/json" : {
"ok" : false,
"error" : "Errore generale"
}
}
}
}
}
},
"/event/{id}" : {
"get" : {
"tags" : [ "Events" ],
......@@ -297,6 +361,63 @@
}
}
},
"/event/{id}/unbook" : {
"post" : {
"tags" : [ "Events" ],
"summary" : "/event/{id}/unbook",
"description" : "",
"operationId" : "DELETE_event_unbook",
"consumes" : [ "application/json" ],
"produces" : [ "application/json" ],
"parameters" : [ {
"name" : "X-Auth-Token",
"in" : "header",
"description" : "",
"required" : true,
"type" : "string"
},
{
"name" : "id",
"in" : "path",
"description" : "",
"required" : true,
"type" : "int"
}, {
"name" : "Content-Type",
"in" : "header",
"description" : "",
"required" : false,
"type" : "string",
"default" : " application/json"
} ],
"responses" : {
"200" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_200"
},
"examples" : {
"application/json" : {
"ok": true,
"data": "success"
}
}
},
"default" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_default"
},
"examples" : {
"application/json" : {
"ok": false,
"error": "Non ti sei mai prenotato!"
}
}
}
}
}
},
"/profile" : {
"post" : {
"tags" : [ "Users" ],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment