-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vagrant
committed
Jan 5, 2019
0 parents
commit 56be68d
Showing
42 changed files
with
5,214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
APP_NAME=Lumen | ||
APP_ENV=local | ||
APP_KEY= | ||
APP_DEBUG=true | ||
APP_URL=http://book.test | ||
APP_TIMEZONE=UTC | ||
|
||
LOG_CHANNEL=stack | ||
LOG_SLACK_WEBHOOK_URL= | ||
|
||
DB_CONNECTION=mysql | ||
DB_HOST=127.0.0.1 | ||
DB_PORT=3306 | ||
DB_DATABASE=book | ||
DB_USERNAME=homestead | ||
DB_PASSWORD=secret | ||
|
||
|
||
|
||
|
||
|
||
CACHE_DRIVER=file | ||
QUEUE_CONNECTION=sync | ||
|
||
|
||
ACCEPTED_SECRETS= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/vendor | ||
/.idea | ||
Homestead.json | ||
Homestead.yaml | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Book extends Model | ||
{ | ||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'title', | ||
'description', | ||
'price', | ||
'author_id', | ||
]; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Console; | ||
|
||
use Illuminate\Console\Scheduling\Schedule; | ||
use Laravel\Lumen\Console\Kernel as ConsoleKernel; | ||
|
||
class Kernel extends ConsoleKernel | ||
{ | ||
/** | ||
* The Artisan commands provided by your application. | ||
* | ||
* @var array | ||
*/ | ||
protected $commands = [ | ||
// | ||
]; | ||
|
||
/** | ||
* Define the application's command schedule. | ||
* | ||
* @param \Illuminate\Console\Scheduling\Schedule $schedule | ||
* @return void | ||
*/ | ||
protected function schedule(Schedule $schedule) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use Illuminate\Queue\SerializesModels; | ||
|
||
abstract class Event | ||
{ | ||
use SerializesModels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
class ExampleEvent extends Event | ||
{ | ||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use App\Traits\ApiResponser; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Validation\ValidationException; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class Handler extends ExceptionHandler | ||
{ | ||
use ApiResponser; | ||
|
||
/** | ||
* A list of the exception types that should not be reported. | ||
* | ||
* @var array | ||
*/ | ||
protected $dontReport = [ | ||
AuthorizationException::class, | ||
HttpException::class, | ||
ModelNotFoundException::class, | ||
ValidationException::class, | ||
]; | ||
|
||
/** | ||
* Report or log an exception. | ||
* | ||
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. | ||
* | ||
* @param \Exception $exception | ||
* @return void | ||
*/ | ||
public function report(Exception $exception) | ||
{ | ||
parent::report($exception); | ||
} | ||
|
||
/** | ||
* Render an exception into an HTTP response. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Exception $exception | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function render($request, Exception $exception) | ||
{ | ||
if ($exception instanceof HttpException) { | ||
$code = $exception->getStatusCode(); | ||
$message = Response::$statusTexts[$code]; | ||
|
||
return $this->errorResponse($message, $code); | ||
} | ||
|
||
if ($exception instanceof ModelNotFoundException) { | ||
$model = strtolower(class_basename($exception->getModel())); | ||
|
||
return $this->errorResponse("No existe una instacia de {$model} con el id enviado", Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
if ($exception instanceof AuthorizationException) { | ||
return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
if ($exception instanceof AuthenticationException) { | ||
return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
if ($exception instanceof ValidationException) { | ||
$errors = $exception->validator->errors()->getMessages(); | ||
|
||
return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
if (env('APP_DEBUG', false)) { | ||
return parent::render($request, $exception); | ||
} | ||
|
||
return $this->errorResponse('Error inesperado, intente mas tarde', Response::HTTP_INTERNAL_SERVER_ERROR); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Book; | ||
use App\Traits\ApiResponser; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class BookController extends Controller | ||
{ | ||
use ApiResponser; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Retornamos un listado de book | ||
* @return Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
$books = Book::all(); | ||
|
||
|
||
return $this->successResponse($books); | ||
} | ||
|
||
/** | ||
* Creamos un instacia de book | ||
* @return Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$rules = [ | ||
'title' => 'required|max:255', | ||
'description' => 'required|max:255', | ||
'price' => 'required|min:1', | ||
'author_id' => 'required|min:1', | ||
]; | ||
|
||
|
||
$this->validate($request, $rules); | ||
|
||
$book = Book::create($request->all()); | ||
|
||
return $this->successResponse($book, Response::HTTP_CREATED); | ||
} | ||
|
||
/** | ||
* Retornamos un book especifico | ||
* @return Illuminate\Http\Response | ||
*/ | ||
public function show($book) | ||
{ | ||
$book = Book::findOrFail($book); | ||
|
||
return $this->successResponse($book); | ||
} | ||
|
||
/** | ||
* Actualizamos la información de un book | ||
* @return Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, $book) | ||
{ | ||
$rules = [ | ||
'title' => 'max:255', | ||
'description' => 'max:255', | ||
'price' => 'min:1', | ||
'author_id' => 'min:1', | ||
]; | ||
|
||
$this->validate($request, $rules); | ||
|
||
$book = Book::findOrFail($book); | ||
|
||
$book->fill($request->all()); | ||
|
||
if ($book->isClean()) { | ||
return $this->errorResponse('At least one value must change', Response::HTTP_UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
$book->save(); | ||
|
||
return $this->successResponse($book); | ||
} | ||
|
||
/** | ||
* Eliminamos un book | ||
* @return Illuminate\Http\Response | ||
*/ | ||
public function destroy($book) | ||
{ | ||
$book = Book::findOrFail($book); | ||
|
||
$book->delete(); | ||
|
||
return $this->successResponse($book); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Laravel\Lumen\Routing\Controller as BaseController; | ||
|
||
class Controller extends BaseController | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
class ExampleController extends Controller | ||
{ | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Factory as Auth; | ||
|
||
class Authenticate | ||
{ | ||
/** | ||
* The authentication guard factory instance. | ||
* | ||
* @var \Illuminate\Contracts\Auth\Factory | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Factory $auth | ||
* @return void | ||
*/ | ||
public function __construct(Auth $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @param string|null $guard | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next, $guard = null) | ||
{ | ||
if ($this->auth->guard($guard)->guest()) { | ||
return response('Unauthorized.', 401); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
Oops, something went wrong.