Commit cd797229 authored by Andrea Ruggiero's avatar Andrea Ruggiero
Browse files

Add api for profile and accomodation

parent 1b3088fc
Pipeline #1505 failed with stage
<?php
namespace App\Http\Controllers;
use App\Helpers\ApiResponse;
use App\Repositories\AccomodationRepository;
use App\Repositories\SessionRepository;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;
class AccomodationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getAccomodation(Request $request, SessionRepository $sessionRepository,
AccomodationRepository $accomodationRepository, UserRepository $userRepository) {
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
$user = $userRepository->getById($user->user_id);
$accomodation = $accomodationRepository->getById($user->accomodation_id);
if ($accomodation === null) {
return ApiResponse::error("L'utente non ha un alloggio assegnato", 404);
}
return ApiResponse::success([ 'accomodation' => [
"address" => $accomodation->address,
"n_seats" => $accomodation->n_seats,
"owner_name" => $accomodation->owner_name,
"phone_number" => $accomodation->phone_number,
"latitude" => $accomodation->latitude,
"longitude" => $accomodation->longitude
]
]);
}
}
\ No newline at end of file
......@@ -43,4 +43,16 @@ class UserController extends Controller
return ApiResponse::success();
}
public function getProfile(Request $request, SessionRepository $sessionRepository, UserRepository $userRepository) {
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
$user = $userRepository->getById($user->user_id);
return ApiResponse::success([ 'user' => [
"name" => $user->name,
"surname" => $user->surname
]
]);
}
}
\ No newline at end of file
......@@ -36,4 +36,14 @@ $app->group(['prefix' => '/api/v1', 'namespace' => 'App\Http\Controllers'], func
*/
$app->get('/restaurants', 'RestaurantController@getRestaurants');
/*
* Profile
*/
$app->get('/profile', 'UserController@getProfile');
/*
* Accomodation
*/
$app->get('/accomodation', 'AccomodationController@getAccomodation');
});
\ No newline at end of file
<?php
namespace App\Repositories;
class AccomodationRepository extends BaseRepository
{
protected $table = 'accomodations';
public function getById($id) {
return $this->db->where('accomodation_id', $id)->first();
}
}
\ No newline at end of file
<?php
use App\Application;
use app\Repositories\AccomodationRepository;
use App\Repositories\EventRepository;
use App\Repositories\RestaurantRepository;
use App\Repositories\SessionRepository;
use App\Repositories\UserRepository;
......@@ -65,6 +67,14 @@ $app->singleton('EventRepository', function ($app) {
return new EventRepository($app('db'));
});
$app->singleton('RestaurantRepository', function ($app) {
return new RestaurantRepository($app('db'));
});
$app->singleton('AccomodationRepository', function ($app) {
return new AccomodationRepository($app('db'));
});
$app->singleton('Application', function ($app) {
return new Application();
});
......
......@@ -309,6 +309,102 @@
}
}
},
"/profile" : {
"post" : {
"tags" : [ "Users" ],
"summary" : "/profile",
"description" : "",
"operationId" : "GET_profile",
"consumes" : [ "application/json" ],
"produces" : [ "application/json" ],
"parameters" : [ {
"name" : "X-Auth-Token",
"in" : "header",
"description" : "",
"required" : true,
"type" : "string"
},
{
"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":{"user":{"name":"Andrea","surname":"Ruggiero"}}}
}
},
"default" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_default"
},
"examples" : {
"application/json" : {
"ok": false,
"error": "Errore generale"
}
}
}
}
}
},
"/accomodation" : {
"post" : {
"tags" : [ "Accomodations" ],
"summary" : "/accomodation",
"description" : "",
"operationId" : "GET_accomodation",
"consumes" : [ "application/json" ],
"produces" : [ "application/json" ],
"parameters" : [ {
"name" : "X-Auth-Token",
"in" : "header",
"description" : "",
"required" : true,
"type" : "string"
},
{
"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":{"accomodation":{"address":"Indirizzo 1","n_seats":666,"owner_name":"Dio","phone_number":"+39 6666666666","latitude":666,"longitude":666}}}
}
},
"default" : {
"description" : "",
"schema" : {
"$ref" : "#/definitions/inline_response_default"
},
"examples" : {
"application/json" : {
"ok": false,
"error": "L'utente non ha un alloggio assegnato"
}
}
}
}
}
},
"/restaurants" : {
"get" : {
"tags" : [ "Restaurants" ],
......
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