Commit 2b8c657a authored by Andrea Ruggiero's avatar Andrea Ruggiero
Browse files

Finish events and restaurants api

parent dc2304d4
Pipeline #1455 failed with stage
in 1 minute and 8 seconds
......@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
use App\Helpers\ApiResponse;
use App\Repositories\BookingRepository;
use App\Repositories\EventRepository;
use App\Repositories\SessionRepository;
use Illuminate\Http\Request;
class EventController extends Controller
......@@ -15,41 +17,69 @@ class EventController extends Controller
$this->middleware('auth');
}
public function getEvents(EventRepository $eventRepository) {
public function getEvents(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) {
$response[] = [
"id" => $event->id,
"name" => $event->name,
"cover" => $event->cover,
"description" => $event->description
"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)),
"hasBooked" => $bookingRepository->getBookingByEventAndUser($user->user_id, $event->event_id) !== null
];
}
return ApiResponse::success($response);
}
public function postSubscribe() {
// TODO: Add subscriptions
}
public function postBooking($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'));
$bookingRepository->addBooking($user->user_id, $event->event_id);
public function getEvent(Request $request, EventRepository $eventRepository) {
$body = json_decode($request->getContent(), true);
return ApiResponse::success();
}
if (!isset($body['id'])) return ApiResponse::error('Bad request', 400);
$id = $body['id'];
public function getEvent($id, EventRepository $eventRepository, Request $request,
BookingRepository $bookingRepository, SessionRepository $sessionRepository) {
$event = $eventRepository->get($id);
if ($event === null) return ApiResponse::error('Evento non trovato');
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
return ApiResponse::success([ 'event' => [
"id" => $event->id,
"name" => $event->name,
"cover" => $event->cover,
"description" => $event->description
"id" => $event->event_id,
"title" => $event->title,
"speaker" => $event->speaker,
"date" => $event->date,
"start" => $event->start,
"end" => $event->end,
"place" => $event->place,
"address" => $event->address,
"latitude" => $event->latitude,
"longitude" => $event->longitude,
"capacity" => $event->capacity,
"type" => $event->type,
"theme" => $event->theme,
"bookings" => count($bookingRepository->getBookingsByEvent($event->event_id)),
"hasBooked" => $bookingRepository->getBookingByEventAndUser($user->user_id, $event->event_id) !== null
]
]);
}
......
......@@ -16,39 +16,21 @@ class RestaurantController extends Controller
$this->middleware('auth');
}
public function getEvents(RestaurantRepository $restaurantRepository) {
public function getRestaurants(RestaurantRepository $restaurantRepository) {
$restaurants = $restaurantRepository->all();
$response = [];
foreach ($restaurants as $restaurant) {
$response[] = [
"id" => $restaurant->id,
"restaurant_id" => $restaurant->restaurant_id,
"name" => $restaurant->name,
"cover" => $restaurant->cover,
"description" => $restaurant->description
"address" => $restaurant->address,
"latitude" => $restaurant->latitude,
"longitude" => $restaurant->longitude,
"phone_number" => $restaurant->phone_number
];
}
return ApiResponse::success($response);
}
public function getEvent(Request $request, RestaurantRepository $restaurantRepository) {
$body = json_decode($request->getContent(), true);
if (!isset($body['id'])) return ApiResponse::error('Bad request', 400);
$id = $body['id'];
$restaurant = $restaurantRepository->get($id);
if ($restaurant === null) return ApiResponse::error('Ristorante non trovato');
return ApiResponse::success([ 'event' => [
"id" => $restaurant->id,
"name" => $restaurant->name,
"cover" => $restaurant->cover,
"description" => $restaurant->description
]
]);
}
}
\ No newline at end of file
......@@ -23,15 +23,15 @@ class UserController extends Controller
{
$body = json_decode($request->getContent(), true);
if (!isset($body['number'])) return ApiResponse::error('Bad request', 400);
if (!isset($body['key'])) return ApiResponse::error('Bad request', 400);
$number = $body['number'];
$user = $userRepository->getByNumber($number);
$key = $body['key'];
$user = $userRepository->getById($key);
if ($user === null) return ApiResponse::error('Utente non trovato', 403);
$token = uniqid();
if (!$sessionRepository->addTokenToUser($token, $user->id)) return ApiResponse::error('Errore generale');
if (!$sessionRepository->addTokenToUser($token, $user->user_id)) return ApiResponse::error('Errore generale');
return ApiResponse::success(['token' => $token]);
}
......
......@@ -29,12 +29,11 @@ $app->group(['prefix' => '/api/v1', 'namespace' => 'App\Http\Controllers'], func
$app->get('/events', 'EventController@getEvents');
$app->post('/events/subscribe', 'EventController@postSubscribe');
$app->get('/event/{id}', 'EventController@getEvent');
// TODO: Unsubscribe?
$app->post('/event/{id}/book', 'EventController@postBooking');
/*
* Restaurants
*/
$app->get('/restaurants', 'RestaurantController@getRestaurants');
$app->get('/restaurant/{id}', 'RestaurantController@getRestaurant');
});
\ No newline at end of file
<?php
namespace App\Repositories;
class BookingRepository extends BaseRepository
{
protected $table = 'bookings';
public function addBooking($user_id, $event_id) {
return $this->db->insert(['user_id' => $user_id, 'event_id' => $event_id]);
}
public function getBookingsByEvent($event_id) {
return $this->db->where('event_id', $event_id)->get();
}
public function getBookingByEventAndUser($user_id, $event_id) {
return $this->db->where('event_id', $event_id)->where('user_id', $user_id)->first();
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ class EventRepository extends BaseRepository
protected $table = 'events';
public function get($id) {
return $this->db->where('id', $id)->first();
return $this->db->where('event_id', $id)->first();
}
}
\ No newline at end of file
......@@ -5,10 +5,10 @@ namespace App\Repositories;
class UserRepository extends BaseRepository
{
protected $table = 'utenti';
protected $table = 'users';
public function getByNumber($number) {
return $this->db->where('id_utente', $number)->first();
public function getById($id) {
return $this->db->where('user_id', $id)->first();
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -199,7 +199,7 @@
<span class="warning"><strong>Dead Code</strong></span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
</footer>
......
......@@ -166,7 +166,7 @@
<span class="warning"><strong>Dead Code</strong></span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
</footer>
......
......@@ -139,7 +139,7 @@
<footer>
<hr/>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
......@@ -109,7 +109,7 @@
<span class="success"><strong>High</strong>: 70% to 100%</span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
......@@ -126,7 +126,7 @@
<span class="warning"><strong>Dead Code</strong></span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
</footer>
......
......@@ -137,7 +137,7 @@
<footer>
<hr/>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
......@@ -109,7 +109,7 @@
<span class="success"><strong>High</strong>: 70% to 100%</span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
......@@ -252,7 +252,7 @@
<span class="warning"><strong>Dead Code</strong></span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
</footer>
......
......@@ -144,7 +144,7 @@
<footer>
<hr/>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
......@@ -109,7 +109,7 @@
<span class="success"><strong>High</strong>: 70% to 100%</span>
</p>
<p>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Wed May 11 21:26:42 UTC 2016.</small>
<small>Generated by <a href="http://github.com/sebastianbergmann/php-code-coverage" target="_top">PHP_CodeCoverage 3.3.1</a> using <a href="http://php.net/" target="_top">PHP 7.0.6</a> and <a href="https://phpunit.de/">PHPUnit 5.3.2</a> at Tue May 31 18:37:22 UTC 2016.</small>
</p>
</footer>
</div>
......
This diff is collapsed.
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