Unverified Commit 29ab8f80 authored by Andrea Ruggiero's avatar Andrea Ruggiero
Browse files

Add restaurants api

parent 891faffe
Pipeline #1289 passed with stages
......@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Helpers\ApiResponse;
use App\Repositories\EventRepository;
use Illuminate\Http\Request;
class EventController extends Controller
{
......@@ -34,8 +35,23 @@ class EventController extends Controller
// TODO: Add subscriptions
}
public function getEvent() {
// TODO: Get single events details
public function getEvent(Request $request, EventRepository $eventRepository) {
$body = json_decode($request->getContent(), true);
if (!isset($body['id'])) return ApiResponse::error('Bad request', 400);
$id = $body['id'];
$event = $eventRepository->get($id);
if ($event === null) return ApiResponse::error('Evento non trovato');
return ApiResponse::success([ 'event' => [
"id" => $event->id,
"name" => $event->name,
"cover" => $event->cover,
"description" => $event->description
]
]);
}
}
\ No newline at end of file
<?php
namespace App\Http\Controllers;
use App\Helpers\ApiResponse;
use App\Repositories\EventRepository;
use App\Repositories\RestaurantRepository;
use Illuminate\Http\Request;
class RestaurantController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getEvents(RestaurantRepository $restaurantRepository) {
$restaurants = $restaurantRepository->all();
$response = [];
foreach ($restaurants as $restaurant) {
$response[] = [
"id" => $restaurant->id,
"name" => $restaurant->name,
"cover" => $restaurant->cover,
"description" => $restaurant->description
];
}
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
......@@ -31,4 +31,10 @@ $app->group(['prefix' => '/api/v1', 'namespace' => 'App\Http\Controllers'], func
$app->get('/event/{id}', 'EventController@getEvent');
// TODO: Unsubscribe?
/*
* Restaurants
*/
$app->get('/restaurants', 'RestaurantController@getRestaurants');
$app->get('/restaurant/{id}', 'RestaurantController@getRestaurant');
});
\ No newline at end of file
......@@ -8,4 +8,8 @@ class EventRepository extends BaseRepository
protected $table = 'events';
public function get($id) {
return $this->db->where('id', $id)->first();
}
}
\ No newline at end of file
<?php
namespace App\Repositories;
class RestaurantRepository extends BaseRepository
{
protected $table = 'restaurants';
public function get($id) {
return $this->db->where('id', $id)->first();
}
}
\ No newline at end of file
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